3

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?

1
  • Do the strings hold filenames of files you want to compress or do you want to compress the strings themselves? Commented Apr 26, 2013 at 19:29

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.