程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Explain the python compressed package processing module zipfile and py7zr

編輯:Python

        At present, the commonly used format for file compression and decompression is zip Format and 7z Format , Today, I will use an article to understand the operation of the two compressed file formats .

One :zipfile Common operations of

1, Compressed files

 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 zipfile
import 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 zipfile
import os
testdir = "D:\\FTZ\\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

2, Unzip the file

import zipfile
import 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")

3, List all the files in the package

import zipfile
import os
testdir = "D:\\FTZ\\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 :

 4, Other common methods

import zipfile
import 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 .

  Two :py7zr Common operations of

 1, Compressed files

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')

2, Unzip the file

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 py7zr
import 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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved