python zipfile压缩比_Zipfile Python模块字节大小差异

I'm using zipfile module for python to extract a zipfile I retrieved from the internet using

urllib.urlretrieve()

the files in the zip file are patch files created by bsdiff, however when I let python extract the zip file and try to use bspatch it says corrupted patch file. When I manually extract the zip file using 7-zip overwrite the patch files and then run the patcher it patches fine. I also noticed when overwriting these files manually that the bytesize differed.

One should be 195 bytes but is 196 bytes, one should be 20656 bytes but is 20781 bytes and one is the right size (which is the only one which patches without the corrupt patch message)

The code I'm using to extract is:

z = zipfile.ZipFile('patchfiles.zip', 'r', zipfile.ZIP_DEFLATED)

z.printdir()

for info in z.infolist():

if not os.path.isdir(patchdir):

os.mkdir(patchdir)

fname = info.filename

data = z.read(fname)

dest = os.path.join(patchdir, fname)

data = z.read(fname)

f = open(dest, 'w')

f.write(data)

f.close()

z.close()

The zip file is compressed using a normal Deflate, I even tried just using a ZIP_STORED with 7zip just zipping it up as a stored file.

Any ideas?

解决方案

Is this on Windows? Maybe try f = open(dest, 'wb')

On Windows only, the b makes the filesystem treat the file as binary as opposed to plain text, and not mess with the line endings. On other systems, there's no difference (and the b is silently ignored).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值