'File update : multiple versions stored inside the ZIP archive

Let's say we have a test.zip file and we update a file:

zfh = zipfile.ZipFile("test.zip", mode = "a")
zfh.write("/home/msala/test.txt")
zfh.close()

Repeating a few times this "update", using the builtin method printdir() I see in the archive there are stored not only the last one "test.txt" but also all the previous copies of the file.

Ok, I understand the zipfile library hasn't a delete method.

Questions:

  • if I call the builtin method extract("/home/msala/test.txt"), which copy of the file is extracted and written to the file system ?
  • inside the zip archive, is there any flag telling that old copies .. are old copies, superseded by the last one ?

At the moment I list all the stored files and sort them by filename, last modification time...



Solution 1:[1]

The tl;dr is no, you can't do this without building a bit of extra info—but that can be done without sorting, and, even if you did have to sort, the performance cost would be irrelevant.


First, let me explain how zipfiles work. (Even if you understand this, later readers with the same problem may not.)

Unfortunately, the specification is a copyrighted and paywalled ISO document, so I can't link to it or quote it. The original PKZip APPNOTE.TXT which is the de facto pro-standardization standard is available, however. And numerous sites like Wikipedia have nice summaries.

A zipfile is 0 or more fragments, followed by a central directory.

Fragments are just treated as if they were all concatenated into one big file.

The body of the file can contain zip entries, in arbitrary order, along with anything you want. (This is how DOS/Windows self-extracting archives work—the unzip executable comes at the start of the first fragment.) Anything that looks like a zip entry, but isn't referenced by the central directory, is not treated as a zip entry (except when repairing a corrupted zipfile.)

Each zip entries starts with a header that gives you the filename, compression format, etc. of the following data.

The directory is a list of directory entries that contain most of the same information, plus a pointer to where to find the zip entry.

It's the order of directory entries that determines the order of the files in the archive.


if I call the builtin method extract("/home/msala/test.txt"), which copy of the file is extracted and written to the file system ?

The behavior isn't really specified anywhere.

Extracting the whole archive should extract both files, in the order present in the zip directory (the same order given by infolist), with the second one overwriting the first.

But extracting by name doesn't have to give you both—it could give you the last one, or the first, or pick one at random.

Python gives you the last. The way this works is that, when reading the directory, it builds a dict mapping filenames to ZipInfos, just adding them as encountered, so the last one will overwrite the previous ones. (Here's the 3.7 code.) Whenever you try to access something by filename, it just looks up the filename in that dict to get the ZipInfo.

But is that something you want to rely on? I'm not sure. On the one hand, this behavior has been the same from Python 1.6 to 3.7, which is usually a good sign that it's not going to change, even if it's never been documented. On the other hand, there are open issues—including #6818, which is intended to add deletion support to the library one way or another—that could change it.


And it's really not that hard to do the same thing yourself. With the added benefit that you can use a different rule—always keep the first, always keep the one with the latest mod time, etc.

You seem to be worried about the performance cost of sorting the infolist, which is probably not worth worrying about. The time it takes to read and parse the zip directory is going to make the cost of your sort virtually invisible.

But you don't really need to sort here. After all, you don't want to be able to get all of the entries with a given name in some order, you just want to get one particular entry for each name. So, you can just do what ZipFile does internally, which takes only linear time to build, and constant time each time you search it. And you can use any rule you want here.

entries = {}
for entry in zfh.infolist():
    if entry.filename not in entries:
        entries[entry.filename] = entries

This keeps the first entry for any name. If you want to keep the last, just remove the if. If you want to keep the latest by modtime, just change it if entry.date_time > entries[entry.filename].date_time:. And so on.

Now, instead of relying on what happens when you call extract("home/msala/test.txt"), you can call extract(entries["home/msala/test.txt"]) and know that you're getting the first/last/latest/whatever file of that name.


inside the zip archive, is there any flag telling that old copies .. are old copies, superseded by the last one ?

No, not really.

The way to delete a file is to remove it from the central directory. Which you do just by rewriting the central directory. Since it comes at the end of the zipfile, and is almost always more than small enough to fit on even the smallest floppy, this was generally considered fine even back in the DOS days.

(But notice that if you unplug the computer in the middle of it, you've got a zipfile without a central directory, which has to be rebuilt by scanning all of the file entries. So, many newer tools will instead, at least for smaller files, rewrite the whole file to a tempfile then rename it over the original, to guarantee a safe, atomic write.)

At least some early tools would sometimes, especially for gigantic archives, rewrite the entry's pathname's first byte with a NUL. But this doesn't really mark the entry as deleted, it just renames it to "\0ome/msala/test.txt". And many modern tools will in fact treat it as meaning exactly that and give you weird errors telling you they can't find a directory named 'ome' or '' or something else fun. Plus, this means the filename in the directory entry no longer matches the filename in the file entry header, which will cause many modern tools to flag the zipfile as corrupted.

At any rate, Python's zipfile module doesn't do either of these, so you'd need to subclass ZipFile to add the support yourself.

Solution 2:[2]

I solved this way, similar to database records management.

Adding a file to the archive, I look for previous stored copies (same filename). For each of them, I set their field "comment" to a specific marker, for example "deleted".

We add the new file, with comment = empty.

As we like, we can "vacuum": shrink the zip archive using the usually tools (under the hood a new archive is created, discarding the files having the comment set to "deleted").

This way, we have also a simple "versioning". We have all the previous files copies, until the vacuum.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2