如何在Unix命令行中递归解压目录及其子目录中的归档文件?

98
unzip命令没有递归解压存档文件的选项。
如果我有以下目录结构和存档文件:
/Mother/Loving.zip
/Scurvy/Sea Dogs.zip
/Scurvy/Cures/Limes.zip
并且我想将所有存档文件解压缩到与每个存档文件名称相同的目录中:
/Mother/Loving/1.txt
/Mother/Loving.zip
/Scurvy/Sea Dogs/2.txt
/Scurvy/Sea Dogs.zip
/Scurvy/Cures/Limes/3.txt
/Scurvy/Cures/Limes.zip
我要发出哪些命令?
重要的是,这不能因文件名中包含空格而中断。

长文件路径超过260个字符怎么办?请参考:https://superuser.com/a/1263234/439537 - Andrew
11个回答

-3

这对我有效

def unzip(zip_file, path_to_extract):
    """
    Decompress zip archives recursively
    Args:
        zip_file: name of zip archive
        path_to_extract: folder where the files will be extracted
    """
    try:
        if is_zipfile(zip_file):
            parent_file = ZipFile(zip_file)
            parent_file.extractall(path_to_extract)
            for file_inside in parent_file.namelist():
                if is_zipfile(os.path.join(os.getcwd(),file_inside)):
                    unzip(file_inside,path_to_extract)
            os.remove(f"{zip_file}")
    except Exception as e:
        print(e)

1
即使这个问题既没有标记为 [tag:shell] 也没有标记为 [tag:python],我仍然不明白为什么一个 Python 答案会受到欢迎。如果它像问题中所示一样下降目录树而不是解压缩压缩的 zip 文件,那么它可能会变得有用。 - greybeard

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