A Script Tool for Unzipping Files in ArcGIS

I was recently working with the SWReGAP data DVDs and wanted to develop an ArcGIS geoprocessing model to copy the DVD data, clip the rasters to the state of Arizona boundary and reproject them.

Because the DVDs contained a raster dataset for each species that was stored as a zip file, I wanted a way to decompress each raster within my model rather than simply decompressing all the zip files to a folder on my hard drive and using the uncompressed files in my model. (This is because each DVD was filled to capacity with 2+ GB zip files, so decompressing the DVD contents to my hard disk first filled up the remaining space on my hard drive. So I decided that I should decompress each file individually and delete it when I was finished.)

So I set about developing a script tool to decompress each zip file so that I could incorporate the script tool into my ArcGIS geoprocessing model. I tried using the zipfile libraries built into Python, but ran into problems decompressing these files because each zip file was too large for the readfile buffer used by the Python routines.

This led me to 7-Zip, an open source to WinZip. While 7-Zip contains a user interface, I was particularly interested in the 7-Zip Command Line Version as this made it possible for me to call the command line from my ArcGIS script too.

With the 7-Zip Command Line Version installed and a few lines of Python, I had a script tool that was capable of extracting a zip file and could be included in an ArcGIS geoprocessing model. I then developed a second script to decompress a whole folder of zip files, and a third script for zipping a feature class.

import os
import arcpy
infile = arcpy.GetParameterAsText(0)
outpath = arcpy.GetParameterAsText(1)

# create 7zip command
zipcommand = “7za e -tzip \”%s\” -o\”%s\”” % (infile, outpath)

try:
      # execute command
os.system(zipcommand)
arcpy.AddMessage(“Finished”)
except Exception as e:
      print e.message
arcpy.AddError(e.message)

I then added the script to a toolbox in ArcGIS as a script tool and set two parameters: 1) Zipfile Name as a file, and 2) Output Location as a folder. When clicked, the script tool appears like the one below:

I was pleased enough with the resulting tool that I developed a simple model that incorporated the tool into an iterator to loop through a folder of zip files and extract each one. That model is illustrated below:

Finally, I also created a second script tool that decompresses an entire folder of zipfiles to a specified location.

Since these have been helpful in automating my workflow I thought I would offer them to the GIS community as a zip file that contains a folder with an ArcGIS Toolbox, two script tools and a sample mode. You can download them from my website at: http://www.johndonoghue.net/ecology/resources.html

To use the ArcGIS Script tools:

There are a couple of things you need to do before the tools will work. Follow the simple steps below to obtain a copy of 7-Zip Command Line Version and modify your system paths.

  • Download and install 7-Zip Command Line Version. When you download the application it will consist of a zip file. decompress the zip file to a folder on your hard disk. I highly recommend extracting the files to a folder in your root directory named something like C:\7Zip or the default C:\7za920. If you extract them into a folder inside your program files folder on 64-bit systems, you may not be able to run the scripts as Python cannot find the executable even with the path statement.
  • Add the path to your 7-Zip installation to your Windows PATH environment variables – add it to both the System PATH variable and the User PATH variable . For example, my path was: C:\7za920; (see http://www.computerhope.com/issues/ch000549.htm if you need help doing this).
  • Finally,download the  Tools from my website and extract them to a folder on your hard drive. You should then be able to use them as you would any ArcGIS script .

I hope you find these tools helpful in your work.

Advertisements

5 thoughts on “A Script Tool for Unzipping Files in ArcGIS

  1. Did you consider using the python zipfile module which is installed as part of python?

    import zipfile

    def extract(zipfilepath, extractiondir):
    zip = zipfile.ZipFile(zipfilepath)
    zip.extractall(path=extractiondir

  2. Thanks for your comment. I did try the zipfile python module but experienced problems. The file sizes I were dealing with were large, with zipfiles often exceeding 2 GB each. In this case, the extractall function took an enormous amount of time to extract the same files that 7-Zip could extract in under a minute. So I moved to the 7-zip implementation as it was much faster.

  3. In a more detailed comment, I will run through some of the
    reasoning behind our choices. Quality reasoning here.
    I really do benefit from going through your posts.
    I learned a vast amount from them.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s