【问题标题】:Generating ZipFile with List of StringIO Object, CRC Error when Opening the ZipFIle使用 StringIO 对象列表生成 ZipFile,打开 ZipFIle 时出现 CRC 错误
【发布时间】:2018-03-26 09:35:16
【问题描述】:

我目前在使用 Python 2.7 生成具有多行文本的文本文件并将其添加到内存中的 ZipFile 方面遇到一些困难。

下面的代码可以生成包含 4 个文本文件的 zip 文件,每个文件有 1 行单词。

如果我将代码“temp[0].write('first in-memory temp file')”修改为多行字符串,生成的zip文件会出现crc错误。

我尝试过字符串转义,但失败了。

我可以知道我应该怎么做才能生成填充多行启用文本文件的 ZipFile 吗?

提前致谢。

# coding: utf-8

import StringIO
import zipfile

# This is where my zip will be written
buff = StringIO.StringIO()
# This is my zip file
zip_archive = zipfile.ZipFile(buff, mode='w')

temp = []
for i in range(4):
    # One 'memory file' for each file 
    # I want in my zip archive
    temp.append(StringIO.StringIO())

# Writing something to the files, to be able to
# distinguish them
temp[0].write('first in-memory temp file')
temp[1].write('second in-memory temp file')
temp[2].write('third in-memory temp file')
temp[3].write('fourth in-memory temp file')

for i in range(4):
    # The zipfile module provide the 'writestr' method.
    # First argument is the name you want for the file
    # inside your zip, the second argument is the content
    # of the file, in string format. StringIO provides
    # you with the 'getvalue' method to give you the full
    # content as a string
    zip_archive.writestr('temp'+str(i)+'.txt',
                         temp[i].getvalue())

# Here you finish editing your zip. Now all the information is
# in your buff StringIO object
zip_archive.close()

# You can visualize the structure of the zip with this command
print zip_archive.printdir()

# You can also save the file to disk to check if the method works
with open('test.zip', 'w') as f:
    f.write(buff.getvalue())

【问题讨论】:

    标签: python python-2.7 text-files zipfile stringio


    【解决方案1】:

    我猜您使用的是 Windows?尝试以二进制模式打开输出 zip 文件,即

    with open('test.zip', 'wb') as f:
        f.write(buff.getvalue())
    

    在文本模式(默认)下,Python 将新行 ('\n') 转换为本机行结束序列,在 Windows 中为 \r\n。这将导致 CRC 失败,因为 CRC 是使用 StringIO 缓冲区中的数据计算得出的,但是当写入文本模式文件时,数据会被更改(\n 转换为 \r\n)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多