#python压缩解压模块: ZipFile tarfile
#--zipfile Module
‘‘‘
Suffix is zip
‘‘‘
ZipFile. ZipFile (file[, mode[, compression[, AllowZip64]])
*zipfile (Path package name, mode, compressed or packaged, optional allowZip64)
function: Create a ZipFile object that represents a zip file.
-parameter file represents the path or class file object (File-like object)
- The parameter mode indicates the pattern of opening the zip file, the default value is R
Span style= "font-family: italics; Font-size:18px "> W means creating a new zip document or overwriting an existing zip document
a Represents appending data to an existing ZIP document.
-parameter compression represents the compression method used when writing a zip document
ZipFile. Zip_stored is just a storage mode and does not compress the file, this is the default value
ZipFile. zip_deflated compressing the file
-if the zip file size to be manipulated exceeds 2G, The ALLOWZIP64 should be set to true.
#压缩文件
#1. ZipFile () write mode w Open or create a new compressed file
#2. Write (path, alias) to add file contents to a compressed file
Close the compressed file #3. Close ()
#解压文件
#1. ZipFile () Read mode R open compressed file
#2. Extractall (path) unzip all files to a certain path
# Extract (file, path) unzip the specified file to a certain path
Close the compressed file #3. Close ()
#追加文件 (support with notation)
ZipFile () Append mode A opens the compressed file
#查看压缩包中的内容
NameList ()
"""
#处理zip中文乱码
#方法一
Zip_file = Zip_file.encode (' cp437 '). Decode (' GBK ')
#方法二
Zip_file = Zip_file.encode (' Utf-8 '). Decode (' Utf-8 ')
"""
#--tarfile Module
mode has tar|gz|bz2 (bz2-mode compressed file is smaller GZ relative bz2 Large)
W simple set one suffix package
W:BZ2 uses bz2 algorithm compression
#压缩文件
#2. Add (Path file, Arcname= "Alias") add file to compressed file
#3, close () closes file
#解压文件
#1. Open (' Path package name ', ' pattern ', ' character encoding ') read-mode opening file
#2. Extractall (path) unzip all files to a certain path
# Extract (file, path) unzip the specified file to a certain path
Close the compressed file #3. Close ()
#追加文件
Open () Append mode A: Opens the compressed file normal add can
#查看压缩包中的内容
GetNames ()
# ZipFile Module
# suffix is zip
# Compressed files are ZipFile
ZP = ZipFile. ZipFile ("/mnt/hgfs/vm_gongxiang/star/a001.zip", "W", ZipFile. zip_deflated)
Zp.write ("/bin/dash", "dash")
Zp.write ("/bin/date", "date")
Zp.write ("/bin/dd", "DD")
Zp.write ("/bin/df", "DF")
Zp.close ()
# Compressed Files
With ZipFile. ZipFile ("/mnt/hgfs/vm_gongxiang/star/a001.zip", "R") as ZP:
Zp.extractall ("/mnt/hgfs/vm_gongxiang/star/a001")
# zp.extract ("/mnt/hgfs/vm_gongxiang/star/a001")
# Append file
With ZipFile. ZipFile ("/mnt/hgfs/vm_gongxiang/star/a001.zip", "a", ZipFile. zip_deflated) as ZP:
Zp.write ("/BIN/DMESG", "/DMESG")
# view content in a compressed package
With ZipFile. ZipFile ("/mnt/hgfs/vm_gongxiang/star/a001.zip", "R") as ZP:
res = Zp.namelist ()
Print (RES)
===
# tarfile module
" "
suffix type:. tar|. tar.gz |. tat.bz2
mode has tar|gz|bz2 (bz2-mode compressed file smaller GZ relative bz2)
W simple set of suffixes package
W:gz uses bz2 algorithm to compress
w:bz2 using GZ algorithm compression
# tarfile compressed file
with tarfile.open ("/mnt/hgfs/mv_gongxiang/star/a001.tar.bz "," W:bz ", encoding=" GBK ") as TF:
tf.add ("/bin/dash", arcname= "Dash")
tf.add ("/bin/date ", arcname=" date ")
Tf.add ("/bin/dd", arcname= "DD")
tf.add ("/BIN/DF", Arcname= "DF")
tf.add ("/mnt/hgfs/mv_gongxiang/hello. txt", Arcname= "Hello")
# Tarfile Unzip the file
With Tarfile.open ("/mnt/hgfs/mv_gongxiang/star/a001.tar.bz", "R") as TF:
Tf.extractall ("/mnt/hgfs/mv_gongxiang/star/a001")
# tf.extract ("/mnt/hgfs/mv_gongxiang/star/a001")
# tarfile Append file
With Tarfil.open ("/mnt/hgfs/mv_gongxiang/star/a001.tar.bz", "A:") as TF:
Tf.add ("/bin/echo", Arcname= "echo")
# view contents in a compressed package
With Tarfile.open ("/mnt/hgfs/mv_gongxiang/star/a001.tar.bz", "R", encoding= "GBK") as TF:
res = Tf.getname ()
Print (RES)
Python_ Object-oriented _zipfile and Tarfile