Python 如何创建zip文件

Python 如何创建zip文件

ZIP是一种用于无损数据压缩的存档文件格式。可以使用一个或多个目录或文件来创建ZIP文件。ZIP支持多种压缩算法,DEFLATE是最常见的。ZIP文件的扩展名为.zip。在本文中,我们将讨论如何使用Python创建Zip文件。

在Python中创建未压缩的ZIP文件

未压缩的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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程