如何完全从Java中的内存(无文件)中的对象创建tar或tar.gz存档

mar

如何在Java中创建不受File实际文件支持的tar或gzip压缩tar归档文件?

我已经找到commons-compress,但是示例和大多数文档都依赖于使用JavaFile对象可以引用的现有文件如果我不想使用File对象并想从构建tar归档,该怎么办byte[]

唯一的构造函数TarArchiveEntry提供了一种设置内容接受的方式,File而没有针对内容的设置器。

TarArchiveEntry文档中:

TarArchiveEntry(File file)
   Construct an entry for a file.
TarArchiveEntry(File file, String fileName)
    Construct an entry for a file.
mar

使用commons-compress,文档中并没有立即明确或举例说明,但这是要点

//Get the content you want to lay down into a byte[]
byte content[] = "I am some simple content that should be written".getBytes();

//Name your entry with the complete path relative to the base directory
//of your archive. Any directories that don't exist (e.g. "testDir") will 
//be created for you
TarArchiveEntry textFile = new TarArchiveEntry("testDir/hello.txt");

//Make sure to set the size of the entry. If you don't you will not be able
//to write to it
textFile.setSize(content.length);


TarArchiveOutputStream gzOut = null;
try {

    /*In this case I chose to show how to lay down a gzipped archive.
      You could just as easily remove the GZIPOutputStream to lay down a plain tar.
      You also should be able to replace the FileOutputStream with 
      a ByteArrayOutputStream to lay nothing down on disk if you wanted 
      to do something else with your newly created archive
    */
    gzOut = new TarArchiveOutputStream(
                new GZIPOutputStream(
                new BufferedOutputStream(
                new FileOutputStream("/tmp/mytest.tar.gz")
            )));

    //When you put an ArchiveEntry into the archive output stream, 
    //it sets it as the current entry
    gzOut.putArchiveEntry(textFile);

    //The write command allows you to write bytes to the current entry 
    //on the output stream, which was set by the above command.
    //It will not allow you to write any more than the size
    //that you specified when you created the archive entry above
    gzOut.write(content);

    //You must close the current entry when you are done with it. 
    //If you are appending multiple archive entries, you only need  
    //to close the last one. The putArchiveEntry automatically closes
    //the previous "current entry" if there was one
    gzOut.closeArchiveEntry();

} catch (Exception ex) {
    System.err.println("ERROR: " + ex.getMessage());
} finally {
    if (gzOut != null) {
        try {
            gzOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从tar.gz存档中删除单个文件

从Nim中的tar.gz存档中读取文件

从python中的tar存档中提取压缩的gz文件

从Gradle中的.tar.gz存档获取tarball

创建不同位置的多个目录的 tar.gz 存档——《tar:懦弱拒绝创建空存档》

从多个目录创建多个tar.gz存档

Ansible不能取消存档tar.gz文件

Python 3:从tar.gz存档中提取文件

将tar.gz存档中压缩的多个文件读入Spark

在Pyspark中读取tar.gz存档时使用特定模式过滤文件

将 tar.gz 存档提取到不同的文件夹中(由于限制)

正确区分压缩的.gz文件和存档的tar.gz文件?

使用python查看.gz存档中包含的文件

如何从大型tar.gz存档中提取单个文件夹?

javascript - 在 google 脚本中解压缩 tar.gz 存档

提取tar.gz文件将返回“看起来不像tar存档”。

如何在C#中创建tar.gz文件

如何在Scala中正确解压缩gz存档

rpm 规范:是否可以在准备之前自动创建 tar.gz 存档?

使用Python 3.6和tarfile模块在Windows 10上创建.tar.gz存档

发出使分割的tar.gz文件重新组合和提取的问题。不是有效的.gz存档

如何将tar gz存档解压缩到特定的目标?

如何卸载从.tar.gz存档安装的笔记本电脑模式工具?

如何重命名.tar.gz文件而不提取内容并在UBUNTU中创建新的.tar.gz文件?

无法使用tar.gz存档从源代码安装应用程序

将.tar.gz存档解压缩到特定目录

在父目录中创建tar存档

脚本中的sed创建了损坏的.tar.gz文件

如何从python中的.tar存档中提取特定文件?