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

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

问题描述

通过大量的尝试和错误,我已经弄清楚如何通过PyLZMA制作lzma压缩文件,但我想复制看似简单的任务压缩文件夹和所有的文件/目录递归到一个7z文件。我只是通过7z.exe,但我似乎无法捕获进程的stdout,直到它完成,我想要一些每7z文件进展,因为我会压缩文件夹范围从几百的演出到超过一万亿字节。遗憾的是,我不能提供任何代码,我已经尝试,只是因为我见过的例子使用py7zlib的唯一的事情是从现有的文件提取文件。有没有任何人有这两个组合的运气,或者可以提供一些帮助?



这是值得的,这将是在Windows上使用python 2.7。奖励点如果有一些魔法多线程可能发生在这里,特别是考虑到lzma压缩似乎有多慢(时间,但是,这不是一个问题)。提前感谢!

解决方案

纯Python替代方法是创建 .tar.xz 文件与标准库的组合 tarfile 模块和 liblzma 包装模块 pyliblzma 。这将创建大小与 .7z 档案类似的文件:

  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 类,因此

下面的例子中,nofollow> add 方法才会返回。问题讨论监视tar文件创建进度的可能策略:




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?

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!

解决方案

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()

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.

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

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

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