在Python的标准库中,有一个名为zipfile的模块。使用此模块,我们可以对zip文件执行不同的操作。
压缩文件是存档文件格式。Zip文件用于获得较少损失的数据压缩功能,因此从压缩形式可以完美地重建它。一个zip文件可以在其中保存一个或多个压缩文件。
压缩文件的优点是,它减少了存储需求,并通过标准连接传输速度。
在Python中,我们需要导入zipfile模块以使用zip文件。
from zipfile import ZipFile
首先,我们将看到如何从目录创建Zip文件。在这种情况下,主目录包含一个文件和一些其他目录。
因此,首先,我们必须爬网整个目录以获取所有文件和文件夹,并将路径存储为列表。然后将Zip File作为写入模式打开,然后从存储的路径中全部写入。
#Get all files from directory and subdirectories and convert to zip from zipfile import ZipFile import os defget_all_files(directory): #function to get all files from directory paths = [] for root, dirs, files in os.walk(directory): for f_name in files: path = os.path.join(root, f_name) #get a file and add the total path paths.append(path) return paths #Return the file paths directory = './sample_files' paths = get_all_files(directory) #Display the filenames print('The following files will be stored in the zip file') for file in paths: print(file) #Open zip file as write mode to create zip with ZipFile('my_files.zip', 'w') as zf: for file in paths: zf.write(file) print('Zip is created Successfully')
输出结果
The following files will be stored in the zip file ./sample_files\TP_python_prev.pdf ./sample_files\text_files\file1.txt ./sample_files\text_files\file2.txt Zip is created Successfully
现在看看,如何使用zipfile模块提取zip文件。要提取一个zip,首先,我们必须以读取模式打开zip文件,然后使用extract()
或extractall()
方法提取内容。该extract()
方法采用从Zip文件中提取文件的路径。
#Extract the zip files from zipfile import ZipFile zip_file = 'my_files.zip' with ZipFile(zip_file, 'r') as zf: #display the files inside the zip zf.printdir() #Extracting the files from zip file zf.extractall() print('Zip Extraction Completed')
输出结果
File Name Modified Sizesample_files/TP_python_prev.pdf 2018-10-19 13:19:48 1915882sample_files/text_files/file1.txt 2018-11-06 13:34:46 22sample_files/text_files/file2.txt 2018-11-06 13:35:02 24Zip Extraction Completed
现在,我们将讨论infolist()
zipfile模块的方法。这提供了不同信息的列表,例如文件名,修改日期,文件大小等。
#Get information about the zip file from zipfile import ZipFile import datetime zip_file = 'my_files.zip' with ZipFile(zip_file, 'r') as zf: for detail in zf.infolist(): print('File Name: ' + detail.filename) print('\tModify Date: ' + str(datetime.datetime(*detail.date_time))) print('\tZip Version: ' + str(detail.create_version)) print('\tSystem Version: ' + str(detail.create_system)) #0 for windows print('\tFile Size (Bytes): ' + str(detail.file_size))
输出结果
File Name: sample_files/TP_python_prev.pdf Modify Date: 2018-10-19 13:19:48 Zip Version: 20 System Version: 0 File Size (Bytes): 1915882File Name: sample_files/text_files/file1.txt Modify Date: 2018-11-06 13:34:46 Zip Version: 20 System Version: 0 File Size (Bytes): 22File Name: sample_files/text_files/file2.txt Modify Date: 2018-11-06 13:35:02 Zip Version: 20 System Version: 0 File Size (Bytes): 24