Index: zipfile.py =================================================================== --- zipfile.py (revision 82467) +++ zipfile.py (working copy) @@ -622,7 +622,7 @@ class ZipFile: - """ Class with methods to open, read, write, close, list zip files. + """ Class with methods to open, read, write, remove, close, list zip files. z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=False) @@ -689,6 +689,7 @@ self._RealGetContents() # seek to start of directory and overwrite self.fp.seek(self.start_dir, 0) + self.fp.truncate() except BadZipfile: # file is not a zip file, just append self.fp.seek(0, 2) else: @@ -1106,6 +1107,31 @@ self.filelist.append(zinfo) self.NameToInfo[zinfo.filename] = zinfo + def remove(self, member): + """Remove a member from the archive.""" + + zinfo = self.getinfo(member) + + # compute the location of the file data in the local file header, + # by adding the lengths of the records before it + zlen = len(zinfo.FileHeader()) + zinfo.compress_size + fileidx = self.filelist.index(zinfo) + fileofs = sum( + [len(self.filelist[f].FileHeader()) + self.filelist[f].compress_size + for f in xrange(0, fileidx)] + ) + + self.fp.seek(fileofs + zlen) + after = self.fp.read() + self.fp.seek(fileofs) + self.fp.write(after) + self.fp.seek(-zlen, 2) + self.fp.truncate() + + self._didModify = True + self.filelist.remove(zinfo) + del self.NameToInfo[member] + def __del__(self): """Call the "close()" method in case the user forgot.""" self.close()