Select to view content in your preferred language

Zip a file geodatabase using ArcPy or Zipfile library

11444
14
Jump to solution
11-02-2018 11:19 AM
MKF62
by
Frequent Contributor

Does anybody have a good method of doing this?

I've tried using the zipfile python library but am utterly confused on how to do it for a file geodatabase since it's not exactly a directory/file structure. My folder structure looks like so, I want to zip up the folder/geodatabase outlined in red:

I've tried this (all the indentation is correct in my script, don't know why GeoNet is changing it upon publishing the question):

import zipfile

#Creates the empty zip file and opens it for writing
myzipfile = zipfile.ZipFile("D:\GIS_Testing\HabitatDbase\MyZip.zip", 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk("D:\GIS_Testing\HabitatDbase"):
 if root == "D:\GIS_Testing\HabitatDbase\HabitatData.gdb":
 for f in files:
 myzipfile.write(os.path.join(root, file))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I've also tried using shutil.make_archive and for some reason instead of zipping up my file geodatabase it zips up my python script file which is completely bewildering as I have no idea how it would even get the path to the script file...

I get a TypeError on the last line that says "object of type 'type' has no len()"

Tags (2)
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Should it be 'f' rather than 'file' in the last line?

View solution in original post

14 Replies
DarrenWiens2
MVP Honored Contributor

Should it be 'f' rather than 'file' in the last line?

MKF62
by
Frequent Contributor

Hahaha, seriously, of course it's just a typo. Thank you!! I'll leave the thread up because I couldn't find any good examples on how to zip up a file geodatabase in python on GeoNet. Maybe it'll help someone later...

DanPatterson_Retired
MVP Emeritus

Michelle Mathias‌ code mangling still today... it is going to get ugly until they fix it

JoshuaBixby
MVP Esteemed Contributor

Agreed, now that the mangling is affecting discussions in addition to blogs, the impacts will be much more significant.

MichelleMathias
Esri Community Manager

I just put some pressure on the vendor to get us a fix soon. 

Michelle Mathias
Manager, Community Experience & Programs
AaronKoelker
Frequent Contributor

This is one of the only examples I can find of zipping up a .gdb with python, however when I try to use this method,(accounting for the typo in the last line) my new zip file contains the entire folder structure leading up to the .gdb. Anyone have an idea of how to just zip up the .gdb file?

-Aaron
0 Kudos
AaronKoelker
Frequent Contributor

As usual, seconds after posting a question -- I get it to work. However I used shutil.make_archive instead of the above ZipFile method.

shutil.make_archive(zipfilename, 'zip', fgdb)‍‍‍‍‍‍

  • zipfilename = filename of your new zip, plus the path ("D:/MyFolder/ExampleFolder/thenameofmynewzip"), leave off the .zip file extension
  • fgdb = the file geodatabase you want to zip ("D:/MyFolder/ExampleFolder/MyFGDB.gdb")

EDIT: Actually, while the above code technically works, it won't be a good solution if you're trying to zip up the fgdb for uploading to ArcGIS Online. Instead, have fgdb point to the folder that contains the MyFGDB.gdb file, rather than the .gdb file itself. Otherwise you'll get an error when uploading to AGO. You'll also want separate the the new zipfile and the fgdb into their own folders, or add code to grab the fgdb specifically (similar to what the original post is doing). If not, you'll get an empty zip within your zip since it creates the new zip file first before adding in your .gdb, looks like. I'm sure someone else has a more elegant solution to this bit.

-Aaron
VinceAngelo
Esri Esteemed Contributor

The '.gdb' of a file geodatabase is a directory, not a file. The file geodatabase is the directory and all the files below it (except the lock files -- you can keep those out).

- V

DAVIDWADSWORTH_LVVWD
Occasional Contributor

Thanks for adding this key distinction. Saved me a lot of time.

0 Kudos