Commits

Rafał Kos  committed 46443cb

Added zipdir script

  • Participants
  • Parent commits 3147fce

Comments (0)

Files changed (1)

+import os
+import zipfile
+
+def zipdir(dirPath=None, zipFilePath=None, includeDirInZip=True):
+    """
+    zipdir("foo") #Just give it a dir and get a .zip file
+    zipdir("foo", "foo2.zip") #Get a .zip file with a specific file name
+    zipdir("foo", "foo3nodir.zip", False) #Omit the top level directory
+    zipdir("../test1/foo", "foo4nopardirs.zip")
+    """
+    if not zipFilePath:
+        zipFilePath = dirPath + ".zip"
+    if not os.path.isdir(dirPath):
+        raise OSError("dirPath argument must point to a directory. "
+            "'%s' does not." % dirPath)
+    parentDir, dirToZip = os.path.split(dirPath)
+    #Little nested function to prepare the proper archive path
+    def trimPath(path):
+        archivePath = path.replace(parentDir, "", 1)
+        if parentDir:
+            archivePath = archivePath.replace(os.path.sep, "", 1)
+        if not includeDirInZip:
+            archivePath = archivePath.replace(dirToZip + os.path.sep, "", 1)
+        return os.path.normcase(archivePath)
+
+    outFile = zipfile.ZipFile(zipFilePath, "w",
+        compression=zipfile.ZIP_DEFLATED)
+    for (archiveDirPath, dirNames, fileNames) in os.walk(dirPath):
+        for fileName in fileNames:
+            filePath = os.path.join(archiveDirPath, fileName)
+            outFile.write(filePath, trimPath(filePath))
+        #Make sure we get empty directories as well
+        if not fileNames and not dirNames:
+            zipInfo = zipfile.ZipInfo(trimPath(archiveDirPath) + "/")
+            #some web sites suggest doing
+            #zipInfo.external_attr = 16
+            #or
+            #zipInfo.external_attr = 48
+            #Here to allow for inserting an empty directory.  Still TBD/TODO.
+            outFile.writestr(zipInfo, "")
+    outFile.close()
+
+zipdir(r"d:\My Dropbox\projekty\python\scripts")