Skip to content

Instantly share code, notes, and snippets.

@romiras
Created January 14, 2017 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romiras/fd9d534db29b5ee7e97c20dbc1bf6b06 to your computer and use it in GitHub Desktop.
Save romiras/fd9d534db29b5ee7e97c20dbc1bf6b06 to your computer and use it in GitHub Desktop.
ReZip - tool for recompression Zip files, used to efficiently store in SCM
#!/usr/bin/env python3
"""Read zip format file from stdin and write new zip to stdout.
With the --store option the output will be an uncompressed zip.
Uncompressed files are stored more efficiently in VCS.
Example:
python rezip.py --store < file.docx > file.flat.docx
Based on https://github.com/costerwi/rezip
"""
import sys
import io
import zipfile
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--store",
help="Store data to stdout zip without compression",
action="store_true")
args = parser.parse_args()
if args.store:
compression = zipfile.ZIP_STORED
else:
compression = zipfile.ZIP_DEFLATED
if not hasattr(sys.stdout, 'buffer'):
raise RuntimeError('Sorry, Python3 is required.')
# Use BytesIO objects as random access source and destination files
with io.BytesIO(sys.stdin.buffer.read()) as source, io.BytesIO() as dest:
# Read and re-zip the file in memory
with zipfile.ZipFile(source, 'r') as source_zip, zipfile.ZipFile(dest, 'w') as dest_zip:
for info in source_zip.infolist(): # Iterate over each file in zip
dest_zip.writestr(info, source_zip.read(info), compression)
dest_zip.comment = source_zip.comment # Copy the comment if any
# Write the dest file as binary to stdout
dest.seek(0)
sys.stdout.buffer.write(dest.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment