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 a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:04:29+00:00 2026-06-07T00:04:29+00:00

I am trying to include a binary file within a zip file and below

  • 0

I am trying to include a binary file within a zip file and below is the code snippet:
I first unzip the zip contents into a temporary location and add few more files and zip it back to a new archive.

import zipfile


def test(fileName, tempDir):
    # unzip the file contents,may contain binary files
    myZipFile = zipfile.ZipFile(fileName, "r")
    for name in myZipFile.namelist():
        toFile = tempDir + "/" + name
        fd = open(toFile, "w")
        fd.write(myZipFile.read(name))
        fd.close()
    myZipFile.close()
    # code which post processes few of the files goes here

    # zip it back
    newZip = zipfile.ZipFile(fileName, mode="w")
    try:
        fileList = os.listdir(tempDir)
        for name in fileList:
            name = tempDir + "/" + name
            newZip.write(name, os.path.basename(name))
        newZip.close()
    except Exception:
        print("Exception occured while writing to PAR file: " + fileName)

Some of the files may be binary files. The zipping code works fine but when i try to unzip it using linux ‘ unzip or python’s zip module , i get the below error:

zipfile corrupt. (please check that you have transferred or
created the zipfile in the appropriate BINARY mode and that you have
compiled UnZip properly)

And am using python 2.3

What’s going wrong here ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:04:30+00:00Added an answer on June 7, 2026 at 12:04 am

    Hmm not sure if its a bug in python 2.3. Current work environment do not allow me to upgrade to a higher version of python 🙁 🙁 🙁

    The below workaround worked:

    import zipfile
    
    def test(fileName, tempDir):
        # unzip the file contents,may contain binary files
        myZipFile=zipfile.ZipFile(fileName,'r')
        for name in myZipFile.namelist(): 
            toFile = tempDir + '/' + name
    
            # check if the file is a binary file
            #if binary file, open it in "wb" mode
                fd = open(toFile, "wb")
            #else open in just "w" mode
                fd = open(toFile, "w")
    
            fd.write(myZipFile.read(name))
            fd.close()
        myZipFile.close()
        # code which post processes few of the files goes here
    
        #zip it back
        newZip = zipfile.ZipFile(fileName, mode='w')
        try:
            fileList = os.listdir(tempDir)
            for name in fileList:
                name = tempDir + '/' + name
                newZip.write(name,os.path.basename(name))
            newZip.close()
        except Exception:
                print 'Exception occured while writing to PAR file: ' + fileName    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to load binary file using fstream in the following way: #include <iostream>
I am trying to mmap a binary file (~ 8Gb) using the following code
Working on a binary search. The code below should explain what I'm trying to
I am trying to read a binary file and store it into a database,
I'm trying include this barcode source code Stefanhafeneger - Barcode github.com to my project
I am trying to include a .php file that will generate a form for
I am trying to include an IF within my LET in LINQ but i
I'm trying to make a program that creates and reads a binary file, which
In my program, I am trying to read data from binary file, and then
I'm trying to convert the following bash code into C++ using boost::iostreams: #!/usr/bin/bash (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.