如何使用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
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP