tags:

views:

935

answers:

2

Hi,

I am using the Zip utility package of Java and wanted to know how to create a zip file with no compression at all. Setting the level to 0 doesn't help. Is this right?

Also, when I used the 'STORED' method, it throws following exception:

java.util.zip.ZipException: STORED entry missing size, compressed size, or crc-32

I can set the size but now following exception is thrown:

java.util.zip.ZipException: invalid entry crc-32

I am just following all the examples available by searching on web and am not able to really understand it properly I guess. It would be great if someone can help me on this and provide me suggestion to correct the problem I might be doing.

Thanks & Regards, Keya

+1  A: 

You could try something like the following:

public void zipText(String text) throws Exception {
    FileOutputStream outstr = new FileOutputStream(new File("someFile"));
    ZipOutputStream zipOut = new ZipOutputStream(outstr);
    zipOut.setLevel(ZipOutputStream.STORED);
    ZipEntry entry = new ZipEntry("someEntry");
    zipOut.putNextEntry(entry);
    zipOut.write(text.getBytes("some format"), 0, text.getBytes("some format").length);
    zipOut.closeEntry();
    zipOut.close();
}

Note that the ZipEntry is what creates a new file (entry) in the zip file you are creating.

aperkins
Check out http://java.sun.com/developer/technicalArticles/Programming/compression/ for some more examples
Curtis Tasker
A: 

If you're using linux and do not want any compression, why don't you use tar instead of zip ?

Commons Compress gives you an easy way to work with Tar archives.

Valentin Rocher
I need to use specifically zip and moreover it needs to run on Windows as well. Do you have some idea about the issues that I have reported in the earlier comment? Thanks.