Ever since the ever-popular post, Zipping a shapefile using python, came out, people have been asking (one person, yesterday) for a sample of how to zip a file geodatabase using python.
The key trick, as shown in line 17, is appending the basename of the file geodatabase (“nfg.gdb/” in my example) in front of each file as you write it to the zipfile.
UPDATE: WordPress messes with the spacing when I post code, making it difficult to post code that can just be copied & pasted and have work. So I have posted a the code HERE for downloading.
import os import zipfile import glob infile = "c:/temp/nfg.gdb" outfile = "c:/temp/nfg.zip" def zipFileGeodatabase(inFileGeodatabase, newZipFN): if not (os.path.exists(inFileGeodatabase)): return False if (os.path.exists(newZipFN)): os.remove(newZipFN) zipobj = zipfile.ZipFile(newZipFN,'w') for infile in glob.glob(inFileGeodatabase+"/*"): zipobj.write(infile, os.path.basename(inFileGeodatabase)+"/"+os.path.basename(infile), zipfile.ZIP_DEFLATED) print ("Zipping: "+infile) zipobj.close() return True zipFileGeodatabase(infile,outfile)