Home > Backend Development > PHP Problem > How to use bz2 compression extension tool in PHP

How to use bz2 compression extension tool in PHP

醉折花枝作酒筹
Release: 2023-03-10 10:54:02
forward
2713 people have browsed it

This article will introduce to you how to use the bz2 compression extension tool in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to use bz2 compression extension tool in PHP

In daily development and computer use, we often come into contact with some tools for compression and decompression. PHP has also prepared many related operation expansion packages for us, all of which There are directly available functions that can conveniently operate some compression and decompression functions. Today, we will first learn a simpler but less commonly used compression format: Bzip2.

Installing the extension

The installation of this extension requires bzip2-devel on the system. So we need to install support for this software package on the system first, and then this extension is released with the PHP installation package, so we only need to compile PHP and add the corresponding compilation command in ./configure.

# yum install bzip2-devel
# ./configure xxxx --with-bz2
# make && make install
Copy after login

Basic operations

Bzip2 provides few functions and is very simple. The first thing we look at is saving a string to a file.

$bz = bzopen('/tmp/test.bz', 'w');
// -rw-r--r-- 1 root root 14 Jun 28 09:51 test.bz

$text = "This is Bz Compress";
bzwrite($bz, $text);
// -rw-r--r-- 1 root root 59 Jun 28 09:53 test.bz

bzclose($bz);

$bz = bzopen('/tmp/test.bz', 'r');

$v = bzread($bz);
echo $v, PHP_EOL;
// This is Bz Compress

bzclose($bz);
Copy after login

Just like the file operation function, we need to first open the file through bzopen() to obtain the handle. Then use bzwrite() to write to the file, and bzread() to read the file. Finally use bzclose() to close the file.

What needs to be noted here is that the second parameter of bzopen(), which is the form of file opening, can only be written as "w" or "r". It has no other type, and cannot be read and written at the same time, that is, it cannot be written in the form "wr". So after we finish writing the file, we have to use "r" to open the file to read it.

Read length setting

$bz = bzopen('/tmp/test.bz', 'r');

$v = bzread($bz, 10);
echo $v, PHP_EOL;
// This is Bz

$v = bzread($bz);
echo $v, PHP_EOL;
//  Compress

bzclose($bz);
Copy after login

The second parameter of bzread() is the optional byte length, the default is 1024, and a maximum of 8192 uncompressed bytes can be read at one time.

String encoding

The Bzip2 extension also provides us with functions for encoding strings directly. There is no need to save it to the file every time. If it is the same string, the function using string encoding and the content output to the file will be the same garbled binary content.

$str = "Test compress String";

$bzstr = bzcompress($str, 9);
echo $bzstr, PHP_EOL;
// BZh91AY&SY��J���@
//
// �� 1
// df����2�h>.�p�!��//

$newStr = bzdecompress($bzstr);
echo $newStr, PHP_EOL;

$chineseStr = "测试";
$bzstr = bzcompress($chineseStr, 9);
echo bzdecompress($bzstr), PHP_EOL;
Copy after login

bzcompress() is used to encode and compress strings. The second parameter is the compression ratio, and 9 is the highest level. The encoded content is non-human binary gibberish. bzdecompress() is used to decode encoded content. I believe many friends have discovered that this can be used for encrypted transmission of confidential content. At the same time, in the test code, we can see that it also supports Chinese normally.

Error message

Finally, let’s take a look at Bzip2’s error handling function.

$bz = bzopen('/tmp/test.bz', 'r');
bzwrite($bz, 'aaa');
print_r(bzerror($bz));
// Array
// (
//     [errno] => -1
//     [errstr] => SEQUENCE_ERROR
// )

echo bzerrno($bz), PHP_EOL; // -1
echo bzerrstr($bz), PHP_EOL; // SEQUENCE_ERROR

bzclose($bz);
Copy after login

We first constructed an error environment. After using "r" to open the file and obtain the handle, write to the file. bzerror() will return an array of error messages, which contains the error number and error message content. bzerrno() and bzerrstr() return the error number and error content respectively. Three very simple and easy-to-understand functions.

Summary

This extension is still very simple. The most important thing is that the Bzip2 compressed file type is not a very commonly used type, so people who may know it do not not much. But we still found a little surprise in it, that is, it provides string encoding and decoding functions. These two functions can indeed be used as a means of information encryption in certain scenarios.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202006/source/PHP%E7%9A%84Bzip2%E5%8E%8B%E7%BC%A9%E6%89%A9%E5%B1%95%E5%B7%A5%E5%85%B7.php
Copy after login

Recommended learning: php video tutorial

The above is the detailed content of How to use bz2 compression extension tool in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template