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

  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • From Indicators to Insights: Automating IOC Enrichment Using Python and Threat Feeds
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever

Trending

  • 5 AI Security Incidents That Broke Things in Production (and What They Have in Common)
  • Every Cache Miss Is a Tiny Tax on Your Performance
  • Observability for Agents and Workflows: Tracing Prompts, Tool Calls, and Business Outcomes End-to-End
  • Why Stable RAG Answers Can Still Hide Unstable Evidence
  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
9.9K 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

  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • From Indicators to Insights: Automating IOC Enrichment Using Python and Threat Feeds
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever

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