Python模块字节大小差异

Python模块字节大小差异

本文介绍了Zipfile Python模块字节大小差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python的zipfile模块来提取我使用

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

urllib.urlretrieve()从互联网上检索到的zipfile。 c $ c>

urllib.urlretrieve()

zip文件中的文件是bsdiff创建的补丁文件,但是当我让python提取该zip文件并尝试使用bspatch时,它表示损坏的补丁文件。当我使用7-zip手动提取zip文件时,覆盖补丁文件,然后运行修补程序,它会很好地进行修补。我还注意到手动覆盖这些文件时字节大小有所不同。

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.

一个应为195字节,但为196字节,一个应为20656字节,但为20781字节,一个为正确的大小(这是唯一没有损坏的补丁消息进行补丁的大小)

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)

我要提取的代码是:

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

该zip文件使用普通的Deflate压缩,我什至尝试仅将ZIP_STORED和7zip压缩在一起作为存储文件。

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.

有什么想法吗?

推荐答案

在Windows上吗?也许尝试 f = open(dest,'wb')

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

仅在Windows上, b 使文件系统将文件视为二进制文件,而不是纯文本文件,并且不会弄乱行尾。在其他系统上,没有区别( b 被静默忽略)。

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

这篇关于Zipfile Python模块字节大小差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:42