josh said:
Hello there,
after huge amount of time with Google, I decided to ask you for help.
I am wondering is it possible, using Java API, to get some ZIP file and
delete/add/replace some files that are inside it. Everywhere they
provide examples how to create new ZIP or how to read one, using
ZipFile, ZipEntry, Zip[Input/Output]Stream, etc... but how to alter
existing archive
Do you have any ideas?
Here's what occurs to me on reading your question ...
AFAIK Current operating systems don't provide any mechanism for
inserting data into, or deleting data from, the middle of a sequential
file. This applies whether it is a text file, a ZIP archive or any other
type of sequential file. You have to read the whole current file and
write a new file.
You can then delete old; rename new old; to maintain the illusion that
you have inserted data into the middle of a file.
Obviously files used by databases are not treated as sequential files.
To insert data into the "middle" of a table held in a file I'd expect a
DBMS to overwite one or more blocks and update an index. The data might
be written to an "empty" block or appended to the end of the file. The
database index is what makes the new data seem to be in the middle of
the table (when ordered by an indexed column).
A zip archive is a sequential file, not a database file.
None of the above is specific to Java.
So the answer to your question is to read the existing archive, stash
the extracted files in memory or to temporary disk space then write a
new zip archive including new items or skipping some items as needed.
You can obviously read and write in parallel to reduce usage of
temporary storage (memory or disk)
It's conceivable that some Java API provides a
zipArchive.insertFile(...) method that does all the above unseen in the
background. I'd have expected you to have found it, if it existed.
Just my $0.02 worth.