2015-11-25 209 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