0

I tried unzipping a file through Python using zipfile.extractAll but it gave BAD zip file, hence I tried this:

zipfile cant handle some type of zip data?

As mentioned in this answer, i used the code:

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("Truncating 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  enter code here

but it gave the error

Message File Name Line Position Traceback
C:\Users\aditya1.r\Desktop\Python_pyscripter\module1.py 50
main C:\Users\aditya1.r\Desktop\Python_pyscripter\module1.py 17
fixBadZipfile C:\Users\aditya1.r\Desktop\Python_pyscripter\module1.py 37
TypeError: 'str' does not support the buffer interface

I'm using Python 3.4

How can i unzip this file?

Community
  • 1
  • 1
Aditya Rohilla
  • 327
  • 2
  • 5
  • 15

2 Answers2

0

You are reading the file as a bytes object but trying to find passing a string object so just simply change this line -

pos = data.find('\x50\x4b\x05\x06')

to

pos = data.find(b'\x50\x4b\x05\x06') 

Note that I have casted it to a byte object by simply prepending a b.

You don't need to do this is Python 2.X but in python 3.X you need to explicitly serialize a string object to a byte object.

hashcode55
  • 5,622
  • 4
  • 27
  • 40
0
import subprocess

subprocess.Popen('unzip ' + file_name, shell = True).wait()

Hope this help you :)

1pa
  • 715
  • 2
  • 9
  • 23
  • Can you please explain how does this work? Not showing any output niether unzipping the file. – Aditya Rohilla Mar 01 '17 at 09:38
  • @AdityaRohilla Imagine that you want to unzip file "example.zip", then you run your python script where "example.zip" is. This will not output anything, but if you check the folder where "example.zip" is, you will find the unzip file. With subprocess when you write ´'unzip ' + file_name´ is like you are writing on the command line. If you need more help just ask! – 1pa Mar 01 '17 at 09:55
  • Still no help. The program just waits without any change in the zip file. – Aditya Rohilla Mar 03 '17 at 05:56