使用 PyLZMA 和 py7zlib 将文件夹递归压缩为 7z [英] Compress a folder recursively as 7z with PyLZMA and py7zlib

查看:37
本文介绍了使用 PyLZMA 和 py7zlib 将文件夹递归压缩为 7z的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过多次反复试验,我已经弄清楚如何通过 PyLZMA 制作 lzma 压缩文件,但我想将看似简单的任务复制到递归压缩文件夹及其所有文件/目录到 7z 文件.我只是通过 7z.exe 来完成它,但在它完成之前我似乎无法捕捉到进程的标准输出,我想要一些 per-7z 文件的进展,因为我将压缩数百个文件夹演出规模超过 1 TB.不幸的是,我无法提供我尝试过的任何代码,仅仅是因为我看到的使用 py7zlib 的唯一示例是从预先存在的文件中提取文件.有没有人对这两者的组合有任何运气或可以提供一些帮助?

Through much trial and error, I've figured out how to make lzma compressed files through PyLZMA, but I'd like to replicate the seemingly simple task of compressing a folder and all of its files/directories recursively to a 7z file. I would just do it through 7z.exe, but I can't seem to be able to catch the stdout of the process until it's finished and I would like some per-7z-file progress as I will be compressing folders that range from hundreds of gigs to over a terabyte in size. Unfortunately I can't provide any code that I have tried, simply because the only thing I've seen examples of using py7zlib are extracting files from pre-existing files. Has anybody had any luck with the combination of these two or could provide some help?

就其价值而言,这将是在使用 python 2.7 的 Windows 上.如果这里可能发生一些神奇的多线程,特别是考虑到 lzma 压缩看起来有多慢(但是,时间在这里不是问题),则加分.提前致谢!

For what it's worth, this would be on Windows using python 2.7. Bonus points if there some magic multi-threading that could occur here, especially given how slow lzma compression seems to be (time, however, is not an issue here). Thanks in advance!

推荐答案

纯 Python 替代方案是创建 .tar.xz 文件,并结合标准库 tarfile 模块和 liblzma 包装模块 pyliblzma.这将创建大小与 .7z 存档相当的文件:

A pure Python alternative is to create .tar.xz files with a combination of the standard library tarfile module and the liblzma wrapper module pyliblzma. This will create files comparable to in size to .7z archives:

import tarfile
import lzma

TAR_XZ_FILENAME = 'archive.tar.xz'
DIRECTORY_NAME = '/usr/share/doc/'

xz_file = lzma.LZMAFile(TAR_XZ_FILENAME, mode='w')

with tarfile.open(mode='w', fileobj=xz_file) as tar_xz_file:
    tar_xz_file.add(DIRECTORY_NAME)

xz_file.close()

棘手的部分是进度报告.上面的示例对 tarfile 的目录使用递归模式.TarFile 类,所以有 add 方法在添加整个目录之前不会返回.

The tricky part is the progress report. The example above uses the recursive mode for directories of the tarfile.TarFile class, so there the add method would not return until the whole directory was added.

以下问题讨论了监控 tar 文件创建进度的可能策略:

The following questions discuss possible strategies for monitoring the progress of the tar file creation:

这篇关于使用 PyLZMA 和 py7zlib 将文件夹递归压缩为 7z的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆