【问题标题】:Python tarfile not creating valid .tar.gz filePython tarfile 未创建有效的 .tar.gz 文件
【发布时间】:2014-12-16 19:43:30
【问题描述】:

我有一个 Django 应用程序,它创建一个 .tar.gz 文件以供下载。在本地,我在我的开发机器 Python 2.7 和远程开发服务器 Python 2.6.6 上运行。当我下载文件时,我可以通过 Mac Finder / 命令行打开并查看内容。但是,Python 2.7 不喜欢在我的远程开发服务器上创建的 .tar.gz 文件……我需要将这些文件上传到使用 Python 解包/解析档案的站点。我怎样才能调试出了什么问题?在 Python 外壳中:

>>> tarfile.is_tarfile('myTestFile_remote.tar.gz')
False

>>> tarfile.is_tarfile('myTestFile_local.tar.gz')
True

>>> f = tarfile.open('myTestFile_remote.tar.gz', 'r:gz')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py", line 1678, in open
    return func(name, filemode, fileobj, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py", line 1727, in gzopen
    **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py", line 1705, in taropen
    return cls(name, mode, fileobj, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py", line 1574, in __init__
    self.firstmember = self.next()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py", line 2331, in next
    raise ReadError(str(e))
tarfile.ReadError: invalid header

来自this SO question,我还尝试对远程文件运行gzip -t,但没有输出(我相信这意味着文件正常)。从this other SO question,我运行file myTestFile_remote.tar.gz,我相信输出显示了正确的文件格式:

myTestFile_remote.tar.gz: gzip compressed data, from Unix

我不太确定我还能尝试什么。似乎抛出异常是因为我的 tarfile 有 self.offset == 0,但我不知道这意味着什么,而且我不明白如何创建 tarfile 以免发生这种情况。欢迎提出建议...

不确定什么代码在这里有用。我创建和返回 tarfile 的代码:

zip_filename = '%s_%s.tar.gz' % (course.name, course.url)
s = cStringIO.StringIO()
zf = tarfile.open(zip_filename, mode='w:gz', fileobj=s)

<add a bunch of stuff>

zipped = zip_collection(zip_data)
zf.close()

if zipped:
    response = HttpResponse(content_type="application/tar")
    response['Content-Disposition'] = 'attachment; filename=%s' % zip_filename
    s.seek(0, os.SEEK_END)
    response.write(s.getvalue())

----- 更新 ------ 根据this SO post,我还使用命令行中的tar -zxvf myTestFile_remote.tar.gz 验证了远程文件是tar.gz 文件。文件解压得很好。

【问题讨论】:

  • 你在 tar 中添加了什么?
  • 图像文件、XML 文档、HTML 文件。本地和远程都添加相同类型的文件...
  • 您是否关闭了 tar 文件?
  • 是的,抱歉,更新我的示例代码块以显示(试图简化示例代码)
  • 就像测试一样,您可以尝试使用压缩0 看看会发生什么?

标签: python django python-2.7 tarfile


【解决方案1】:

我认为问题出在zlib 而不是tarfile 本身。

解决方法:

  • 使用bz2创建文件
    tarfile.open(zip_filename, mode='w:bz2', fileobj=s)

  • 强制压缩级别(写入/读取)

    zf = tarfile.open(zip_filename, mode='w:gz', fileobj=s, compresslevel=9)

    zf = tarfile.open(zip_filename, mode='r:gz', compresslevel=9)

  • 降低压缩水平直到问题消失

    zf = tarfile.open(zip_filename, mode='w:gz', fileobj=s, compresslevel=[9-0])

  • 完全消除压缩

    tarfile.open(zip_filename, mode='w', fileobj=s)

最后一个是仅在绝对需要压缩并且以前没有工作的情况下:

f = open(zip_filename, "w") 
proc = subprocess.Popen(["gzip", "-9"], stdin=subprocess.PIPE, stdout=fobj) 
tar = tarfile.open(fileobj=proc.stdin, mode="w|") 
tar.add(...) 
tar.close() 
proc.stdin.close() 
f.close() 

【讨论】:

    猜你喜欢
    • 2013-11-23
    • 2014-06-16
    • 2013-02-22
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    相关资源
    最近更新 更多