我已经使用了一点点幽门,但我必须能够创建与7zip窗口应用程序兼容的文件。请注意,我的一些文件确实很大(3到4gb是由第三方软件以专有二进制格式创建的)。
我一遍又一遍地看了一遍又一遍,按照这里的说明:https://github.com/fancycode/pylzma/blob/master/doc/USAGE.md
我能够用以下代码创建兼容的文件:
def Compacts(folder,f):
os.chdir(folder)
fsize=os.stat(f).st_size
t=time.clock()
i = open(f, 'rb')
o = open(f+'.7z', 'wb')
i.seek(0)
s = pylzma.compressfile(i)
result = s.read(5)
result += struct.pack('<Q', fsize)
s=result+s.read()
o.write(s)
o.flush()
o.close()
i.close()
os.remove(f)较小的文件(高达2Gb)使用这段代码可以很好地压缩,并且与7Zip兼容,但是较大的文件只会在一段时间后使python崩溃。
根据用户指南,要压缩大文件,应该使用流,但结果文件与7zip不兼容,如下面的代码段所示。
def Compacts(folder,f):
os.chdir(folder)
fsize=os.stat(f).st_size
t=time.clock()
i = open(f, 'rb')
o = open(f+'.7z', 'wb')
i.seek(0)
s = pylzma.compressfile(i)
while True:
tmp = s.read(1)
if not tmp: break
o.write(tmp)
o.flush()
o.close()
i.close()
os.remove(f)在保持7zip兼容性的同时,如何将pylzma中的流技术结合起来,有什么想法吗?
发布于 2014-05-16 00:55:28
您仍然需要正确地编写标题(.read(5))和大小,例如:
import os
import struct
import pylzma
def sevenzip(infile, outfile):
size = os.stat(infile).st_size
with open(infile, "rb") as ip, open(outfile, "wb") as op:
s = pylzma.compressfile(ip)
op.write(s.read(5))
op.write(struct.pack('<Q', size))
while True:
# Read 128K chunks.
# Not sure if this has to be 1 instead to trigger streaming in pylzma...
tmp = s.read(1<<17)
if not tmp:
break
op.write(tmp)
if __name__ == "__main__":
import sys
try:
_, infile, outfile = sys.argv
except:
infile, outfile = __file__, __file__ + u".7z"
sevenzip(infile, outfile)
print("compressed {} to {}".format(infile, outfile))https://stackoverflow.com/questions/23690552
复制相似问题