Unzip in Python Script

29950
10
Jump to solution
02-16-2011 08:57 AM
DarrenClay
New Contributor
I am trying to unzip a Zip file in a Windows directory.  Does anyone know the commands to do this. 

Thanks in advance.

the Zipfile library is too confusing for me.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
import zipfile

zip = zipfile.ZipFile(r'c:\my.zip')
zip.extractall(r'c:\output')

This will extract the contents of my.zip to c:\output

View solution in original post

10 Replies
JasonScheirer
Occasional Contributor III
import zipfile

zip = zipfile.ZipFile(r'c:\my.zip')
zip.extractall(r'c:\output')

This will extract the contents of my.zip to c:\output
JasonScheirer
Occasional Contributor III
def zip_directory_to_zipfile(directory, out_zipfile):

    out_zip = zipfile.ZipFile(out_zipfile, 'w')

    for (dirpath, dirnames, filenames) in os.walk(directory):
        for file in filenames:
            path_in_zip = os.path.join(os.path.relpath(dirpath, 
                                                lib_dir), 
                                                file)
            if os.path.split(path_in_zip)[0] == ".":
                path_in_zip = os.path.split(path_in_zip)[1]
            out_zip.write(os.path.join(dirpath, file), path_in_zip)
JasonScheirer
Occasional Contributor III
I think you are onto somthing, but I need a little help integrating it into the arcpy module.  What would it be like if we know that the dirpath is the arcpy.env.workspace is the current one.  Then we want to make everything in that dirpath zipped up.  Would it make the script shorter somehow?


Not really.

def zip_directory_to_zipfile(out_zipfile):

    out_zip = zipfile.ZipFile(out_zipfile, 'w')
 
    for (dirpath, dirnames, filenames) in os.walk(arcpy.env.workspace):
        for file in filenames:
            path_in_zip = os.path.join(os.path.relpath(dirpath, 
                                                lib_dir), 
                                                file)
            if os.path.split(path_in_zip)[0] == ".":
                path_in_zip = os.path.split(path_in_zip)[1]
            out_zip.write(os.path.join(dirpath, file), path_in_zip)
0 Kudos
JasonScheirer
Occasional Contributor III
If that's what you want, then a .zip file is not the appropriate distribution mechanism -- workspaces can be more than a directory, such as an .sde file or file geodatabase, and a dataset living in any non-directory workspace would not be included in the zip. In fact, it would be hard to even include .shp files in the directory tree because ListDatasets would return each one without the .shp extension.

For what you want, use a Map or Layer package from desktop to consolidate your data.
0 Kudos
JasonScheirer
Occasional Contributor III
The tool documentation includes examples.
0 Kudos
ScottBlankenbeckler
Occasional Contributor
Jason,

I am using your code to zip up a single file in a directory. However the zip file and the original file(*.txt) are the same size (i.e. 0% compression) what would cause this and what do I need to look for to correct it.
0 Kudos
JasonScheirer
Occasional Contributor III
Try this:

out_zip = zipfile.ZipFile(out_zipfile, 'w', zipfile.ZIP_DEFLATED)
0 Kudos
ScottBlankenbeckler
Occasional Contributor
That fixed it. Much thanks.

New problem.

Any way to set it up so that it creates a split zip file of 5MB max size.
i.e. the original file is just over 10.7MB using the code above but my email server will only allow a 10MB file. If I can split the file into a split zip of 5MB parts I can send it as two emails.
0 Kudos
meriyalootka
New Contributor III
That fixed it. Much thanks.

New problem.

Any way to set it up so that it creates a split zip file of 5MB max size.
i.e. the original file is just over 10.7MB using the code above but my email server will only allow a 10MB file. If I can split the file into a split zip of 5MB parts I can send it as two emails.


I have a simple model for Unzip a zip folder and get it's features. But I can not get extracted features. What is wrong in my model?
0 Kudos