PHP application: detailed explanation of compression and decompression file functions

王林
Release: 2023-06-20 15:20:02
Original
2716 people have browsed it

As the amount of data increases, file compression and decompression have become an essential function for many applications. As a popular web development language, PHP naturally provides some built-in functions for developers to use. In this article, we will learn more about the compression and decompression file functions in PHP.

  1. Compressed files

(1)gzip compression

gzip is a tool for compressing files. It can compress a file into a .gz files. In PHP, you can use the gzopen() function to open a gz file, and use the gzwrite() function to write data to the file.

Sample code:

<?php
$filename = "example.txt";
$gzfilename = "example.txt.gz";

// 打开gz文件并写入数据
$gz = gzopen($gzfilename, 'w9');
gzwrite($gz, file_get_contents($filename));
gzclose($gz);

echo "文件压缩成功";
?>
Copy after login

(2) zip compression

Zip is a popular compressed file format that can compress multiple files into one zip file. You can use the ZipArchive class in PHP to operate zip files, including adding files to zip files, deleting files, renaming files, etc.

Sample code:

<?php
$zipname = "example.zip";
$filename_array = array("example.txt", "example2.txt", "example3.txt");

// 新建一个ZipArchive对象
$zip = new ZipArchive();
$zip->open($zipname, ZipArchive::CREATE);

// 把多个文件压缩到Zip包中
foreach ($filename_array as $filename) {
    // 向Zip包中添加文件
    $zip->addFile($filename);
}

// 关闭ZipArchive对象
$zip->close();

echo "文件压缩成功";
?>
Copy after login

(3) tar compression

tar can package multiple files into one .tar file, but does not compress it. In PHP, you can use the exec() function to call the tar command to complete the packaging operation.

Sample code:

<?php
$dir = "/path/to/directory";  // 需要打包的目录
$tarname = "example.tar";     // 打包后的文件名

// 执行tar命令,把$dir目录下的所有文件打包成$tarname文件
exec("tar -cvf " . $tarname . " " . $dir);

echo "文件打包成功";
?>
Copy after login
  1. Decompress files

(1)gzip decompression

gzip compressed files can use gzopen () function is opened and the data is read using the gzread() function. If you need to decompress the compressed file, you can use the gzopen() function to open the compressed file in read mode, and use the file_get_contents() function to read the contents into a string.

Sample code:

<?php
$gzfilename = "example.txt.gz";
$outputname = "example.txt";

// 打开gz文件并解压缩
$gz = gzopen($gzfilename, 'r');
$output = file_get_contents("compress.zlib://$gz");

// 把解压缩后的数据写入文件
file_put_contents($outputname, $output);

echo "文件解压缩成功";
?>
Copy after login

(2) zip decompression

The extractTo() function of the ZipArchive class can decompress one or more files from a zip file, and The output directory can be specified.

Sample code:

<?php
$zipname = "example.zip";
$outputdir = "/path/to/output/dir";

// 新建一个ZipArchive对象并打开zip文件
$zip = new ZipArchive();
$zip->open($zipname);

// 解压缩zip文件
$zip->extractTo($outputdir);

// 关闭ZipArchive对象
$zip->close();

echo "文件解压缩成功";
?>
Copy after login

(3) tar decompression

The tar file can be decompressed in the terminal by calling the tar command using the exec() function.

Sample code:

<?php
$tarname = "example.tar";
$outputdir = "/path/to/output/dir";

// 执行tar命令,在$outputdir目录下解压缩$tarname文件
exec("tar -xvf " . $tarname . " -C " . $outputdir);

echo "文件解压缩成功";
?>
Copy after login

The above is a detailed explanation of the compression and decompression file functions in PHP. By flexibly using these functions, we can easily complete file compression and decompression operations, which improves our development efficiency.

The above is the detailed content of PHP application: detailed explanation of compression and decompression file functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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