[Python-ideas] Add a .pop() method to ZipFile

Daniel Holth dholth at gmail.com
Fri May 10 15:35:47 CEST 2013


On Fri, May 10, 2013 at 9:22 AM, Masklinn <masklinn at masklinn.net> wrote:
> On 2013-05-10, at 15:01 , Antoine Pitrou wrote:
>
>> Le Fri, 10 May 2013 08:32:17 -0400,
>> Daniel Holth <dholth at gmail.com> a écrit :
>>> Check out this efficient way to remove the last file from any
>>> ordinary zip file.
>>
>> But why would you care such an operation? A generic remove() operation
>> would sound more useful.
>
> Guessing it's because a generic `remove()` is less trivial to implement,
> which would also be why the suggestion is for .pop() but not
> .pop([index]): removing files at the end means you can just truncate the
> archive and append the central directory[0], removing files in the middle
> means either zeroing and leaving a hole or moving all following files.

Yes, only the last file can be removed efficiently.

> Although technically it's only simple if you assume file content and
> central directory entries are in the same order, which is unwarranted.

The implementation assumes several things that could be false, but are
usually true. A sort and several other checks would be warranted.

> So, guessing it's because you can do a half-assed job at implementing
> pop(), it will usually work (and will silently corrupt your archive
> when it does not)
>
> [0] you may also need to rewrite all offsets in the central directory, I
> don't remember if they are offset from file start or central directory
> record start.

_didModify = True takes care of rewriting the central directory.



More information about the Python-ideas mailing list