Zipfile.badzipfile: file is not a zip file

·

2 min read

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.

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.