Ágota Horváth
Ágota Horváth

Reputation: 1353

How to create a Gzip archive in java, from strings?

I have 3 strings, each one of them representing a txt file content, not loaded from the computer, but generated by 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

How do I create a GZIP file, with the three files in it, and save the compressed file to the disc?

Upvotes: 3

Views: 9391

Answers (3)

martinez314
martinez314

Reputation: 12332

It's unclear if you want to store just the text or actual individual files. I don't think you can store multiple files in a GZIP without first TARing. Here is an example for storing a string to GZIP though. Maybe it will help you along:

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) { }
    }
}

Upvotes: 0

Modus Tollens
Modus Tollens

Reputation: 5123

To create a zip-file named output.zip that contains the files 1.txt, 2.txt and 3.txt with their content string, try the following:

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();
    }
}

See Creating ZIP and JAR files for more information, in particular the chapter Compressing Files.

Upvotes: 2

Aurand
Aurand

Reputation: 5537

Generally speaking, GZIP is only used for compressing single files (hence why java.util.zip.GZIPOutputStream only really supports a single entry).

For multiple files I'd recommend using a format designed for multiple files (like zip). java.util.zip.ZipOutputStream provides just that. If, for some reason, you really want the end result to be a GZIP, you could always create a ZIP file which contains all your 3 files and then GZIP that.

Upvotes: 0

Related Questions