Create "Zip" file in Model Builder

877
24
Jump to solution
12-09-2021 06:38 AM
Labels (1)
MatthewSyvertson
Occasional Contributor II

I have a model builder process that will create a shapefile, that later is retrieved by another process.  The other process requires a zip file input.  Is there any way I can zip up these files within model builder?

0 Kudos
2 Solutions

Accepted Solutions
curtvprice
MVP Esteemed Contributor

zipshape.png

UPDATE: added check to loop to avoid trying to write the zipfile into itself and causing an infinite loop (!) (discussed downthread)  
UPDATE: this function requires the two inputs folder (path to the shapefile) and name (shapefilename, for example if your shapefile is foo.shp, the name should be foo (without the extension)

 

 

 

 

import os
import glob
import zipfile
def zipshape(folder, name):
    os.chdir(folder)
    zipname = name + ".zip"
    with zipfile.ZipFile(zipname, "w") as newzip:
        for ff in glob.glob(name + ".*"):
            if ff[-4:] != "lock" and ff != zipname: 
                newzip.write(ff)
    return os.path.join(folder,zipname)

 

 

 

 

 

View solution in original post

curtvprice
MVP Esteemed Contributor

Improved, skipping the zip to avoid trying to write it into itself:

 

import os
import glob
import zipfile
def zipshape(folder, name):
    os.chdir(folder)
    zipname = name + ".zip"
    flist = glob.glob(name + ".*")
    with zipfile.ZipFile(zipname, "w") as newzip:
        for ff in flist:
            if ff[-4:] != "lock" and ff != zipname: 
                newzip.write(ff)
    return os.path.join(folder, zipname)

 

 

View solution in original post

24 Replies
ABishop
Regular Contributor II

Hello Matthew,

I have a similar process but it is handled using a combination of a python script to prepare the data and then a batch file script to zip the contents of the folder.  I can share it with you if you like.

Amanda Bishop, GISP
0 Kudos
curtvprice
MVP Esteemed Contributor

You can do this using the Calculate Value tool, with a python function that uses glob and  zipfile. You may want to run Parse Path first to get the info you need to create the zip (folder, shapefile root name).

curtvprice
MVP Esteemed Contributor

zipshape.png

UPDATE: added check to loop to avoid trying to write the zipfile into itself and causing an infinite loop (!) (discussed downthread)  
UPDATE: this function requires the two inputs folder (path to the shapefile) and name (shapefilename, for example if your shapefile is foo.shp, the name should be foo (without the extension)

 

 

 

 

import os
import glob
import zipfile
def zipshape(folder, name):
    os.chdir(folder)
    zipname = name + ".zip"
    with zipfile.ZipFile(zipname, "w") as newzip:
        for ff in glob.glob(name + ".*"):
            if ff[-4:] != "lock" and ff != zipname: 
                newzip.write(ff)
    return os.path.join(folder,zipname)

 

 

 

 

 

MatthewSyvertson
Occasional Contributor II

I am not yet great at python, so simple is better.  If I already know what the full path and shapefile name is always going to be, how would this look simplified?

ABishop
Regular Contributor II

See below for the batch file script that I use to zip up the files. You will have to download 7Zip software to make it work.  Put this in a text file in notepad and then save it as a .bat.  I run it in Windows Task scheduler behind the script that prepares the text files.

 

:: Zip TxtFiles in DATA\TEXT folder
@echo ON

SET SourceDir=K:\GIS_UPDATE\DATA\TXT*.txt
SET DestDir=K:\GIS_UPDATE\DATA\TXT\MCPAEXTRACT.zip

CD /D "C:\Program Files\7-Zip"
7z.exe a "%DestDir%" "%SourceDir%"

Amanda Bishop, GISP
MatthewSyvertson
Occasional Contributor II

I want to stay in model builder, but if the other solution does not work, I will try this one.  Thank you for your contribution.  I am sure it will benefit someone, whether its me or another down the road.

curtvprice
MVP Esteemed Contributor

Honestly I can't think of an simpler way to do this.

This is a plug and play python function, you don't need to edit it at all, just create a calculate value tool and paste the code in the code block, and make sure the variables (inside the %) match the model variables that have your folder and shapefile name. The function doesn't care as long as you give it the correct folder and shapefile root name, which is easily found with Parse Path. I suppose you could enter your folder and shapefile name directly in the Calculate Value tool expression in place the model variables...but Parse Path makes it pretty easy to avoid having to hard-code it.

MatthewSyvertson
Occasional Contributor II

OK.  I will try it and let you know how it goes.

ABishop
Regular Contributor II

Let me know if you try my method.  I have been doing this same workflow for over 3 years with no issues.

Amanda Bishop, GISP
0 Kudos