Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Fixing the error: zipfile badzipfile file is not a zip file
Next
Answered
Cooper Byrne
  • 21
Cooper Byrne
Asked: May 18, 20222022-05-18T20:40:36+00:00 2022-05-18T20:40:36+00:00In: python

Fixing the error: zipfile badzipfile file is not a zip file

  • 21

. Advertisement .

..3..

. Advertisement .

..4..

I get an error: “BadZipfile: File is not a zip file” When I’m using Python 2.5 and i open zip files with Python’s zipfile module [ zipfile.ZipFile(“filex.zip”) ]

Here are the error details:

Traceback (most recent call last):
 File "<module1>", line 5, in <module>
  zipfile.ZipFile("c:/temp/test.zip")
 File "C:\Python25\lib\zipfile.py", line 346, in init
  self._GetContents()
 File "C:\Python25\lib\zipfile.py", line 366, in _GetContents
  self._RealGetContents()
 File "C:\Python25\lib\zipfile.py", line 378, in _RealGetContents
  raise BadZipfile, "File is not a zip file"
 BadZipfile: File is not a zip file
END_BLOCK: 4096,
  comment: '\x00',
  data: '\xd6\xf6\x03\x00\x88,N8?<e\xf0q\xa8\x1cwK\x87\x0c(\x82a\xee\xc61N'1qN\x0b\x16K-\x9d\xd57w\x0f\xa31n\xf3dN\x9e\xb1s\xffu\xd1\.....', (truncated)
  endrec: ['PK\x05\x06', 0, 0, 4, 4, 268, 199515, 0],
  filesize: 199806L,
  fpin: <open file 'c:/temp/test.zip', mode 'rb' at 0x045D4F98>,
  start: 4073

How to fix the zipfile badzipfile file is not a zip file. Please give me some good ideas.

badzipfile file is not a zip file
  • 3 3 Answers
  • 1k Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-21T06:09:22+00:00Added an answer on June 21, 2022 at 6:09 am

    The cause: The cause of this error is that Python can be confused with files named file.

    Solution: You can fix this error by renaming these files. If that doesn’t work, try the following 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("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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Ambre Girard
    2022-05-25T21:07:55+00:00Added an answer on May 25, 2022 at 9:07 pm

    Files named file can cause confusion in python. Try naming it another name. If it STILL doesn’t work, you can try this 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("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 
    • 23
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
    • Jim
      2022-06-09T06:54:37+00:00Replied to answer on June 9, 2022 at 6:54 am

      Gzip, renaming, tracing, and 7zip expressions are the most basic ways to overcome the problem (Zipfile.badzipfile: file is not a zip) in Python. In fact, these two options necessitate the installation of new modules.

      • 15
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.