Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os
  3. import zipfile
  4.  
  5. def zipdir(path, zip):
  6. for root, dirs, files in os.walk(path):
  7. for file in files:
  8. zip.write(os.path.join(root, file))
  9.  
  10. if __name__ == '__main__':
  11. zipf = zipfile.ZipFile('Python.zip', 'w')
  12. zipdir('tmp/', zipf)
  13. zipf.close()
  14.  
  15. import os
  16. import zipfile
  17.  
  18. zf = zipfile.ZipFile("myzipfile.zip", "w")
  19. for dirname, subdirs, files in os.walk("mydirectory"):
  20. zf.write(dirname)
  21. for filename in files:
  22. zf.write(os.path.join(dirname, filename))
  23. zf.close()
  24.  
  25. def make_zipfile(output_filename, source_dir):
  26. relroot = os.path.abspath(os.path.join(source_dir, os.pardir))
  27. with zipfile.ZipFile(output_filename, "w", zipfile.ZIP_DEFLATED) as zip:
  28. for root, dirs, files in os.walk(source_dir):
  29. # add directory (needed for empty dirs)
  30. zip.write(root, os.path.relpath(root, relroot))
  31. for file in files:
  32. filename = os.path.join(root, file)
  33. if os.path.isfile(filename): # regular files only
  34. arcname = os.path.join(os.path.relpath(root, relroot), file)
  35. zip.write(filename, arcname)
  36.  
  37. #!/usr/bin/env python
  38. import os
  39. import zipfile
  40.  
  41. def addDirToZip(zipHandle, path, basePath=""):
  42. """
  43. Adding directory given by a path to opened zip file a zipHandle
  44.  
  45. @param basePath path that will be removed from a path when adding to archive
  46.  
  47. Examples:
  48. # add whole "dir" to "test.zip" (when you open "test.zip" you will see only "dir")
  49. zipHandle = zipfile.ZipFile('test.zip', 'w')
  50. addDirToZip(zipHandle, 'dir')
  51. zipHandle.close()
  52.  
  53. # add contents of "dir" to "test.zip" (when you open "test.zip" you will see only it's contents)
  54. zipHandle = zipfile.ZipFile('test.zip', 'w')
  55. addDirToZip(zipHandle, 'dir', 'dir')
  56. zipHandle.close()
  57.  
  58. # add contents of "dir/subdir" to "test.zip" (when you open "test.zip" you will see only contents of "subdir")
  59. zipHandle = zipfile.ZipFile('test.zip', 'w')
  60. addDirToZip(zipHandle, 'dir/subdir', 'dir/subdir')
  61. zipHandle.close()
  62.  
  63. # add whole "dir/subdir" to "test.zip" (when you open "test.zip" you will see only "subdir")
  64. zipHandle = zipfile.ZipFile('test.zip', 'w')
  65. addDirToZip(zipHandle, 'dir/subdir', 'dir')
  66. zipHandle.close()
  67.  
  68. # 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")
  69. zipHandle = zipfile.ZipFile('test.zip', 'w')
  70. addDirToZip(zipHandle, 'dir/subdir')
  71. zipHandle.close()
  72.  
  73. # add whole "dir" and "otherDir" (with full path) to "test.zip" (when you open "test.zip" you will see only "dir" and "otherDir")
  74. zipHandle = zipfile.ZipFile('test.zip', 'w')
  75. addDirToZip(zipHandle, 'dir')
  76. addDirToZip(zipHandle, 'otherDir')
  77. zipHandle.close()
  78. """
  79. basePath = basePath.rstrip("\/") + ""
  80. basePath = basePath.rstrip("\/")
  81. for root, dirs, files in os.walk(path):
  82. # add dir itself (needed for empty dirs
  83. zipHandle.write(os.path.join(root, "."))
  84. # add files
  85. for file in files:
  86. filePath = os.path.join(root, file)
  87. inZipPath = filePath.replace(basePath, "", 1).lstrip("\/")
  88. #print filePath + " , " + inZipPath
  89. zipHandle.write(filePath, inZipPath)
  90.  
  91. def WriteDirectoryToZipFile( zipHandle, srcPath, zipLocalPath = "", zipOperation = zipfile.ZIP_DEFLATED ):
  92. basePath = os.path.split( srcPath )[ 0 ]
  93. for root, dirs, files in os.walk( srcPath ):
  94. p = os.path.join( zipLocalPath, root [ ( len( basePath ) + 1 ) : ] )
  95. # add dir
  96. zipHandle.write( root, p, zipOperation )
  97. # add files
  98. for f in files:
  99. filePath = os.path.join( root, f )
  100. fileInZipPath = os.path.join( p, f )
  101. zipHandle.write( filePath, fileInZipPath, zipOperation )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement