Skip to main content
 首页 » 编程设计

Python 从压缩文件中删除条目

2025年04月02日13dudu

我目前正在为容器格式编写一个开源库,其中涉及修改 zip 存档。因此我使用了 pythons 内置的 zipfile 模块。由于某些限制,我决定修改模块并将其与我的库一起发布。这些修改包括一个补丁,用于从 python 问题跟踪器的 zip 文件中删除条目:https://bugs.python.org/issue6818 更具体地说,我包含了来自 ubershmekel 的 zipfile.remove.2.patch。 在对 Python-2.7 进行一些修改后,根据随附的单元测试,补丁工作正常。

但是,在删除、添加和删除 + 添加文件而不关闭其间的 zip 文件时,我遇到了一些问题。

Error 
Traceback (most recent call last): 
  File "/home/martin/git/pyCombineArchive/tests/test_zipfile.py", line 1590, in test_delete_add_no_close 
    self.assertEqual(zf.read(fname), data) 
  File "/home/martin/git/pyCombineArchive/combinearchive/custom_zip.py", line 948, in read 
    with self.open(name, "r", pwd) as fp: 
  File "/home/martin/git/pyCombineArchive/combinearchive/custom_zip.py", line 1003, in open 
    % (zinfo.orig_filename, fname)) 
BadZipFile: File name in directory 'foo.txt' and header 'bar.txt' differ. 

意思是 zip 文件没问题,但中央字典/条目标题以某种方式弄乱了。 此单元测试重现此错误:

def test_delete_add_no_close(self): 
    fname_list = ["foo.txt", "bar.txt", "blu.bla", "sup.bro", "rollah"] 
    data_list = [''.join([chr(randint(0, 255)) for i in range(100)]) for i in range(len(fname_list))] 
 
    # add some files to the zip 
    with zipfile.ZipFile(TESTFN, "w") as zf: 
        for fname, data in zip(fname_list, data_list): 
            zf.writestr(fname, data) 
 
    for no in range(0, 2): 
        with zipfile.ZipFile(TESTFN, "a") as zf: 
            zf.remove(fname_list[no]) 
            zf.writestr(fname_list[no], data_list[no]) 
            zf.remove(fname_list[no+1]) 
            zf.writestr(fname_list[no+1], data_list[no+1]) 
 
            # try to access prior deleted/added file and prior last file (which got moved, while delete) 
            for fname, data in zip(fname_list, data_list): 
                self.assertEqual(zf.read(fname), data) 

我修改后的 zipfile 模块和完整的单元测试文件可以在这个要点中找到:https://gist.github.com/FreakyBytes/30a6f9866154d82f1c3863f2e4969cc4

请您参考如下方法:

经过一些深入的调试,我很确定移动剩余 block 时出了问题。 (那些在删除文件之后存储的)所以我继续重写了这个代码部分,所以它一次复制这些文件/ block 。我还重写了每个文件的文件头(以确保它有效)和 zip 文件末尾的中央目录。 我的删除函数现在看起来像这样:

def remove(self, member): 
    """Remove a file from the archive. Only works if the ZipFile was opened 
    with mode 'a'.""" 
 
    if "a" not in self.mode: 
        raise RuntimeError('remove() requires mode "a"') 
    if not self.fp: 
        raise RuntimeError( 
              "Attempt to modify ZIP archive that was already closed") 
    fp = self.fp 
 
    # Make sure we have an info object 
    if isinstance(member, ZipInfo): 
        # 'member' is already an info object 
        zinfo = member 
    else: 
        # Get info object for member 
        zinfo = self.getinfo(member) 
 
    # start at the pos of the first member (smallest offset) 
    position = min([info.header_offset for info in self.filelist])  # start at the beginning of first file 
    for info in self.filelist: 
        fileheader = info.FileHeader() 
        # is member after delete one? 
        if info.header_offset > zinfo.header_offset and info != zinfo: 
            # rewrite FileHeader and copy compressed data 
            # Skip the file header: 
            fp.seek(info.header_offset) 
            fheader = fp.read(sizeFileHeader) 
            if fheader[0:4] != stringFileHeader: 
                raise BadZipFile("Bad magic number for file header") 
 
            fheader = struct.unpack(structFileHeader, fheader) 
            fname = fp.read(fheader[_FH_FILENAME_LENGTH]) 
            if fheader[_FH_EXTRA_FIELD_LENGTH]: 
                fp.read(fheader[_FH_EXTRA_FIELD_LENGTH]) 
 
            if zinfo.flag_bits & 0x800: 
                # UTF-8 filename 
                fname_str = fname.decode("utf-8") 
            else: 
                fname_str = fname.decode("cp437") 
 
            if fname_str != info.orig_filename: 
                if not self._filePassed: 
                    fp.close() 
                raise BadZipFile( 
                      'File name in directory %r and header %r differ.' 
                      % (zinfo.orig_filename, fname)) 
 
            # read the actual data 
            data = fp.read(fheader[_FH_COMPRESSED_SIZE]) 
 
            # modify info obj 
            info.header_offset = position 
            # jump to new position 
            fp.seek(info.header_offset, 0) 
            # write fileheader and data 
            fp.write(fileheader) 
            fp.write(data) 
            if zinfo.flag_bits & _FHF_HAS_DATA_DESCRIPTOR: 
                # Write CRC and file sizes after the file data 
                fp.write(struct.pack("<LLL", info.CRC, info.compress_size, 
                        info.file_size)) 
            # update position 
            fp.flush() 
            position = fp.tell() 
 
        elif info != zinfo: 
            # move to next position 
            position = position + info.compress_size + len(fileheader) + self._get_data_descriptor_size(info) 
 
    # Fix class members with state 
    self.start_dir = position 
    self._didModify = True 
    self.filelist.remove(zinfo) 
    del self.NameToInfo[zinfo.filename] 
 
    # write new central directory (includes truncate) 
    fp.seek(position, 0) 
    self._write_central_dir() 
    fp.seek(self.start_dir, 0)  # jump to the beginning of the central directory, so it gets overridden at close() 

您可以在最新版本的要点中找到完整的代码:https://gist.github.com/FreakyBytes/30a6f9866154d82f1c3863f2e4969cc4

或者在我写的库的 repo 协议(protocol)中:https://github.com/FreakyBytes/pyCombineArchive