【问题标题】:Python, write in memory zip to filePython,在内存中写入 zip 到文件
【发布时间】:2013-08-29 17:49:26
【问题描述】:

如何将内存中的 zipfile 写入文件?

# Create in memory zip and add files
zf = zipfile.ZipFile(StringIO.StringIO(), mode='w',compression=zipfile.ZIP_DEFLATED)
zf.writestr('file1.txt', "hi")
zf.writestr('file2.txt', "hi")

# Need to write it out
f = file("C:/path/my_zip.zip", "w")
f.write(zf)  # what to do here? Also tried f.write(zf.read())

f.close()
zf.close()

【问题讨论】:

    标签: python zip stringio


    【解决方案1】:

    StringIO.getvalue返回StringIO的内容:

    >>> import StringIO
    >>> f = StringIO.StringIO()
    >>> f.write('asdf')
    >>> f.getvalue()
    'asdf'
    

    或者,您可以使用seek 更改文件的位置:

    >>> f.read()
    ''
    >>> f.seek(0)
    >>> f.read()
    'asdf'
    

    尝试以下操作:

    mf = StringIO.StringIO()
    with zipfile.ZipFile(mf, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
        zf.writestr('file1.txt', "hi")
        zf.writestr('file2.txt', "hi")
    
    with open("C:/path/my_zip.zip", "wb") as f: # use `wb` mode
        f.write(mf.getvalue())
    

    【讨论】:

    • 这给了我“ZipFile 实例没有属性'getvalue'”
    • @user984003,您是否在 Windows 中运行此代码?然后,您必须使用wb 模式。我更新了代码。
    • @user984003,在我的 ubuntu 盒子里,它运行良好。 i.imgur.com/MiT3UAX.png
    【解决方案2】:

    修改 falsetru 对 python3 的回答

    1) 使用io.StringIO 而不是StringIO.StringIO

    StringIO in python3

    2) 使用 b"abc" 代替 "abc" ,或

    python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file

    3) 编码为二进制字符串str.encode(s, "utf-8")

    Best way to convert string to bytes in Python 3?

    import zipfile
    import io
    mf = io.BytesIO()
    
    with zipfile.ZipFile(mf, mode="w",compression=zipfile.ZIP_DEFLATED) as zf:
    
        zf.writestr('file1.txt', b"hi")
    
        zf.writestr('file2.txt', str.encode("hi"))
        zf.writestr('file3.txt', str.encode("hi",'utf-8'))
    
    
    with open("a.txt.zip", "wb") as f: # use `wb` mode
        f.write(mf.getvalue())
    

    这也适用于 gzip:How do I gzip compress a string in Python?

    【讨论】:

    • 如果 Python 不会在版本上弄得这么乱的话,那将会是一门很好的语言。应该有一种方法可以检测 Py3 有效的答案...
    【解决方案3】:
      with ZipFile(read_file, 'r') as zipread:
            with ZipFile(file_write_buffer, 'w', ZIP_DEFLATED) as zipwrite:
                for item in zipread.infolist():
                    # Copy all ZipInfo attributes for each file since defaults are not preseved
                    dest.CRC = item.CRC
                    dest.date_time = item.date_time
                    dest.create_system = item.create_system
                    dest.compress_type = item.compress_type
                    dest.external_attr = item.external_attr
                    dest.compress_size = item.compress_size
                    dest.file_size = item.file_size
                    dest.header_offset = item.header_offset
    

    如果 zip 文件读取损坏并且您注意到缺少符号链接或时间戳错误的文件损坏,则可能是文件属性没有被复制。

    上面的代码sn-p就是我解决问题的方法。

    【讨论】:

    • 您的代码似乎对dest 没有任何作用。您是否缺少 zipwrite.write(dest) 之类的内容?
    猜你喜欢
    • 2019-06-18
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多