PHP Development notes series (7)-compression and decompression ???? Compression and decompression may not be used much in general web applications, but are commonly used in the following application scenarios, such as compressing database backup files, package multiple files in one file for download, compress the files packed and uploaded by the customer, and explain them. be familiar with the PHP Development notes series (7)-compress and decompress
?
??? Compression and decompression may not be used much in general web applications, but are commonly used in the following application scenarios, such as compressing database backup files, package multiple files in one file for download, compress and interpret the files that the customer packs and uploads, and familiarize yourself with common functions and classes for compression and decompression.
??? This article "PHP Development notes series (7)-compression and decompression" will be the seventh article in "PHP development Notes Series (XAMPP + PhpEclipse + XDebug, describes how to use the Pear plug-in Archive Tar to perform regular file compression and decompression operations.
???1. use the Archive_Tar class for file compression
???? The Archive_Tar class is located in the Archive/Tar. php file in the Pear folder, and requires a small amount of code to compress files using this class. Step: 1) instantiate this class. The first construction parameter is the compressed target file name, and the second construction parameter is the compression type (gz: gzip compression, bz2 use bzip2 compression ), if the second parameter is omitted, only the Archive_Tar object is packaged and not compressed. 2) after the Archive_Tar object is created, input the file name data to the create method of the object. The code is as follows:
???
file:tar.phpurl:http://localhost:88/archive/tar.php
create($files); echo 'xdebug.tar.gz exist:'.file_exists($tarFile).'
'; echo 'xdebug.tar.gz mtime:'.date('Y-m-d h-i:s', filemtime($tarFile)).'
'; echo 'xdebug.tar.gz :atime:'.date('Y-m-d h-i:s', fileatime($tarFile)).'
'; echo 'xdebug.tar.gz size:'.filesize($tarFile).'
';?>
?
???2.Use Archi Ve_Tar class for file compression
? ? The decompression process is similar to the compression process. The steps are as follows: 1) instantiate the Archive_Tar object and upload it to the source compressed file name; 2) after the object is instantiated, input a target folder name to the decompressed method extract, the files in the compressed package will be released to the target folder. The code is as follows:
?
file:extract.phpurl:http://localhost:88/archive/extract.php
extract($targetDir); $dp = opendir($targetDir); while ($entry = readdir($dp)){ if(is_dir($entry)) { echo '[DIR] '.$entry. '
'; }elseif (is_file($entry)) { echo '[FILE] '.$entry. '
'; } } closedir($dp);?>
?
?
?
??? Address: http://ryan-d.iteye.com/blog/1543417