我在S3中有一个压缩文件,我正在下载它,然后解压缩它。
我对这些行进行了编码:
conn = S3Connection('','')
bucket = conn.get_bucket('buck1')
key = bucket.get_key("folder1/foldr2/file1.gz")
f = open('/folder1/folder2/file1.gz', 'w')
key.get_file(f)
f.close()
cmd = 'unzip /folder1/folder2/file1.gz'
system(cmd)但是这给了以下的错误
End-of-central-directory signature not found. Either this file is not
Archive: /folder1/folder2/file1.gz
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of /folder1/folder2/file1.gz or
/folder1/folder2/file1.gz.zip, and cannot find /folder1/folder2/file1.gz.ZIP, period.我也尝试过解压缩这个代码,但这也导致了cannot be unzipped as this does not seems a zip file错误。
zip_ref = zipfile.ZipFile('/folder1/folder2/file1.gz', 'r')
zip_ref.extractall('/folder1/folder2/')
zip_ref.close()我知道当zip文件出现问题时会出现错误,但我不明白的是,我只是从S3中提取文件并试图解压缩它。如何解决此错误并获得所需的结果?
注意:我也不能手动解压缩我的机器(linux)上的文件。我可以看到已下载的文件,但无法解压缩并获得error.Also,如果我手动从s3下载该文件,然后手动解压缩,则它将不会出现任何错误。
发布于 2015-03-23 23:20:49
AFAIK,您不能使用unzip打开gzip存档。unzip只适用于.ZIP文件。对gunzip文件使用.gz命令:
cmd = 'gunzip /folder1/folder2/file1.gz'
system(cmd)编辑:
如果该文件仍然损坏,您应该确保它实际上是一个gzip文件。尝试以下几点:
hd /folder1/folder2/file1.gz | head你应该得到这样的东西:
00000000 1f 8b 08 08 0e 7f fc 50 00 03 63 6f 70 79 5f 63 |.......P..copy_c|确保00000000之后的第二个八进制是1f 8b,这是一个gzip文件的头文件。
发布于 2015-03-30 19:30:21
虽然文件很可能不是一个好的.gz文件(正如前面提到的那样,您不能使用解压缩来处理.gz gzip文件),但是有另一种方法可以不显式地使用文件句柄来下载文件。
根据您的代码:
key.set_contents_to_file('/path/to/file.gz')您还可以查看gzip模块https://docs.python.org/2/library/gzip.html。
https://stackoverflow.com/questions/29222034
复制相似问题