The ZipFile module in Python uses the detailed

Source: Internet
Author: User
Tags file size join reserved zip root directory zipinfo in python

This article mainly introduces the ZipFile module in Python to use the detailed, ZipFile module is used to manipulate the zip file, the need for friends can refer to the

The zip file format is a common document compression standard, in the Ziplib module, the ZipFile class is used to manipulate the zip file, the following details:

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 file; parameter mode indicates the mode of the open zip file, the default value is ' R ', which means reading the existing zip file, or ' W ' or ' a ', ' W ' means to create a new zip document or overwrite an existing zip document, ' A ' means to attach the data to an existing ZIP document. The parameter compression represents the compression method that is used when writing the zip document, and its value can be zipfile. Zip_stored or ZipFile. Zip_deflated. If the size of the zip file you want to manipulate exceeds 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. Zipinfo object, which includes details of the file. The object will be specifically described below.

Zipfile.infolist ()

Gets information about all the files in the Zip document and returns a ZipFile. 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 file in the zip document to the current directory. Parameter member specifies the name of the file or the corresponding Zipinfo object to extract, and the parameter path specifies the folder where the file is saved, and the parameter pwd to the decompression password. The following example extracts all files in the txt.zip that are saved in the root directory of the program to the D:/work directory:

?

1 2 3 4 5 6 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]])

Extract all the files from the zip document into the current directory. The default value for the parameter members is a list of all file names within the zip document, or you can set it yourself to select the name of the file you want to extract.

Zipfile.printdir ()

Prints 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 illustrates the use of Read (), which includes a txt.txt text file, reads the binary data using the Read () method, and then saves it to D:/txt.txt.

?

1 2 3 4 5 6 7 8 #coding =GBK import zipfile, os ZipFile = ZipFile. ZipFile (Os.path.join (OS.GETCWD (), ' txt.zip ') data = Zipfile.read (' txt.txt ') (Lambda F, D: (F.write (d), F.close ())) ( Open (R ' d:/txt.txt ', ' WB '), Data #一行语句就完成了写文件操作. Careful thinking. ~_~ Zipfile.close () zipfile.write (filename[, arcname[, Compress_type])

Adds the specified file to the zip document. FileName is the file path, arcname 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 a compressed document.

?

1 2 3 4 5 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

The Zipfile.getinfo (name) method returns a Zipinfo object that represents the information for the corresponding file in the zip document. It supports the following properties:

Zipinfo.filename: Gets the file name.

Zipinfo.date_time: Gets the last modification time of the file. Returns a tuple containing 6 elements: (year, month, day, time, minute, second)

Zipinfo.compress_type: Compression type.

Zipinfo.comment: Document description.

ZIPINFO.EXTR: Extended item 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: Gets the PKZIP version required to extract the zip document.

zipinfo.reserved: Reserved field, the current implementation always returns 0.

ZipInfo.flag_bits:zip sign bit.

Zipinfo.volume: The volume label of the file header.

Zipinfo.internal_attr: Internal properties.

Zipinfo.external_attr: External properties.

Zipinfo.header_offset: File head offset shift.

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:

?

1 2 3 4 5 6 7 8 9 A, 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. I thought I was. NET platform, the use of sharpziplib compression, decompression a file, I spent more than n time, looking for more than n English resources, just write a file can compress the demo. Now, using Python, you can read the Python manual and have a one or two-hour grasp of the basic use of the ZipFile module. Haha, using Python, that's cool!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.