Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Zip file: how to replace entry? Is that possible?

Thread view: 
josh - 21 Feb 2007 22:07 GMT
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?
Andrew Thompson - 21 Feb 2007 22:27 GMT
On Feb 22, 9:07 am, josh <pljosh_malpa_poczta.neostrada...@nospam.org>
wrote:
..
> I am wondering is it possible, using Java API, to get some ZIP file and
> delete/add/replace some files that are inside it.

<http://groups.google.com/groups?
selm=44cf1d68$0$636$bed64819@news.gradwell.net>

Follow the link to  update jar file with java code'
for further discussion.

Andrew T.
RedGrittyBrick - 21 Feb 2007 23:27 GMT
> Hello there,
> after huge amount of time with Google, I decided to ask you for help.
[quoted text clipped - 6 lines]
>
> 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.
Arne Vajhøj - 22 Feb 2007 02:13 GMT
> AFAIK Current operating systems don't provide any mechanism for
> inserting data into, or deleting data from, the middle of a sequential
[quoted text clipped - 13 lines]
>
> A zip archive is a sequential file, not a database file.

A zip file is *not* a sequential file in the meaning of the word
you use here.

The zip format does have the concept of indexes and length.

And as a consequence the content of a zip file can be updated.

Arne
Chris Uppal - 22 Feb 2007 12:56 GMT
> > A zip archive is a sequential file, not a database file.
>
[quoted text clipped - 4 lines]
>
> And as a consequence the content of a zip file can be updated.

I'd say you are both about 2/3 right.  Unfortunately, the bottom line is that
RedGrittyBrick's conclusion is correct -- you cannot update a ZIP file without
re-writing everything from the point at which you make the change up to the end
of the file.

(I don't think any sensible ZIP API would offer even that much flexibility --
better to keep it simple and only allow writing from the very start of the
file, or appendfing to the very end of the (logical) file.  My own ZIP stuff
(not Java) allows both, but unless something has changed in 1.6/1.7[*], Java's
built-in ZIP handling doesn't support appending to files.)

The format of a ZIP is first a sequence of entries of variable sizes, followed
by an index to allow random access reading of the file (in case that should be
needed).  So you can't change anything in the middle of the file without
rewriting that entry and everything that follows -- just like lines in text
files.  But there /is/ an index, and fast random access is supported, but it's
not the kind of ISAM-like index which would allow in-place modification of the
file.

   -- chris

[*] I think there are changes in 1.6, but haven't looked for them yet, let
alone looked /at/ them ;-)
josh - 22 Feb 2007 02:03 GMT
Thanks for help.
I did it as Chris Uppal suggested (in the link Andrew wrote).

I am providing an attachment with source code if anyone's interested in
my implementation. It is saving entire ODF (OpenDocumentFormat is a ZIP)
from source file, excluding content.xml which is replaced by altered
version.

Thanks again,
W.Sz.
Witold Szczerba - 22 Feb 2007 17:12 GMT
A slightly better version in attachment.
I hope that will help someone eventually :)

W.Sz.
Andrew Thompson - 22 Feb 2007 18:06 GMT
On Feb 23, 4:12 am, Witold Szczerba <josh.nos...@poczta.neostrada.pl>
wrote:
> A slightly better version in attachment.

Please note that this group was not intended
for messages with attachments*.  As such, many
news servers will strip them.

* As far as I understand, in any case.

If you have some code worth sharing, please
simply post it in the body of a message,
perhaps delimited by..

<code>
</code>

..or..

<sscce>
</sscce>

etc.

> I hope that will help someone eventually :)

I was about to repost the code, then
realised it would probably be wrapped
and broken by Google groups.  I would
recommend to reformat code so that any
line extends no more than around 63 chars,
to ensure it is safe from (immediate,
before being quoted) line wrap.

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.