且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Python 中将多个 zip 文件合并为一个 zip 文件

更新时间:2023-11-23 11:26:34

这是我能想到的最短版本:

>>>将 zipfile 导入为 z>>>z1 = z.ZipFile('z1.zip', 'a')>>>z2 = z.ZipFile('z2.zip', 'r')>>>z1.namelist()['a.xml', 'b.xml']>>>z2.namelist()['c.xml', 'd.xml']>>>[z1.writestr(t[0], t[1].read()) for t in ((n, z2.open(n)) for n in z2.namelist())][无,无]>>>z1.namelist()['a.xml', 'b.xml', 'c.xml', 'd.xml']>>>z1.close()

不测试替代方案,对我来说这是***的(可能也是最明显的!)解决方案,因为 - 假设两个 zip 文件包含相同数量的数据,这种方法只需要解压和重新压缩一半(1 个文件).

PS:列表理解只是为了将指令保留在控制台的一行上(这可以加快调试速度).考虑到结果列表没有任何用途......

HTH!

I have multiple zip files that have the same structure -- they contain XML files at the root level. All files in each zip file are unique (no duplicates across the zip files). I need to combine all of the XML files from all of the zip files into a single zip file (with the same structure as the original zip files). Suggestions for how to best go about doing this? Thanks.

This is the shortest version I could come up with:

>>> import zipfile as z
>>> z1 = z.ZipFile('z1.zip', 'a')
>>> z2 = z.ZipFile('z2.zip', 'r')
>>> z1.namelist()
['a.xml', 'b.xml']
>>> z2.namelist()
['c.xml', 'd.xml']
>>> [z1.writestr(t[0], t[1].read()) for t in ((n, z2.open(n)) for n in z2.namelist())]
[None, None]
>>> z1.namelist()
['a.xml', 'b.xml', 'c.xml', 'd.xml']
>>> z1.close()

Without testing the alternative, to me this is the best (and probably most obvious too!) solution because - assuming both zip files contains the same amount of data, this method requires the decompression and re-compression of only half of it (1 file).

PS: List comprehension is there just to keep instructions on one line in the console (which speeds debugging up). Good pythonic code would require a proper for loop, given that the resulting list serves no purpose...

HTH!