<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Problem unzipping .kmz file from arcgis service request in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/problem-unzipping-kmz-file-from-arcgis-service/m-p/514794#M40411</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am having trouble unzipping a kmz file returned from a service request.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example of a service request:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer/findAddressCandidates?Address=801+lake+creek+dr&amp;amp;City=&amp;amp;State=&amp;amp;Zip=75070&amp;amp;outFields=x%2Cy%2Cscore&amp;amp;outSR=&amp;amp;f=kmz" rel="nofollow noopener noreferrer" target="_blank"&gt;http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer/findAddressCandidates?Address=801+lake+creek+dr&amp;amp;City=&amp;amp;State=&amp;amp;Zip=75070&amp;amp;outFields=x%2Cy%2Cscore&amp;amp;outSR=&amp;amp;f=kmz&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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).&amp;nbsp; I've tried both gzip and zlib.&amp;nbsp; My sample code is below.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas what I may be doing wrong?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;--Jason&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#sample program
import zlib,sys,os,gzip

input = sys.argv[1]
output = '%s.decomp' % input
if os.path.exists( output ):
&amp;nbsp;&amp;nbsp;&amp;nbsp; 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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; output_data = zlib.decompress( contents )
&amp;nbsp;&amp;nbsp;&amp;nbsp; fail = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Decompression with zip succeeded'
except Exception,e:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Decompression error (zip): %s' % e
&amp;nbsp;&amp;nbsp;&amp;nbsp; fail = 1
if not fail:
&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.write( output_data )
outfile.close()
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
if fail:
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.unlink( output )
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Zip failed. Try gzip
&amp;nbsp;&amp;nbsp;&amp;nbsp; gz = gzip.open( input, 'rb' )
&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile = open( output, 'wb' )
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.writelines( gz )
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fail = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Decompression with gzip succeeded'
&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception,e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Decompression error (gzip): %s' % e
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fail = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; gz.close()
&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.close()

if fail and os.path.exists( output ):
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.unlink( output )
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Decompressed file %s created' % output
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 22:29:53 GMT</pubDate>
    <dc:creator>JasonHinsperger</dc:creator>
    <dc:date>2021-12-11T22:29:53Z</dc:date>
    <item>
      <title>Problem unzipping .kmz file from arcgis service request</title>
      <link>https://community.esri.com/t5/python-questions/problem-unzipping-kmz-file-from-arcgis-service/m-p/514794#M40411</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am having trouble unzipping a kmz file returned from a service request.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example of a service request:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer/findAddressCandidates?Address=801+lake+creek+dr&amp;amp;City=&amp;amp;State=&amp;amp;Zip=75070&amp;amp;outFields=x%2Cy%2Cscore&amp;amp;outSR=&amp;amp;f=kmz" rel="nofollow noopener noreferrer" target="_blank"&gt;http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer/findAddressCandidates?Address=801+lake+creek+dr&amp;amp;City=&amp;amp;State=&amp;amp;Zip=75070&amp;amp;outFields=x%2Cy%2Cscore&amp;amp;outSR=&amp;amp;f=kmz&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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).&amp;nbsp; I've tried both gzip and zlib.&amp;nbsp; My sample code is below.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas what I may be doing wrong?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;--Jason&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#sample program
import zlib,sys,os,gzip

input = sys.argv[1]
output = '%s.decomp' % input
if os.path.exists( output ):
&amp;nbsp;&amp;nbsp;&amp;nbsp; 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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; output_data = zlib.decompress( contents )
&amp;nbsp;&amp;nbsp;&amp;nbsp; fail = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Decompression with zip succeeded'
except Exception,e:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Decompression error (zip): %s' % e
&amp;nbsp;&amp;nbsp;&amp;nbsp; fail = 1
if not fail:
&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.write( output_data )
outfile.close()
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
if fail:
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.unlink( output )
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Zip failed. Try gzip
&amp;nbsp;&amp;nbsp;&amp;nbsp; gz = gzip.open( input, 'rb' )
&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile = open( output, 'wb' )
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.writelines( gz )
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fail = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Decompression with gzip succeeded'
&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception,e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Decompression error (gzip): %s' % e
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fail = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; gz.close()
&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.close()

if fail and os.path.exists( output ):
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.unlink( output )
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Decompressed file %s created' % output
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:29:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-unzipping-kmz-file-from-arcgis-service/m-p/514794#M40411</guid>
      <dc:creator>JasonHinsperger</dc:creator>
      <dc:date>2021-12-11T22:29:53Z</dc:date>
    </item>
  </channel>
</rss>

