无法解压用zipfile构建的压缩文件(Python)(Can't unzip archive built with zipfile (Python))

编程入门 行业动态 更新时间:2024-10-28 11:33:15
无法解压用zipfile构建的压缩文件(Python)(Can't unzip archive built with zipfile (Python))

我在使用Python中的zipfile构建的存档时遇到了问题。 我遍历目录中的所有文件并将它们写入存档。 当我试图提取它们后,我得到一个与路径分隔符相关的异常。

the_path= "C:\\path\\to\\folder" zipped= cStringIO.StringIO() zf = zipfile.ZipFile(zipped_cache, "w", zipfile.ZIP_DEFLATED) for dirname, subdirs, files in os.walk(the_path) : for filename in files: zf.write(os.path.join(dirname, filename), os.path.join(dirname[1+len(the_path):], filename)) zf.extractall("C:\\destination\\path") zf.close() zipped_cache.close()

以下是例外情况:

zipfile.BadZipfile:目录“env \ index”和头文件“env / index”中的文件名不同。

更新:我用临时文件( tempfile.mkstemp("temp.zip") )替换了字符串缓冲区cStringIO.StringIO() ),现在它工作。 当zipfile模块写入缓冲区破坏归档文件时会发生这种情况,但不确定问题是什么。

问题在于,我正在读取/写入以“r”/​​“w”模式而不是“rb”/“wb”打开的文件中的信息。 这在Linux中不是问题,但由于字符编码的原因,它在Windows中出现错误。 解决了。

I'm having problems with an archive that I built using zipfile in Python. I'm iterating over all the files in a directory and writing them to an archive. When I attempt to extract them afterward I get an exception related to the path separator.

the_path= "C:\\path\\to\\folder" zipped= cStringIO.StringIO() zf = zipfile.ZipFile(zipped_cache, "w", zipfile.ZIP_DEFLATED) for dirname, subdirs, files in os.walk(the_path) : for filename in files: zf.write(os.path.join(dirname, filename), os.path.join(dirname[1+len(the_path):], filename)) zf.extractall("C:\\destination\\path") zf.close() zipped_cache.close()

Here's the exception:

zipfile.BadZipfile: File name in directory "env\index" and header "env/index" differ.

Update: I replaced the string buffer cStringIO.StringIO() with a temporary file (tempfile.mkstemp("temp.zip")) and now it works. There's something that happens when the zipfile module writes to the buffer that corrupts the archive, not sure what the problem is though.

The issue was that I was reading/writing the information from/into files that were open in "r"/"w" mode instead of "rb"/"wb". This isn't an issue in Linux, but it gave me errors in Windows due to character encoding. Solved.

最满意答案

您应该考虑在字符串之前添加一个r来表示它是一个原始字符串 - 路径中的反斜杠将被解释为转义字符。

以下代码:

#!/bin/env python print(r"C:\destination\path") print(r"C:\path\to\folder") print("C:\destination\path") print("C:\path\to\folder")

产生以下输出:

C:\destination\path C:\path\to\folder C:\destination\path C:\path o older

请注意,\ t和\ f在最后一行中被解释为制表符

有趣的是,你也可以改变反斜杠为正斜杠(即open("C:/path/to/folder" ),这将工作。

或者,用反斜杠(即open("C:\\path\\to\\folder") )转义反斜杠。

国际海事组织,最明确和最简单的解决方案是简单地添加一个r


编辑:它看起来像你需要去与第二个解决方案,正斜杠。 zipfile库显然是严格的 - 并且考虑到这是一个纯粹的窗口缺陷,它可能会偷偷摸摸地通过。 (见问题6839 )。

Found the answer to my question here: http://www.penzilla.net/tutorials/python/scripting.

I'm pasting the two functions that are relevant to zipping up a directory. The problem was not the string buffer, nor the slashes, but the way I was iterating and writing to the zipfile. These 2 recursive functions fix the problem. Iterating over the entire tree of sub-directories with os.walk is not a good way to write the archive.

def zippy(path, archive): paths = os.listdir(path) for p in paths: p = os.path.join(path, p) # Make the path relative if os.path.isdir(p): # Recursive case zippy(p, archive) else: archive.write(p) # Write the file to the zipfile return def zipit(path, archname): # Create a ZipFile Object primed to write archive = ZipFile(archname, "w", ZIP_DEFLATED) # "a" to append, "r" to read # Recurse or not, depending on what path is if os.path.isdir(path): zippy(path, archive) else: archive.write(path) archive.close() return "Compression of \""+path+"\" was successful!"

更多推荐

本文发布于:2023-08-02 23:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382531.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:压缩文件   Python   zipfile   built   archive

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!