Problem with Zip Files

Hi there,
I have a problem with ZipFiles.
In my programm I can create and read the Zipfiles and add files to it, when they are newly created. But when I open a zipfile and wanna append some files, it always overwrite it from the beginning. How have I to open and read the zipfile, that the outputstream will append at the end where the inputstream lasts?

Benny

It’s not a problem with Zip, it is a problem with the way you open the file. You might be doing this:


FileOutputStream out = new FileOutputStream("myfile.zip");
ZipOutputStream zip = new ZipOutputStream(out);

What you need is:


FileOutputStream out = new FileOutputStream("myfile.zip", true);
ZipOutputStream zip = new ZipOutputStream(out);

Note that the bytes you append to the file will not be part of the zip-file structure.

[quote]But when I open a zipfile and wanna append some files
[/quote]
You cannot append files that way to the zip. You have to rewrite the entire file.

You cannot append files that way to the zip. You have to rewrite the entire file.
[/quote]
I never tried it before. I just thought you could append if you opened the stream with append enabled.

I never tried it before. I just thought you could append if you opened the stream with append enabled.
[/quote]
Zip files are an on-disc indexed data structure. Thats how the Zip program can tell a damaged one (sometimes.) You cant just dorp stuff at the end.

err, ok…when i understand all correct…i have to open the zip file and extract all entries to a tmp directory and then creating a new zipfile that will hold the new and old entries. Uff, i won’t mention the loading bars the user see…

But who does the Win32 Version of Zip do appending? There is just no speeddecrease while appending files to the archive. You open and can add them… Confused…

Benny

The files in a zip archive are individually compressed. Therefore Winzip (say) only needs to open the archive and extract the files as binary blobs. When new files are added, they are compressed into blobs as part of the adding process. When you close the archive, the directory structure is regenerated and it and the blobs are written to the disc. Thus why you do need to rewrite the entire archive, you don’t need to decompress/recompress the files already in it.

So… the question is (and I don’t know the answer) - Can you open a zip archive in java & add stuff to it, without decompressing/recompressing the existing files.

You mean it should be enough to create for each ZipEntry in the file a corresponding ZipEntry that will be saved back to the new archive with the new entries?

I’ ll give all hints a try :slight_smile:

You just can’t do it with java.util.zip.*

You would have to write your own Zip-loader that gives you the off->len for each entry in the binary zip-file. This might be quite some work, but when you achieved that, you can copy the compressed data, instead of decompress/compress. And you would have to recreate the directory-structure. Maybe there are some 3rd party APIs what will save you all this work.

Good luck.

Ok, that seems to be some more work that i wanted in first place. I will look through the web for such a 3rd API or just create a simple package with each containing file compressed with GZIP as alternative. That way i woul be able to append at the end.

thank for the tips :slight_smile: