This article “Python Compressed packet processing module zipfile and py7zr How do you use it? ” Most people don't quite understand the knowledge points of the article , So I made up the following summary for you , Detailed content , The steps are clear , It has certain reference value , I hope you can gain something after reading this article , Let's take a look at this article “Python Compressed packet processing module zipfile and py7zr How do you use it? ” Article bar .
zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])
Parameters file Represents the path to the file ; Parameters mode Indicates that zip File mode , There are three kinds of mode
decompression :r
Compress :w
Additional compression :a
The default value is 'r', Indicates that the read already exists zip file , It can also be for 'w' or 'a','w' Means to create a new zip Document or overwrite an existing zip file ,‘a’ Indicates additional compression
Let's create a compressed file result.zip, And compress test All the files under the folder
import zipfileimport os testdir = "D:\\FTZ\\python_tool\\result\\test"filename = "./result.zip"z = zipfile.ZipFile(filename, 'w') for d in os.listdir(testdir): z.write(d) z.close
Of course, you can also use with Method on
import zipfileimport os testdir = "D:\\DPI\\python_tool\\result\\test"filename = "./result.zip"with zipfile.ZipFile(filename, 'w') as z: for d in os.listdir(testdir): z.write(d) z.close
import zipfileimport os testdir = "D:\\FTZ\\python_tool\\result\\test"filename = "./result.zip"with zipfile.ZipFile(filename, 'r') as z: z.extractall(testdir)
It should be noted that some compressed packages are decompressed with passwords , Call at this time extractall You can also enter a password when you use the , stay python3 in , The password parameters of the extracted file pwd The received value is binary , So add one in front of it b
z.extractall(testdir,pwd=b"ftz")
import zipfileimport ostestdir = "D:\\DPI\\python_tool\\result\\test"filename = "./result.zip"with zipfile.ZipFile(filename, 'r') as z: files = z.namelist() print(files)
The operation results are as follows , It returns a list :
import zipfileimport os testdir = "D:\\ftz\\python_tool\\result\\test"filename = "./result.zip"with zipfile.ZipFile(filename, 'r') as z: z.setpassword(b'ftz1') # Set up zip The password for the document . z.printdir() # take zip The information in the document is printed to the console . data = z.read('file.yaml') # obtain zip Binary data of the specified file in the document print(data) info = z.getinfo('file.yaml') # Method returns a ZipInfo object , Express zip The information of the corresponding file in the document . It supports the following properties print(" Get the file name :",info.filename) print(" Get the last modification time of the file :",info.date_time) print(" Get compression type :",info.compress_type) print(" Get the compressed size :",info.compress_size) print(" Get uncompressed file size :",info.file_size) print(" Determine whether it is a compressed file :",zipfile.is_zipfile(filename))
The operation results are as follows :
File Name Modified Size
data_yaml.yaml 2022-06-25 10:50:42 198
file.yaml 2022-06-25 10:13:46 123
vnfname.txt 2022-01-14 11:27:56 9320
b'\xe4\xb8\x80\xe7\xba\xa7\xe6\xa0\x87\xe9\xa2\x98:\n \xe4\xba\x8c\xe7\xba\xa7\xe6\xa0\x87\xe9\xa2\x981:\n b:1\n c:2\n a:3\n \xe4\xba\x8c\xe7\xba\xa7\xe6\xa0\x87\xe9\xa2\x982:\n f:7\n t:8\n z:9'
Get the file name : file.yaml
Get the last modification time of the file : (2022, 6, 25, 10, 13, 46)
Get compression type : 0
Get the compressed size : 123
Get uncompressed file size : 123
Determine whether it is a compressed file : True
[Finished in 0.2s]
Of course getinfo Other properties are also supported , Here are the more complete attributes and methods
ZipInfo.filename: Get the file name .
ZipInfo.date_time: Get the last modification time of the file . Returns a containing 6 Tuples of elements :( year , month , Japan , when , branch , second )
ZipInfo.compress_type: Compression type .
ZipInfo.comment: documentation .
ZipInfo.extr: Extension data .
ZipInfo.create_system: Get to create the zip Document system .
ZipInfo.create_version: obtain establish zip Document PKZIP edition .
ZipInfo.extract_version: obtain decompression zip Required for documentation PKZIP edition .
ZipInfo.reserved: Reserved fields , The current implementation always returns 0.
ZipInfo.flag_bits: zip Sign a .
ZipInfo.volume: Volume label of file header .
ZipInfo.internal_attr: Internal attributes .
ZipInfo.external_attr: External properties .
ZipInfo.header_offset: File header offset .
ZipInfo.CRC: Of uncompressed files CRC-32.
ZipInfo.compress_size: Get the compressed size .
ZipInfo.file_size: Get uncompressed file size .
import py7zr with py7zr.SevenZipFile('target.7z', 'w') as archive: archive.writeall('/path/to/base_dir', 'base')
If you want to set password compression
import py7zr with py7zr.SevenZipFile('target.7z', 'w', password='secret') as archive: archive.writeall('/path/to/base_dir', 'base')
import py7zr archive = py7zr.SevenZipFile('sample.7z', mode='r')archive.extractall(path="/tmp")archive.close()
Also support with Pattern
import py7zr with py7zr.SevenZipFile('sample.7z', mode='r') as z: z.extractall() with py7zr.SevenZipFile('target.7z', 'w') as z: z.writeall('./base_dir')
py7z It also supports extracting a file or a file that conforms to regular matching
import py7zrimport re filter_pattern = re.compile(r'<your/target/file_and_directories/regex/expression>')with SevenZipFile('archive.7z', 'r') as archive: allfiles = archive.getnames() selective_files = [f for f in allfiles if filter_pattern.match(f)] archive.extract(targets=selective_files)
Supports decompressing encrypted 7z file
import py7zr with py7zr.SevenZipFile('encrypted.7z', mode='r', password='secret') as z: z.extractall()
That's about “Python Compressed packet processing module zipfile and py7zr How do you use it? ” The content of this article , I believe we all have a certain understanding , I hope the content shared by Xiaobian will be helpful to you , If you want to know more about it , Please pay attention to the Yisu cloud industry information channel .