"""
@author:P Eng (non-original)
Reference: http://python.jobbole.com/81519/
Record: Development history
"""
Recently writing an Android APP, you need to download the data on the server. Some PDFs, if transmitted directly, will increase the amount of data being communicated, making the download time longer. The server compresses the data before it is transmitted, and the client receives the data to decompress it, which can reduce the traffic of the Netcom transmission data, shorten the download time, and increase the customer experience. Doing similar work with Python can be done easily using the API provided by the ZipFile module.
ZIP file format is a universal document compression standard, in the Ziplib module, using the ZipFile class to manipulate the zip file, the following specific introduction:
Class ZipFile. ZipFile (file[, mode[, compression[, AllowZip64]])
Creates a ZipFile object that represents a zip file. The parameter file represents the path or class file object (File-like object) of the files, and the parameter mode indicates the opening of the zip file, the default value is ' R ', which means that the existing zip file is read, or it can be ' w ' or ' a ', W ' means creating a new zip document or overwriting an existing zip document, ' A ' means attaching the data to an existing ZIP document. The parameter compression represents the compression method used when writing the zip document, and its value can be zipfile. zip_stored or zipfile. zip_deflated,zipfile. zip_stored is the default compression method, just storage, not content compression, it is recommended to use ZipFile. zip_deflated. If the zip file you want to manipulate is larger than 2G, you should set ALLOWZIP64 to true.
ZipFile also provides the following common methods and properties:
zipfile.getinfo (name):
Gets the information for the specified file within the zip document. Returns a ZipFile. A Zipinfo object that includes details about the file. The object is described in detail below.
zipfile.infolist ()
Gets information about all the files in the Zip document and returns a ZipFile. A list of zipinfo.
Zipfile.namelist ()
Gets a list of the names of all the files in the zip document.
Zipfile.extract (member[, path[, PWD])
Extracts the specified files in the zip document to the current directory. The parameter member specifies the name of the file to decompress or the corresponding Zipinfo object, and the parameter path specifies the folder where the file is parsed, and the parameter pwd is the decompression password. The following example extracts all the files in the txt.zip that are stored in the program root directory to the D:/work directory:
Python
| 12345 |
import zipfile, os zipfile = zipfile. Zipfile (os.path. Join (os. Getcwd () , ' txt.zip ' ) ) for file in zipfile. NameList(): zipfile. Extract(file, r' d:/work ') ZipFile. Close() |
Zipfile.extractall ([path[, members[, PWD]])
Unzip all the files in the zip document into the current directory. The default value for the members of the parameter is a list of all the file names in the zip document, or you can set them yourself, and select the name of the file to unzip.
Zipfile.printdir ()
Print the information in the ZIP document to the console.
Zipfile.setpassword (PWD)
Sets the password for the zip document.
Zipfile.read (name[, PWD])
Gets the binary data for the specified file within the zip document. The following example shows the use of Read (), which includes a txt.txt text file in the zip document, reads the binary data using the Read () method, and then saves it to D:/txt.txt.
Python
| 123456 |
#coding =GBKimport zipfile, os zipfile = zipfile. Zipfile (os.path. Join (os. Getcwd () , ' txt.zip ' ) ) data = zipfile. Read(' txt.txt ') (Lambda F, D: (Fwrite (d) , f. Close () ) ) (open (r D:/txt.txt ' , ' WB ' , data #一行语句就完成了写文件操作. Think carefully. ~_~ ZipFile. Close() |
Zipfile.write (filename[, arcname[, Compress_type])
Adds the specified file to the zip document. FileName is the file path, Arcname is the name saved after adding to the zip document, the parameter Compress_type represents the compression method, and its value can be zipfile. zip_stored or zipfile. zip_deflated. The following example shows how to create a zip document and add the file D:/test.doc to the compressed document.
Python
| 1234 |
import zipfile, os zipfile = zipfile. Zipfile (r ' D:/test.zip ' , ' W ' ZipFile. Write(r' D:/test.doc ', ' Ok.doc ', zipfile. zip_deflated) ZipFile. Close() |
Zipfile.writestr (zinfo_or_arcname, bytes)
WRITESTR () supports writing binary data directly to a compressed document.
Class Zipinfo
zipfile.getinfo (name) The Zipinfo method returns an object that represents the information for the corresponding file in the zip document. It supports the following properties:
zipinfo.filename: Get file name.
zipinfo.date_time: Gets the last modified time for the file. Returns a tuple containing 6 elements: (year, month, day, time, minute, second)
zipinfo.compress_type: Compression type.
zipinfo.comment: Documentation description.
zipinfo.extr: extension data.
Zipinfo.create_system: Gets the system that created the zip document.
zipinfo.create_version: Gets the pkzip version of the zip document that was created.
zipinfo.extract_version: Get the PKZIP version required to extract the zip document.
zipinfo.reserved: reserved field, the current implementation always returns 0.
zipinfo.flag_bits: zip flag bit.
Zipinfo.volume: The volume label of the file header.
zipinfo.internal_attr: Internal properties.
zipinfo.external_attr: External properties.
zipinfo.header_offset: file header offset.
ZIPINFO.CRC: CRC-32 of uncompressed files.
zipinfo.compress_size: Gets the size after compression.
zipinfo.file_size: Gets the uncompressed file size.
The following simple example illustrates the meaning of these attributes:
Python
| 123456789101112131415161718192021 |
import zipfile, os zipfile = zipfile. Zipfile (os.path. Join (os. Getcwd () , ' txt.zip ' ) ) zipinfo = zipfile. GetInfo(' Doc.doc ') print ' filename: ', zipinfo. FileName print ' date_time: ', zipinfo. Date_time print ' compress_type: ', zipinfo. Compress_type print ' Comment: ', zipinfo. Comment print ' extra: ', zipinfo. Extra print ' create_system: ', zipinfo. Create_system print ' create_version: ', zipinfo. Create_version print ' extract_version: ', zipinfo. Extract_version print ' extract_version: ', zipinfo. Reserved print ' flag_bits: ', zipinfo. Flag_bits Print ' Volume: ', zipinfo. Volume print ' internal_attr: ', zipinfo. Internal_attr print ' external_attr: ', zipinfo. External_attr print ' header_offset: ', zipinfo. Header_offset print ' CRC: ', zipinfo. CRC print ' compress_size: ', zipinfo. Compress_size print ' file_size: ', zipinfo. File_size ZipFile. Close() |
It's really easy to use the ZipFile module to work with ZIP files. The basic use of the ZipFile module was mastered in one or two hours by reading the Python manual. Haha, using Python, that's cool!
Python module: ZipFile