如何在Python中建立目錄的zip存檔?

如何在Python中建立目錄結構的zip存檔? html


#1樓

要將壓縮添加到生成的zip文件中,請查看此連接python

您須要更改: git

zip = zipfile.ZipFile('Python.zip', 'w')

github

zip = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)

#2樓

我對Mark Byers給出的代碼進行了一些更改。 若是有空目錄,下面的函數還會添加空目錄。 經過示例能夠更清楚地瞭解添加到zip的路徑是什麼。 ide

#!/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)

上面是一個簡單函數,適用於簡單狀況。 您能夠在個人Gist中找到更優雅的課程: https : //gist.github.com/Eccenux/17526123107ca0ac28e6 函數


#3樓

此功能將遞歸壓縮目錄樹, 壓縮文件,並在存檔中記錄正確的相對文件名。 存檔條目與zip -r output.zip source_dir生成的條目相同。 spa

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樓

您可能想看一下zipfile模塊。 在http://docs.python.org/library/zipfile.html上有文檔。 code

您可能還但願os.walk()爲目錄結構創建索引。 htm


#5樓

正如其餘人指出的那樣,您應該使用zipfile 。 該文檔告訴您可用的功能,但並未真正說明如何使用它們壓縮整個目錄。 我認爲用一些示例代碼來解釋是最簡單的: 遞歸

#!/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()

改編自: http : //www.devshed.com/c/a/Python/Python-UnZipped/

相關文章
相關標籤/搜索