Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import os
- import zipfile
- def zipdir(path, zip):
- for root, dirs, files in os.walk(path):
- for file in files:
- zip.write(os.path.join(root, file))
- if __name__ == '__main__':
- zipf = zipfile.ZipFile('Python.zip', 'w')
- zipdir('tmp/', zipf)
- zipf.close()
- import os
- import zipfile
- zf = zipfile.ZipFile("myzipfile.zip", "w")
- for dirname, subdirs, files in os.walk("mydirectory"):
- zf.write(dirname)
- for filename in files:
- zf.write(os.path.join(dirname, filename))
- zf.close()
- 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)
- #!/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)
- def WriteDirectoryToZipFile( zipHandle, srcPath, zipLocalPath = "", zipOperation = zipfile.ZIP_DEFLATED ):
- basePath = os.path.split( srcPath )[ 0 ]
- for root, dirs, files in os.walk( srcPath ):
- p = os.path.join( zipLocalPath, root [ ( len( basePath ) + 1 ) : ] )
- # add dir
- zipHandle.write( root, p, zipOperation )
- # add files
- for f in files:
- filePath = os.path.join( root, f )
- fileInZipPath = os.path.join( p, f )
- zipHandle.write( filePath, fileInZipPath, zipOperation )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement