【问题标题】:Creation gzip archive using Apache Commons Compress使用 Apache Commons Compress 创建 gzip 存档
【发布时间】:2013-01-05 11:15:30
【问题描述】:

我成功创建了具有预期内容的gz 存档,但是如何在存档中设置文件名?

我的意思是,如果创建存档myfile.gz,其中的文件将命名为“myfile”,但我想将其命名为源文件,例如“1.txt”

当前代码:

public static void gz() throws FileNotFoundException, IOException {
    GZIPOutputStream out = null;
    String filePaths[] =  {"C:/Temp/1.txt","C:/Temp/2.txt"}; 
    try {
         out = new GZIPOutputStream(
              new BufferedOutputStream(new FileOutputStream("C:/Temp/myfile.gz")));

            RandomAccessFile f = new RandomAccessFile(filePaths[0], "r");
            byte[] b = new byte[(int)f.length()];
            f.read(b);
            out.write(b, 0, b.length);

            out.finish();
            out.close();
    } finally {
         if(out != null) out.close();
    }
}

【问题讨论】:

    标签: java compression gzip archive apache-commons


    【解决方案1】:

    GZip 压缩流。通常,当人们对多个文件使用 GZip 时,他们也会使用 tar 将它们一起咀嚼。 gzip archive with multiple files inside

    【讨论】:

    • 感谢您的回复,但我想知道的不是如何在一个 gzip 存档中添加多个文件,而是可以在 gz 存档中重命名流。就像存档名称是:“myarch.gz”,但在存档名称中流式传输,例如“iTextFile.txt”。
    • 不幸的是你不能。它是一个字节流。里面没有名字。
    猜你喜欢
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    相关资源
    最近更新 更多