PHP is a widely used open source scripting language for Web development. It has the advantages of being easy to learn, efficient, and cross-platform, and has become one of the most commonly used scripting languages in Web development. When we need to compress and decompress files, zip is a very practical format.
In this article, we will discuss how to convert zip files using PHP. This process includes how to compress files and how to decompress files. The following are detailed steps:
In the first step, we need to create a zip object and send it to it Add files that need to be compressed. The following is a code example:
$zip = new ZipArchive(); $zip_name = 'compressed.zip'; //压缩文件名 if ($zip->open($zip_name, ZipArchive::CREATE)!==TRUE) { exit("无法进行压缩"); } $zip->addFile('file1.txt'); //添加需要压缩的文件 $zip->addFile('file2.txt'); $zip->addFile('folder/file3.txt'); //可以添加文件夹 $zip->close(); //关闭zip对象
The above code creates an empty zip object and adds three files to the compressed package. We can add files that need to be compressed by calling the addFile()
method and passing in the absolute path of the file. If you need to add a folder, you only need to pass in the corresponding folder path. Finally, we need to call the ZipArchive::close()
method to close the created zip object. All changes made to the object will be saved to the zip file before closing.
Decompressing a zip file is also quite simple. We only need to open a zip object and extract the files that need to be decompressed into the specified directory. Below is a code example:
$zip = new ZipArchive(); $unzip_folder = 'uncompressed'; //解压文件夹名 $zip_name = 'compressed.zip'; //要解压的文件名 if ($zip->open($zip_name) === TRUE) { $zip->extractTo($unzip_folder); //解压缩所有文件 $zip->close(); echo '解压成功'; } else { echo '无法进行解压'; }
The above code shows how to extract all files in a zip file. We will create an unzip directory and extract the zip file into that directory. If all goes well, we should be able to see all the files in the folder.
The following is a complete code example showing how to use PHP to convert zip files:
open($zip_name, ZipArchive::CREATE)!==TRUE) { exit("无法进行压缩"); } $zip->addFile('file1.txt'); //添加需要压缩的文件 $zip->addFile('file2.txt'); $zip->addFile('folder/file3.txt'); //可以添加文件夹 $zip->close(); //关闭zip对象 $zip = new ZipArchive(); $unzip_folder = 'uncompressed'; //解压文件夹名 $zip_name = 'compressed.zip'; //要解压的文件名 if ($zip->open($zip_name) === TRUE) { $zip->extractTo($unzip_folder); //解压缩所有文件 $zip->close(); echo '解压成功'; } else { echo '无法进行解压'; }
The above code shows how to create a zip file, add Files and unzipping zip files, these basic operations will become more and more common as we develop for the web. Using PHP to work with zip files allows us to easily create, read, and modify compressed files, which allows us to add these capabilities to our applications faster.
The above is the detailed content of How to convert php to zip. For more information, please follow other related articles on the PHP Chinese website!