jasonhinsperger

Problem unzipping .kmz file from arcgis service request

Discussion created by jasonhinsperger on Mar 8, 2012
I am having trouble unzipping a kmz file returned from a service request.

Here is an example of a service request:
http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer/findAddressCandidates?Address=801+lake+creek+dr&City=&State=&Zip=75070&outFields=x%2Cy%2Cscore&outSR=&f=kmz

I can unzip the resulting file from the above request using the winzip/winrar tools, but using the standard python zip modules fails with a data corruption error (same file in both cases).  I've tried both gzip and zlib.  My sample code is below.

Any ideas what I may be doing wrong?

Thanks,

--Jason

#sample program
import zlib,sys,os,gzip

input = sys.argv[1]
output = '%s.decomp' % input
if os.path.exists( output ):
    os.unlink( output )

# Try zip first
insize = os.stat( input ).st_size
infile = open( input, 'rb' )
contents = infile.read( insize )
infile.close()

outfile = open( output, 'wb' )
try:
    output_data = zlib.decompress( contents )
    fail = 0
    print 'Decompression with zip succeeded'
except Exception,e:
    print 'Decompression error (zip): %s' % e
    fail = 1
if not fail:
    outfile.write( output_data )
outfile.close()
    
if fail:
    os.unlink( output )
    # Zip failed. Try gzip
    gz = gzip.open( input, 'rb' )
    outfile = open( output, 'wb' )
    try:
        outfile.writelines( gz )
        fail = 0
        print 'Decompression with gzip succeeded'
    except Exception,e:
        print 'Decompression error (gzip): %s' % e
        fail = 1
    gz.close()
    outfile.close()

if fail and os.path.exists( output ):
    os.unlink( output )
else:
    print 'Decompressed file %s created' % output

Outcomes