2015-11-25 205 views
0

我想解压缩我的脚本中的zip文件,使用Python的zipfile模块。问题是,当我尝试解压缩这个文件,它提出了Bad magic number for file header error无法解压zip文件

这是error

.. 
    zip_ref.extractall(destination_to_unzip_file) 
    File "C:\Python27\lib\zipfile.py", line 1040, in extractall 
    self.extract(zipinfo, path, pwd) 
    File "C:\Python27\lib\zipfile.py", line 1028, in extract 
    return self._extract_member(member, path, pwd) 
    File "C:\Python27\lib\zipfile.py", line 1082, in _extract_member 
    with self.open(member, pwd=pwd) as source, \ 
    File "C:\Python27\lib\zipfile.py", line 971, in open 
    raise BadZipfile("Bad magic number for file header") 
zipfile.BadZipfile: Bad magic number for file header 

文件我要解压缩下载这种方式:

_url = """http://edane.drsr.sk/report/ds_dphs_csv.zip""" 

def download_platici_dph(self): 
    if os.path.isfile(_destination_for_downloads+'platici_dph.zip'): 
     os.remove(_destination_for_downloads+'platici_dph.zip') 
    with open(_destination_for_downloads+'platici_dph.zip','w') as f: 
     response = requests.get(_url,stream=True) 
     if not response.ok: 
      print 'Something went wrong' 
      return False 
     else: 
      for block in response.iter_content(1024): 
       f.write(block) 

有谁知道问题在哪里?

+0

您是否尝试过使用其他工具来确保解压缩文件I t是一个有效的ZIP文件? –

+0

@MartinEvans是的,我试过了,它工作。 Rob在回答中说,问题出在下载文件。 –

回答

3

答曰the documentation for open():“打开二进制文件时,你应该追加'b'的模式值以二进制方式打开文件”

打开使用b二进制输出文件:

with open(_destination_for_downloads+'platici_dph.zip','wb') as f: 
1

我尝试下载您的归档文件,而无需使用您的下载代码,然后用它提取:

import zipfile 
with zipfile.ZipFile("ds_dphs_csv.zip") as a: 
     a.extractall() 

它工作得很好。例外zipfile.BadZipfile在标题出现问题时引发,因此我认为您的文件在下载后损坏。下载方法一定有问题。

你可以找到在这个帖子异常的更多细节:Python - Extracting files from a large (6GB+) zip file