Add file to zip, or replace file in zip

Discussion in 'Python' started by abcd, Apr 28, 2006.

  1. abcd

    abcd Guest

    I have a script which zips up a directory, once it does with that
    (before it closes the zip file) I want to replace a file that was added
    to the zip, say "Foo.txt".

    So I tried this...
    Code:
    z = zipfile.ZipFile("blah.zip", "w")
    # zip the directory
    #...
    z.writestr("c:\\path\\to\\current\\foo_txt_file\\Foo.txt", "some new
    data")
    z.close()
    
    All this does is add a new Foo.txt file in the zip alongside the
    existing one....any suggestions?
    
    Thanks
    abcd, Apr 28, 2006
    #1
    1. Advertising

  2. abcd

    Roger Miller Guest

    First note that zipfile is a plain Python module, so reading
    Python.../Lib/zipfile.py will reveal all its secrets.

    I don't think it is possible to replace archive members using the
    module. You could copy all the files into a new zip file, replacing the
    ones you want to change as you go. But it might be easier just to use
    os.system() or something similar to run an external zip program.
    Roger Miller, Apr 28, 2006
    #2
    1. Advertising

  3. Roger Miller wrote:
    > First note that zipfile is a plain Python module, so reading
    > Python.../Lib/zipfile.py will reveal all its secrets.
    >
    > I don't think it is possible to replace archive members using the
    > module. You could copy all the files into a new zip file, replacing the
    > ones you want to change as you go. But it might be easier just to use
    > os.system() or something similar to run an external zip program.
    >

    It is not currently possible to delete or replace individual elements
    in a zip file, nor (in all likelihood) would you want to do so. It
    would require having the zip[ file in a broken state for some time
    as you copy data from one area of the zip to another. Any error
    (or raised exception like Control-C) during this process is likely
    to leave you with an inconsistent and therefore unreadable zip file.
    The typical technique is to mark some files "deleted" in the directory,
    add new files (possibly the replacements for the deleted files), and,
    in one pass, copy all non-deleted files to a new zip (which you can then
    swap for the original zip). Shortcutting this process puts all data in
    your zip file at risk.

    --Scott David Daniels
    Scott David Daniels, Apr 30, 2006
    #3
  4. Scott David Daniels wrote:
    > It is not currently possible to delete or replace individual elements
    > in a zip file, nor (in all likelihood) would you want to do so. It
    > would require having the zip[ file in a broken state for some time
    > as you copy data from one area of the zip to another.


    If zips just used sequential access like tar files, you could append newer
    versions without a problem. You windows kids and your crazy data formats.

    > Any error
    > (or raised exception like Control-C) during this process is likely
    > to leave you with an inconsistent and therefore unreadable zip file.


    Isn't that true of any modifications to the zip archive, e.g. appending a
    new file rather than replacing an existing one?

    > in one pass, copy all non-deleted files to a new zip (which you can then
    > swap for the original zip). Shortcutting this process puts all data in
    > your zip file at risk.


    Again, isn't this true of any substantive change to any file whatsoever?
    Errors during write can always leave your data in an inconsistent state,
    unless your data uses a structured append format like journaled
    filesystems. That seems like an orthogonal issue to replacing a file in
    the archive.
    Edward Elliott, Apr 30, 2006
    #4
  5. Edward Elliott wrote:
    > Scott David Daniels wrote:
    >>...

    > ... You windows kids and your crazy data formats.

    There were a few oth OS's than Linux and Windows. Maybe you
    should call me "you crazy Tenex kid." Knuth says, "the fastest
    way to search is to know where to go." -- Zips have locations of
    files, and you needn't read in a lot of a huge zip to find and
    extract a couple of files.

    >> Any error
    >> (or raised exception like Control-C) during this process is likely
    >> to leave you with an inconsistent and therefore unreadable zip file.

    >
    > Isn't that true of any modifications to the zip archive, e.g. appending a
    > new file rather than replacing an existing one?


    Nope. There is enough info in the zip to rebuild the directory
    with a forward scan of the zip. (Each entry has a file descr).
    "appending" is really replacing backing up before the zip archive
    directory and writing another entry, followed by a new directory.

    >> in one pass, copy all non-deleted files to a new zip (which you can then
    >> swap for the original zip). Shortcutting this process puts all data in
    >> your zip file at risk.

    >
    > Again, isn't this true of any substantive change to any file whatsoever?
    > Errors during write can always leave your data in an inconsistent state,
    > unless your data uses a structured append format like journaled
    > filesystems. That seems like an orthogonal issue to replacing a file in
    > the archive.


    --Scott David Daniels
    Scott David Daniels, May 1, 2006
    #5
  6. Scott David Daniels wrote:

    > Edward Elliott wrote:
    >> Scott David Daniels wrote:
    >>>...

    > > ... You windows kids and your crazy data formats.

    > There were a few oth OS's than Linux and Windows. Maybe you
    > should call me "you crazy Tenex kid."


    Windows popularized the zip format, but if you insist:
    You crazy Tenex kids, with your char-at-a-time raw password checking and
    new-fangled virtual memory. Back in my day, all we had was Multics and we
    liked it that way.

    Despite having no memory prior to the Apple II, I do know a bit of history.


    > Knuth says, "the fastest
    > way to search is to know where to go." -- Zips have locations of
    > files, and you needn't read in a lot of a huge zip to find and
    > extract a couple of files.


    You whippersnappers and your random access, always in such a hurry to get
    your data. You want faster search, just wait a few machine generations.
    Edward Elliott, May 1, 2006
    #6
  7. Edward Elliott wrote:
    > Scott David Daniels wrote:
    >
    >> Edward Elliott wrote:
    >>> Scott David Daniels wrote:
    >>>> ...
    >> > ... You windows kids and your crazy data formats.

    >> There were a few oth OS's than Linux and Windows. Maybe you
    >> should call me "you crazy Tenex kid."

    >
    > Windows popularized the zip format, but if you insist:
    > You crazy Tenex kids, with your char-at-a-time raw password checking and
    > new-fangled virtual memory. Back in my day, all we had was Multics and we
    > liked it that way.


    Actually I think it was a combination of CP/M and DOS that popularized
    the ZIP format; essentially the floppy-disk set, for whom the zip format
    was a godsend.

    --
    -Scott David Daniels
    Scott David Daniels, May 1, 2006
    #7
  8. Scott David Daniels wrote:
    > Actually I think it was a combination of CP/M and DOS that popularized
    > the ZIP format; essentially the floppy-disk set, for whom the zip format
    > was a godsend.


    Ah you're right. I just lump all Microsoft OSes under the term 'Windows'
    now, though I suppose that's unfair to Xenix.
    Edward Elliott, May 1, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Bodden

    Add or replace in ZIP file

    Eric Bodden, Feb 10, 2004, in forum: Java
    Replies:
    2
    Views:
    739
    Thomas Weidenfeller
    Feb 11, 2004
  2. Replies:
    0
    Views:
    1,691
  3. HARI OM
    Replies:
    0
    Views:
    17,378
    HARI OM
    May 3, 2005
  4. majj81
    Replies:
    0
    Views:
    318
    majj81
    May 31, 2006
  5. Chris Lambacher

    Re: Add file to zip, or replace file in zip

    Chris Lambacher, Jun 1, 2006, in forum: Python
    Replies:
    0
    Views:
    588
    Chris Lambacher
    Jun 1, 2006
Loading...

Share This Page