DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building an Identity-Aware MCP Server in Python
  • Build Your First Knowledge Graph From Unstructured Documents Using Python
  • Containerizing and Testing a Python Backtesting System With Docker and GitHub Actions
  • Python in 2026: uv vs Poetry vs pip: The Definitive Comparison

Trending

  • Add Observability to Your React Native Application in 5 Minutes
  • Exploring A Few Java 25 Language Enhancements
  • Securing Branch Networks With Firewalls, VPNs, IDS/IPS, and Identity-Based Access
  • How to Format Articles for DZone
  1. DZone
  2. Coding
  3. Languages
  4. Reading Corrupted/partial Zip Files In Python

Reading Corrupted/partial Zip Files In Python

By 
Snippets Manager user avatar
Snippets Manager
·
Apr. 09, 07 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
10.0K Views

Join the DZone community and get the full member experience.

Join For Free
First a simple script for reading non-corruted zipfiles in python:


filename = 'foo.zip'

import zipfile

z = zipfile.ZipFile(filename)

for i in z.infolist():
    print i.filename, i.file_size

z.read('somefile')


Next we use 'zip -FF foo.zip' to fix the zipfile, before reading it:


filename = 'foo.zip'

import zipfile

try:
    z = zipfile.ZipFile(filename)
except zipfile.BadZipfile:
    import commands
    commands.getoutput('zip -FF '+filename)
    z = zipfile.ZipFile(filename)

for i in z.infolist():
    print i.filename, i.file_size

try: 
    z.read('somefile')
except zipfile.BadZipfile:
    print 'Bad CRC-32'



In short: use 'zip -FF file.zip' to fix the file. It will restore the filelist.
Python (language)

Opinions expressed by DZone contributors are their own.

Related

  • Building an Identity-Aware MCP Server in Python
  • Build Your First Knowledge Graph From Unstructured Documents Using Python
  • Containerizing and Testing a Python Backtesting System With Docker and GitHub Actions
  • Python in 2026: uv vs Poetry vs pip: The Definitive Comparison

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook