Python tarfile未创建有效的.tar.gz文件
我有一个Django应用程序,它创建了一个.tar.gz文件供下载。在本地,我在我的开发机器Python2.7和远程开发服务器Python2.6.6上运行。下载文件时,我可以通过Mac Finder/命令行打开并查看内容。但是,Python2.7不喜欢在我的远程开发服务器上创建的.tar.gz文件……我需要将这些文件上载到使用Python解包/解析归档文件的站点。我如何调试出了什么问题?在Python shell中:Python tarfile未创建有效的.tar.gz文件,python,django,python-2.7,tarfile,Python,Django,Python 2.7,Tarfile,我有一个Django应用程序,它创建了一个.tar.gz文件供下载。在本地,我在我的开发机器Python2.7和远程开发服务器Python2.6.6上运行。下载文件时,我可以通过Mac Finder/命令行打开并查看内容。但是,Python2.7不喜欢在我的远程开发服务器上创建的.tar.gz文件……我需要将这些文件上载到使用Python解包/解析归档文件的站点。我如何调试出了什么问题?在Python shell中: >>> tarfile.is_tarfile('myTest
>>> 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
我不太确定我还能尝试什么。似乎抛出异常是因为我的tarfile具有self.offset==0
,但我不知道这意味着什么,也不知道如何创建tarfile以避免发生这种情况。欢迎您的建议
不确定什么代码在这里有用。创建并返回tar文件的代码:
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())
zip\u文件名=“%s\u%s.tar.gz%”(course.name,course.url)
s=cStringIO.StringIO()
zf=tarfile.open(zip_文件名,mode='w:gz',fileobj=s)
,我还使用命令行中的tar-zxvf myTestFile_remote.tar.gz
验证了远程文件是tar.gz文件。文件提取得很好。我认为问题出在zlib
中,而不是tar文件本身
解决办法:
- 使用
bz2创建文件
tarfile.open(zip_文件名,mode='w:bz2',fileobj=s)
- 强制压缩级别(写入/读取)
zf=tarfile.open(zip_文件名,mode='w:gz',fileobj=s,compresslevel=9)
zf=tarfile.open(zip_文件名,mode='r:gz',compresslevel=9)
- 降低压缩级别,直到问题消失
zf=tarfile.open(zip_文件名,mode='w:gz',fileobj=s,compresslevel=[9-0])
- 完全消除压缩
tarfile.open(zip\u文件名,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()
您向tar添加了什么?图像文件、XML文档、HTML文件。本地和远程都添加了相同类型的文件…您是否关闭了tarfile?是的,很抱歉,更新我的示例代码块以显示(试图简化示例代码),就像测试一样,您可以尝试使用压缩0
来查看发生了什么?
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()