How to create a zip archive of directory structure in Python?
#1 building
To add compression to the generated zip file, see This link .
You need to change:
zip = zipfile.ZipFile('Python.zip', 'w')
to
zip = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
#2 building
I am right. Code from Mark Byers Some changes have been made. If there is an empty directory, the following function also adds an empty directory. You can see more clearly what the path to add to zip is through the example.
#!/usr/bin/env python import os import zipfile def addDirToZip(zipHandle, path, basePath=""): """ Adding directory given by \a path to opened zip file \a zipHandle @param basePath path that will be removed from \a path when adding to archive Examples: # add whole "dir" to "test.zip" (when you open "test.zip" you will see only "dir") zipHandle = zipfile.ZipFile('test.zip', 'w') addDirToZip(zipHandle, 'dir') zipHandle.close() # add contents of "dir" to "test.zip" (when you open "test.zip" you will see only it's contents) zipHandle = zipfile.ZipFile('test.zip', 'w') addDirToZip(zipHandle, 'dir', 'dir') zipHandle.close() # add contents of "dir/subdir" to "test.zip" (when you open "test.zip" you will see only contents of "subdir") zipHandle = zipfile.ZipFile('test.zip', 'w') addDirToZip(zipHandle, 'dir/subdir', 'dir/subdir') zipHandle.close() # add whole "dir/subdir" to "test.zip" (when you open "test.zip" you will see only "subdir") zipHandle = zipfile.ZipFile('test.zip', 'w') addDirToZip(zipHandle, 'dir/subdir', 'dir') zipHandle.close() # add whole "dir/subdir" with full path to "test.zip" (when you open "test.zip" you will see only "dir" and inside it only "subdir") zipHandle = zipfile.ZipFile('test.zip', 'w') addDirToZip(zipHandle, 'dir/subdir') zipHandle.close() # add whole "dir" and "otherDir" (with full path) to "test.zip" (when you open "test.zip" you will see only "dir" and "otherDir") zipHandle = zipfile.ZipFile('test.zip', 'w') addDirToZip(zipHandle, 'dir') addDirToZip(zipHandle, 'otherDir') zipHandle.close() """ basePath = basePath.rstrip("\\/") + "" basePath = basePath.rstrip("\\/") for root, dirs, files in os.walk(path): # add dir itself (needed for empty dirs zipHandle.write(os.path.join(root, ".")) # add files for file in files: filePath = os.path.join(root, file) inZipPath = filePath.replace(basePath, "", 1).lstrip("\\/") #print filePath + " , " + inZipPath zipHandle.write(filePath, inZipPath)
The above is a simple function for simple cases. You can find more elegant courses in my Gist: https : //gist.github.com/Eccenux/17526123107ca0ac28e6
#3 building
This function will recursively compress the directory tree, compress the file, and record the correct relative file name in the archive. The archive entry is the same as the one generated by zip - R output. Zip source? Dir.
import os import zipfile def make_zipfile(output_filename, source_dir): relroot = os.path.abspath(os.path.join(source_dir, os.pardir)) with zipfile.ZipFile(output_filename, "w", zipfile.ZIP_DEFLATED) as zip: for root, dirs, files in os.walk(source_dir): # add directory (needed for empty dirs) zip.write(root, os.path.relpath(root, relroot)) for file in files: filename = os.path.join(root, file) if os.path.isfile(filename): # regular files only arcname = os.path.join(os.path.relpath(root, relroot), file) zip.write(filename, arcname)
#4 building
You may want to take a look at the zipfile module. stay http://docs.python.org/library/zipfile.html There are documents.
You may also want os.walk() to index the directory structure.
#5 building
As others have pointed out, you should use the zipfile . This document tells you what features are available, but it doesn't really show you how to use them to compress the entire catalog. I think it's easiest to explain with some sample code:
#!/usr/bin/env python import os import zipfile def zipdir(path, ziph): # ziph is zipfile handle for root, dirs, files in os.walk(path): for file in files: ziph.write(os.path.join(root, file)) if __name__ == '__main__': zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) zipdir('tmp/', zipf) zipf.close()
Adapted from: http : //www.devshed.com/c/a/Python/Python-UnZipped/