chevron_left

Introduction

This article will teach you to split solve the error Zipfile. badzipfile: file is not a zip file using several solutions. If you are trying to know how we can solve this error, this article should help you. You will learn the easiest methods to do this with several libraries.

Table of contents

  1. How to solve the error Zipfile.badzipfile: file is not a zip
  2. Solution 1
  3. Solution 2
  4. In general
  5. Conclusion

How to solve the error Zipfile.badzipfile: file is not a zip

The ZIP file format may be a common file and compression standard. This module gives apparatuses to make, examined, compose, add, and list a ZIP record. Any progressed utilisation of this module will require an understanding of the arrangement, as characterized in the PKZIP Application Note.

This module does not as of now handle multi-disk ZIP files. It can handle ZIP records that utilize the ZIP64 expansions (that's ZIP records that are more than 4 GiB in estimate). It bolsters unscrambling of scrambled records in ZIP chronicles, but it right now cannot create a scrambled record. Unscrambling is amazingly moderate because it is actualized in local Python instead of C. The module characterizes the taking after items:


exception zipfile.BadZipFile The blunder raised for terrible ZIP records.
Now, let’s move to the solutions


Solution 1

def fixBadZipfile(zipFile):  
 f = open(zipFile, 'r+b')  
 data = f.read()  
 pos = data.find('\x50\x4b\x05\x06') # End of central directory signature  
 if (pos > 0):  
     self._log("Trancating file at location " + str(pos + 22)+ ".")  
     f.seek(pos + 22)   # size of 'ZIP end of central directory record' 
     f.truncate()  
     f.close()  
 else:  
     # raise error, file is truncated 

If this solution doesn’t work try this:

content = zipFileContainer.read()
pos = content.rfind('\x50\x4b\x05\x06') # reverse find: this string of bytes is the end of the zip's central directory.
if pos>0:
    zipFileContainer.seek(pos+20) # +20: see secion V.I in 'ZIP format' link above.
    zipFileContainer.truncate()
    zipFileContainer.write('\x00\x00') # Zip file comment length: 0 byte length; tell zip applications to stop reading.
    zipFileContainer.seek(0)

return zipFileContainer

Solution 2

You can also download 7zip and put it in the same folder of the script and then do the following

import subprocess
ziploc = "C:/Program Files/7-Zip/7z.exe" #location where 7zip is installed
cmd = [ziploc, 'e',your_Zip_file.zip ,'-o'+ OutputDirectory ,'-r' ] 
sp = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)

And also you can replace zipfile module with gzip.GzipFile

In general

We can use several methods solve the error Zipfile.badzipfile: file is not a zip using python


Conclusion

Using python, the most straightforward methods to solve the error (Zipfile.badzipfile: file is not a zip) are gzip, renaming, tracing and 7zip expressions. In fact, these 2 solutions require new modules to download.
If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

'virtualenv' is not recognized as an internal or external command, operable program or batch file Ankur Ranpariya 1 - Aug 22
Jupyter' is not recognized as an internal or external command, operable program or batch file JimCoder - Jul 14
TypeError: a bytes-like object is required, not 'Problem' beekip - Sep 17
A bytes-like object is required, not 'str' Ankur Ranpariya - Sep 16
Typeerror a bytes like object is required not str Ankur Ranpariya - Aug 29
_csv.error: iterator should return strings, not bytes (did you open the file in text mode?) zhteja - Sep 8
How to Read a File line by line in Python umasivasu21 - Aug 2
TypeError: 'int' object is not iterable in Python Ferdy - Sep 30
Typeerror: module object is not callable Ankur Ranpariya - Sep 24
[Solved] Typeerror: 'numpy.float64' object is not iterable beekip - Sep 15
Reduce is not defined Python Aizhamal Zhetigenova - Jun 1
distributed 1.21.8 requires msgpack, which is not installed - Fixed sadmin - May 20
The truth value of an array with more than one element is ambiguous. use a.any() or a.all() AAbouelenien - Sep 22
How to check a item is present in list Saikat Saha - Sep 14
Which method is used to delete a directory in Python Ferdy - Aug 24
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). zhteja - Aug 1
Is a Formally Defined Procedure for Performing some Calculation in Python Ankur Ranpariya - Jul 31
Check if a number is between two values using python amr12345a - Jun 1
Write a Program to Print First x Terms of the Series 3n+2 which are not Multiples of 4 beekip - Sep 20
Object of type 'zip' has no len() Aizhamal Zhetigenova - Jun 1