我使用这个脚本压缩我的文件
import pylzma, struct
i = open(path+fileName,'rb')
o = open(path+zipName+'.zip','wb')
data = i.read()
c = pylzma.compressfile(data, eos=1)
result = c.read(5)
result += struct.pack('<Q', len(data))
o.write(result + c.read())
i.close()
o.close()
我使用此方法,如 PyLZMA documentation 中所示因为它允许 7zip 或 lzma.exe 读取我的文件。使用 7zip 解压缩效果很好,但当我使用 PyLZMA 时它不起作用。我用这个:
i = open(path+name+'.zip', 'rb')
o = open(path+name, 'wb')
data = i.read()
u = pylzma.decompress(data)
o.write(u)
它在 pylzma.decompress 上停止,我收到以下错误:
TypeError: Error while decompressing: 1
如果我正在阅读 documentation正确(我在安装 PyLZMA 时遇到问题,所以我无法验证),compress()
输出 decompress()
可以处理的字符串。
但是,为了使压缩后的字符串与其他实用程序兼容,有必要在前 5 个字节和压缩数据的其余部分之间插入 8 个字节的长度。
因此,如果您想使用 PyLZMA 解压缩数据,我怀疑您需要手动删除该 8 字节长度字段(最快的方法可能是打开输入文件,读取 5 个字节,跳过 8,然后读取文件的其余部分)。
我是一名优秀的程序员,十分优秀!