10.3 bz2--supports bzip2 compression and decompression

Source: Internet
Author: User

This module mainly provides the operation function which supports the BZIP2 compression algorithm. the bz2 Library contains content:theopen () function and the bz2file class are primarily used to read and write compressed files; The Bz2compressor and bz2decompressor classes provide an incremental compression and decompression;compress () and the decompress () function provides the ability to compress and decompress data once and for all.

Bz2.open (filename, mode= ' R ', compresslevel=9, Encoding=none, Errors=none, Newline=none) /strong>

Opens a bzip2 Compressed file object that can be opened in binary or text mode. The parameter filename can be a file name, or an existing file object , and the parameter mode is the way to open the file, which can support:' R ', ' RB ', ' W ', ' WB ', ' x ', ' xb ', ' a ', ' ab ', ' RT ', ' wt ', ' XT ', or ' at ' ; parameter compresslevel is the compression level, which can be 1< /c10> to level 9 , parameters such as parameter encoding/errors/newline can be used for text mode .

Class bz2. Bz2file (filename, mode= ' R ', Buffering=none, compresslevel=9)

Open the bzip2 compressed file in binary mode. If the parameter filename is a str or bytes type, open the file directly or act as a file object; Mode same as the previous function; Buffering is no longer used, ignored; parameters Compresslevel same as the previous function.

Peek ([n])

Returns the data in the buffer, but does not move the file position pointer.

Class bz2. Bz2compressor (compresslevel=9)

Creates a new compressed object that this object uses to compress data incrementally. The parameter compresslevel is the compression level, the range is 1 to 9, and the default value is 9.

Compress (data)

Provides partial data to a compressed object, returning a compressed data object, or an empty object. The rest of the data waits until the flush () function is called to compress the return.

Flush ()

Completes the compression process, returning the last compressed data object.

Class bz2. Bz2decompressor

Creates an uncompressed object, which is extracted incrementally.

Decompress (data)

Attempts to decompress the supplied data and return the extracted data object if it can be decompressed.

Eof

Returns Trueif the end of the file is reached.

Unused_data

End data that has not been used after compressing the stream.

Bz2.compress (data, compresslevel=9)

Data is compressed once.

Bz2.decompress (data)

The data is decompressed once.

Example:

#python 3.4

From bz2 import Bz2file

line = B ' Http://blog.csdn.net/caimouse '

With Bz2file (' test.bz2 ', ' WB ') as F:

F.write (line)

With Bz2file (' test.bz2 ', ' RB ') as F:

Test = F.read ()

Print (test)

The resulting output is as follows:

B ' Http://blog.csdn.net/caimouse '


Cai Junsheng qq:9073204 Shenzhen

10.3 bz2--supports bzip2 compression and decompression

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.