Skip to content

Instantly share code, notes, and snippets.

@tonylukasavage
Created April 12, 2012 15:09
Show Gist options
  • Save tonylukasavage/2368003 to your computer and use it in GitHub Desktop.
Save tonylukasavage/2368003 to your computer and use it in GitHub Desktop.
Trying to unzip large files
# Make sure you add the -Xmx512M options or jython will throw a heap exception
# METHOD 1
import zipfile, os, sys
z = zipfile.ZipFile(sys.argv[1])
for name in z.namelist():
destdir = os.path.dirname(name)
if not os.path.isdir(destdir) :
os.makedirs(destdir)
print name
data = z.read(name)
outfile = open(name, 'wb')
outfile.write(data)
outfile.close()
z.close()
# METHOD 2
import zipfile
import zlib
import os
import sys
src = open( sys.argv[1], "rb" )
zf = zipfile.ZipFile( src )
for m in zf.infolist():
destdir = os.path.dirname(m.filename)
if not os.path.isdir(destdir) :
os.makedirs(destdir)
# Examine the header
print m.filename, m.header_offset, m.compress_size, repr(m.extra), repr(m.comment)
src.seek( m.header_offset )
src.read( 30 ) # Good to use struct to unpack this.
nm= src.read( len(m.filename) )
if len(m.extra) > 0: ex= src.read( len(m.extra) )
if len(m.comment) > 0: cm= src.read( len(m.comment) )
# Build a decompression object
decomp= zlib.decompressobj(-15)
# This can be done with a loop reading blocks
out= open( m.filename, "wb" )
result= decomp.decompress( src.read( m.compress_size ) )
out.write( result )
result = decomp.flush()
out.write( result )
# end of the loop
out.close()
zf.close()
src.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment