如何使用 Python 建立 zip 檔案?
ZIP 是一種用於無損資料壓縮的歸檔檔案格式。可以使用一個或多個目錄或檔案來建立 ZIP 檔案。ZIP 支援多種壓縮演算法,其中 DEFLATE 最為常見。ZIP 檔案的副檔名為 .zip。在本文中,我們將討論如何使用 Python 建立 Zip 檔案。
在 Python 中建立未壓縮的 ZIP 檔案
未壓縮的 ZIP 檔案不會減小原始目錄的大小。由於沒有壓縮,因此與共享原始檔案相比,透過網路共享未壓縮的 ZIP 檔案沒有任何優勢。
使用 shutil.make_archive 建立 Zip 檔案
Python 擁有一個標準庫 shutil,可用於建立未壓縮的 ZIP 檔案。此建立 ZIP 檔案的方法僅應用於將多個檔案組織到一個檔案中。
語法
以下是 shutil.make_archive() 的語法 -
shutil.make_archive('output file name', 'zip', 'directory name')
示例
以下是如何使用 shutil.make_archive() 建立 ZIP 檔案的示例 -
import shutil import os.path # Creating the ZIP file archived = shutil.make_archive('E:/Zipped file', 'zip', 'E:/Folder to be zipped') if os.path.exists('E:/Zipped file.zip'): print(archived) else: print("ZIP file not created")
輸出
以下是上述程式碼的輸出 -
E:\Zipped file.zip
在 Python 中建立壓縮的 ZIP 檔案
壓縮的 ZIP 檔案透過應用壓縮演算法來減小原始目錄的大小。壓縮的 ZIP 檔案導致透過網路更快地共享檔案,因為 ZIP 檔案的大小明顯小於原始檔案。
Python 中的 zipfile 庫 允許使用不同的方法建立壓縮的 ZIP 檔案。
從多個檔案建立 ZIP 檔案
在此方法中,ZipFile() 建立一個 ZIP 檔案,其中添加了要壓縮的檔案。這是透過使用 'with' 關鍵字 建立 ZipFile 物件,然後使用 .write() 方法寫入檔案來實現的。
示例
以下是如何使用多個檔案建立 ZIP 檔案的示例 -
import os from zipfile import ZipFile # Create a ZipFile Object with ZipFile('E:/Zipped file.zip', 'w') as zip_object: # Adding files that need to be zipped zip_object.write('E:/Folder to be zipped/Greetings.txt') zip_object.write('E:/Folder to be zipped/Introduction.txt') # Check to see if the zip file is created if os.path.exists('E:/Zipped file.zip'): print("ZIP file created") else: print("ZIP file not created")
輸出
以下是上述程式碼的輸出 -
ZIP file created
從整個目錄建立 ZIP 檔案
在此方法中,使用 for 迴圈遍歷整個目錄,然後將目錄中存在的所有檔案新增到使用 ZipFile 建立的 ZIP 檔案中。
示例
以下是如何從整個目錄建立 ZIP 檔案的示例 -
import os from zipfile import ZipFile # Create object of ZipFile with ZipFile('E:/Zipped file.zip', 'w') as zip_object: # Traverse all files in directory for folder_name, sub_folders, file_names in os.walk('E:/Folder to be zipped'): for filename in file_names: # Create filepath of files in directory file_path = os.path.join(folder_name, filename) # Add files to zip file zip_object.write(file_path, os.path.basename(file_path)) if os.path.exists('E:/Zipped file.zip'): print("ZIP file created") else: print("ZIP file not created")
輸出
以下是上述程式碼的輸出 -
ZIP file created
從目錄中的特定檔案建立 ZIP 檔案
在此方法中,lambda 函式用於過濾要新增到 ZIP 檔案中的特定副檔名的檔案。lambda 函式作為引數傳遞給一個函式,該函式根據副檔名過濾檔案。
示例
以下是如何使用目錄中的特定檔案建立 ZIP 檔案的示例 -
import os from zipfile import ZipFile def zip_csv(directory_name, zip_file_name, filter): # Create object of ZipFile with ZipFile(zip_file_name, 'w') as zip_object: # Traverse all files in directory for folder_name, sub_folders, file_names in os.walk(directory_name): for filename in file_names: # Filter for csv files if filter(filename): # Create filepath of files in directory file_path = os.path.join(folder_name, filename) # Add files to zip file zip_object.write(file_path, os.path.basename(file_path)) if __name__ == '__main__': zip_csv('E:/Folder to be zipped', 'E:/Zipped file.zip', lambda name: 'csv' in name) if os.path.exists('E:/Zipped file.zip'): print("ZIP file created with only CSV files") else: print("ZIP file not created")
輸出
以下是上述程式碼的輸出 -
ZIP file created with only CSV files
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP