Create "Zip" file in Model Builder

4270
24
Jump to solution
12-09-2021 06:38 AM
Syvertson
Occasional Contributor III

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
24 Replies
Syvertson
Occasional Contributor III

The process "calculate values" seems to have created the zip file just fine, but the model builder is stuck on the step.  The zip file was created and has not changed file size for about 20 minutes.  See screen shot:

MatthewSyvertson_0-1639148893340.png

I can't understand what would hang it up.

0 Kudos
Syvertson
Occasional Contributor III

Update to this:  I force killed the process through task manager, and the file size had grown huge, and it was an invalid file when I tried to open.  See screen shot:

MatthewSyvertson_0-1639149509376.png

 

curtvprice
MVP Esteemed Contributor

That's really weird as it worked for me - but my test model didn't create the shapefile, I gave it a path to an existing one. Too bad the zip isn't valid as I can't imagine what it was trying to stuff in there. Thanks for the really detailed screenshot, I will look at the puzzle.

0 Kudos
curtvprice
MVP Esteemed Contributor

I've got an an idea for a small code change, modify the code I posted, try changing

for ff in glob.glob(name + ".*"):

to

for ff in glob.glob(name + "*"):

0 Kudos
Syvertson
Occasional Contributor III

OK.  Maybe in this I can solve another problem.  Can I specify a different folder for shapefile that needs to be zipped and the output zip location?

0 Kudos
Syvertson
Occasional Contributor III

I attempted this change to:  "for ff in glob.glob(name + "*"):"

and I had the same result.

 

Could this be stuffing the zipfile that it is creating into the zipfile that it is creating, creating a loop of sorts?

0 Kudos
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)

 

 

Syvertson
Occasional Contributor III

This worked perfectly.  Thanks so much for helping me with this!

0 Kudos
curtvprice
MVP Esteemed Contributor

That's great! Could you mark the first (updated) post with the screenshot of the test model as the solution? I think the post with the ModelBuilder model would be most useful to people looking for an answer to this question.

ABishop
MVP Regular Contributor

Good job!

Amanda Bishop, GISP
0 Kudos