如何用java从字符串创建Gzip存档?

如何用java从字符串创建Gzip存档?,java,compression,zip,gzip,Java,Compression,Zip,Gzip,我有3个字符串,每个字符串表示txt文件内容,不是从计算机加载的,而是由Java生成的 String firstFileCon = "firstContent"; //File in .gz: 1.txt String secondFileCon = "secondContent"; //File in .gz: 2.txt String thirdFileCon = "thirdContent"; //File in .gz: 3.txt 如何创建一个包含三个文件的GZIP文件,并将压缩文件

我有3个字符串,每个字符串表示
txt
文件内容,不是从计算机加载的,而是由
Java
生成的

String firstFileCon = "firstContent"; //File in .gz: 1.txt
String secondFileCon = "secondContent"; //File in .gz: 2.txt
String thirdFileCon = "thirdContent"; //File in .gz: 3.txt

如何创建一个包含三个文件的
GZIP
文件,并将压缩文件保存到光盘中?

一般来说,
GZIP
仅用于压缩单个文件(因此为什么
java.util.zip.GZIPOutputStream
只支持单个条目)


对于多个文件,我建议使用为多个文件设计的格式(如zip)
java.util.zip.ZipOutputStream
正好提供了这一点。如果出于某种原因,您确实希望最终结果是一个
GZIP
,您可以始终创建一个包含所有3个文件的
ZIP
文件,然后再创建一个GZIP文件。

要创建一个名为output.ZIP的ZIP文件,其中包含文件1.txt、2.txt和3.txt及其内容字符串,请尝试以下操作:

Map<String, String> entries = new HashMap<String, String>();
entries.put("firstContent", "1.txt");
entries.put("secondContent", "2.txt");
entries.put("thirdContent", "3.txt");

FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
    fos = new FileOutputStream("output.zip");

    zos = new ZipOutputStream(fos);

    for (Map.Entry<String, String> mapEntry : entries.entrySet()) {
        ZipEntry entry = new ZipEntry(mapEntry.getValue()); // create a new zip file entry with name, e.g. "1.txt"
        entry.setMethod(ZipEntry.DEFLATED); // set the compression method
        zos.putNextEntry(entry); // add the ZipEntry to the ZipOutputStream
        zos.write(mapEntry.getKey().getBytes()); // write the ZipEntry content
    }
} catch (FileNotFoundException e) {
    // do something
} catch (IOException e) {
    // do something
} finally {
    if (zos != null) {
        zos.close();
    }
}
Map entries=newhashmap();
条目.put(“firstContent”,“1.txt”);
条目.put(“secondContent”,“2.txt”);
输入(“thirdContent”,“3.txt”);
FileOutputStream=null;
ZipOutputStream zos=null;
试一试{
fos=新文件输出流(“output.zip”);
zos=新ZipoutStream(fos);
for(Map.Entry映射条目:entries.entrySet()){
ZipEntry entry=newZipentry(mapEntry.getValue());//创建一个名为“1.txt”的新zip文件条目
entry.setMethod(ZipEntry.DEFLATED);//设置压缩方法
zos.putNextEntry(条目);//将ZipEntry添加到zipoutput流
write(mapEntry.getKey().getBytes());//编写ZipEntry内容
}
}catch(filenotfounde异常){
//做点什么
}捕获(IOE异常){
//做点什么
}最后{
如果(zos!=null){
zos.close();
}
}

有关更多信息,请参阅,特别是压缩文件一章。

不清楚您是只存储文本还是实际的单个文件。我不认为你可以在一个GZIP中存储多个文件,而不需要事先计算时间。下面是一个将字符串存储到GZIP的示例。也许它会帮助你:

public static void main(String[] args) {
    GZIPOutputStream gos = null;

    try {
        String str = "some string here...";
        File myGzipFile = new File("myFile.gzip");

        InputStream is = new ByteArrayInputStream(str.getBytes());
        gos = new GZIPOutputStream(new FileOutputStream(myGzipFile));

        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1) {
            gos.write(buffer, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try { gos.close(); } catch (IOException e) { }
    }
}

字符串是否包含要压缩的文件的文件名,还是要压缩字符串本身?