如何使用Python创建一个包含空目录的文件路径压缩包?

8
我一直在尝试使用 zipfileshutil.make_archive 模块递归地创建一个包含目录的 zip 文件。这两个模块都很好用——但是空目录不会被添加到归档文件中。同时,包含其他空目录的空目录也会被悄无声息地跳过。
我可以使用 7Zip 来创建相同路径的存档文件,并且空目录会得到保留。因此我知道这在文件格式本身上是可能的。只是我不知道如何在 Python 中实现。有什么建议吗?谢谢!

负责的代码在此处:[http://hg.python.org/cpython/file/8f1a8e80f330/Lib/shutil.py#l452]。 - icktoofay
尝试在目录中添加一个虚拟文件,并在之后从归档中删除该文件,以查看zipfile是否支持空目录(尽管格式支持,但某些zip实现可能不支持)。 - Thomas
4个回答

13

这是一个使用zipfile的示例:

import os, zipfile  
from os.path import join  
def zipfolder(foldername, filename, includeEmptyDIr=True):   
    empty_dirs = []  
    zip = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED)  
    for root, dirs, files in os.walk(foldername):  
        empty_dirs.extend([dir for dir in dirs if os.listdir(join(root, dir)) == []])  
        for name in files:  
            zip.write(join(root ,name))  
        if includeEmptyDIr:  
            for dir in empty_dirs:  
                zif = zipfile.ZipInfo(join(root, dir) + "/")  
                zip.writestr(zif, "")  
        empty_dirs = []  
    zip.close() 

if __name__ == "__main__":
    zipfolder('test1/noname/', 'zip.zip')

3
你的代码将绝对路径保存在归档文件中。我稍微修改了一下,现在它保存相对路径(因为它将在另一台机器上解压缩),并且运行得很好!感谢你的帮助! - jamieb
是的,我也做了那个,但那很容易。 我不得不将一个项目回溯到2.6,所以我不得不替换shutil.make_archive()调用。 - Florian Lagg
已在Python 2.7.3下测试,它不会压缩空目录,但会压缩其他所有内容。此外,“empty_dirs.extend([dir for dir in dirs if os.listdir(join(root, dir)) == []])”发生的次数太多了——它只需要为每个根目录触发一次,而不是每个根目录中的每个目录(因为空目录将被遍历)。 - James
我认为将ZipFile对象命名为“ zip”是一种不好的做法,因为它会覆盖Python中内置的zip函数。 - ViggoTW

2
你需要注册一个新的存档格式来实现这个,因为默认的ZIP压缩程序不支持。看一下现有ZIP压缩程序的核心代码。创建一个自己的压缩程序,使用当前未使用的dirpath变量来创建目录。我找到了如何创建空目录的方法,参见this
zip.writestr(zipfile.ZipInfo('empty/'), '')

有了这个,你应该能够编写必要的代码来归档空目录。

1
这是从使用Python将文件夹添加到zip文件中中提取的,但这是我尝试过的唯一有效的函数。作为答案列出的那个在Python 2.7.3下不起作用(不复制空目录且效率低下)。以下是经过验证的:
#!/usr/bin/python
import os
import zipfile

def zipdir(dirPath=None, zipFilePath=None, includeDirInZip=True):

    if not zipFilePath:
        zipFilePath = dirPath + ".zip"
    if not os.path.isdir(dirPath):
        raise OSError("dirPath argument must point to a directory. "
        "'%s' does not." % dirPath)
    parentDir, dirToZip = os.path.split(dirPath)
    #Little nested function to prepare the proper archive path
    def trimPath(path):
        archivePath = path.replace(parentDir, "", 1)
        if parentDir:
            archivePath = archivePath.replace(os.path.sep, "", 1)
        if not includeDirInZip:
            archivePath = archivePath.replace(dirToZip + os.path.sep, "", 1)
        return os.path.normcase(archivePath)

    outFile = zipfile.ZipFile(zipFilePath, "w",
        compression=zipfile.ZIP_DEFLATED)
    for (archiveDirPath, dirNames, fileNames) in os.walk(dirPath):
        for fileName in fileNames:
            filePath = os.path.join(archiveDirPath, fileName)
            outFile.write(filePath, trimPath(filePath))
        #Make sure we get empty directories as well
        if not fileNames and not dirNames:
            zipInfo = zipfile.ZipInfo(trimPath(archiveDirPath) + "/")
            #some web sites suggest doing
            #zipInfo.external_attr = 16
            #or
            #zipInfo.external_attr = 48
            #Here to allow for inserting an empty directory.  Still TBD/TODO.
            outFile.writestr(zipInfo, "")
    outFile.close()

16(或0x10)是目录的MS-DOS标志。您应该设置它,但也要设置UNIX标志。如果您使用ZipInfo.from_path,则它将为您处理。否则,一些程序将解压缩一个空文件而不是目录。 - OrangeDog

-1
def zip_dir(src_dir, dst_zip, *, skip_suffixes=None, dry=False):
    import logging
    from pathlib import Path
    from os import walk
    from tempfile import TemporaryDirectory
    from zipfile import ZipFile, ZipInfo

    _log = logging.getLogger(zip_dir.__name__)
    _log.addHandler(logging.NullHandler())
    _sep = 50 * "-"

    skip_suffixes = skip_suffixes or []
    src_dir, dst_zip = Path(src_dir), Path(dst_zip)
    _log.info("zipping dir: '%s' to: '%s", str(src_dir), str(dst_zip))

    if not src_dir.exists():
        raise FileNotFoundError(str(src_dir))
    if not src_dir.is_dir():
        raise NotADirectoryError(str(src_dir))
    if dst_zip.exists():
        raise FileExistsError(str(dst_zip))

    with TemporaryDirectory() as tmp_dir:
        tmp_zip_path = Path(tmp_dir).joinpath(dst_zip.name)

        with ZipFile(str(tmp_zip_path), mode="w") as zip_out:
            for root, dirs, files in walk(src_dir):
                root = Path(root)

                for folder in dirs:
                    folder = root.joinpath(folder)

                    # add empty folders to the zip
                    if not list(folder.iterdir()):
                        _log.debug(_sep)
                        folder_name = f"{str(folder.relative_to(src_dir))}/"
                        _log.debug("empty dir: '%s'", folder_name)

                        if dry:
                            continue

                        zip_out.writestr(ZipInfo(folder_name), "")

                for file in files:
                    file = root.joinpath(file)
                    _log.debug(_sep)
                    _log.debug("adding:  '%s'", str(file))

                    should_skip = None
                    for suffix in file.suffixes:
                        if suffix in skip_suffixes:
                            should_skip = suffix
                            break

                    if should_skip:
                        _log.debug("skipped [%s]: %s", should_skip, str(file))
                        continue

                    arcname = str(file.relative_to(src_dir))
                    _log.debug("arcname: '%s'", arcname)

                    if dry:
                        continue

                    zip_out.write(str(file), arcname=arcname)

        if not dry:
            dst_zip.write_bytes(tmp_zip_path.read_bytes())

        tmp_zip_path.unlink()

if __name__ == '__main__':
    import logging
    logging.basicConfig(level=logging.DEBUG, format="%(asctime)s | %(levelname)8s | %(module)25s:%(lineno)-5s | %(message)s")

    zip_dir("/tmp/opera_profile", "opera_profile.zip", skip_suffixes=[".log"], dry=True)

你能解释一下这是做什么的,以及为什么它有用吗? - ggorlen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接