Select to view content in your preferred language

Zip a file geodatabase using ArcPy or Zipfile library

11451
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)
14 Replies
tigerwoulds
Frequent Contributor

Old post but figured I'd post my solution incase it helps anyone else. This function will zip the geodatabase and skip any lock files. I was trying to implement the zip in a larger codebase and kept running into locks.

def zipGDB(inputGDB):
gdbFile = str(inputGDB)
outFile = gdbFile[0:-4] + '.zip'
gdbName = os.path.basename(gdbFile)
with zipfile.ZipFile(outFile, mode='w', compression=zipfile.ZIP_DEFLATED, allowZip64=True) as myzip:
for f in os.listdir(gdbFile):
if f[-5:] != '.lock':
myzip.write(os.path.join(gdbFile, f), gdbName + '\\' + os.path.basename(f))
print('Completed zipping: {}'.format(gdbFile))

 

DAVIDWADSWORTH_LVVWD
Occasional Contributor

Thanks for sharing.

0 Kudos
danashney
Regular Contributor

Since this took me much longer to figure out than I care to admit:

The sample provided by TigerWoulds results in a copy of the inputGDB within a zipped folder of the same name. If you want to create a zipped copy without that nesting then remove

gdbName + '\\' + 

from the line

myzip.write(os.path.join(gdbFile, f), gdbName + '\\' + os.path.basename(f))
0 Kudos
patrick_shp
Emerging Contributor

Reviving an older thread, but there aren't too many helpful resources online about this topic...

I had issues utilizing tigerwoulds' script, so I tweaked the function a bit:

 
zip_geodatabase(input_geodatabase, output_zip)
def zip_geodatabase(input_geodatabase, output_zip):
# Create a zip file
with zipfile.ZipFile(output_zip, 'w') as zipf:
# Iterate over all files in the geodatabase
for root, dirs, files in os.walk(input_geodatabase):
for file in files:
# Add each file to the zip file
file_path = os.path.join(root, file)
if file[-5:] != '.lock':
zipf.write(file_path, os.path.relpath(file_path, input_geodatabase))

#input_geodatabase = r"C:\Users\myfile.gdb"
#output_zip = r"C:\Users\myoutputfile.zip"
#zip_geodatabase(input_geodatabase, output_zip)

 

0 Kudos
patrick_shp
Emerging Contributor

Slight addendum:

You can't zip the .gdb file directly and upload to portal using this script. It must first be added to a subfolder by itself. So that means that input_geodatabase points to something like r"C:\Users\parent_folder", where "parent_folder" contains only the .gdb file. Hope that makes sense!

0 Kudos