How does PHP ZipArchive handle Chinese file names in compressed packages?
In development, it is often necessary to compress and decompress files, and PHP provides the ZipArchive extension to meet this need. However, you may encounter some problems when dealing with Chinese file names. This article will introduce how to use ZipArchive to correctly handle Chinese file names in compressed packages.
When we use ZipArchive to create a compressed package, we need to pay attention to the encoding of the file name. By default, ZipArchive uses GBK encoding, while Chinese file names usually use UTF-8 encoding. In order to ensure that Chinese file names are displayed correctly, we need to convert the file names to GBK encoding.
The following is a sample code that creates a compressed package and adds a Chinese file name:
$zip = new ZipArchive(); $filename = 'archive.zip'; if ($zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) { $file1 = '中文文件.txt'; // 转换文件名为GBK编码 $file1_gb2312 = iconv('UTF-8', 'GBK//IGNORE', $file1); // 在压缩包中添加文件 $zip->addFile($file1, $file1_gb2312); // 关闭压缩包 $zip->close(); echo '压缩包创建成功'; } else { echo '创建压缩包失败'; }
In the above code, we first create a ZipArchive object and specify the name of the compressed package. Then, we opened the compressed package and added a Chinese file to it. When adding a file, we use the iconv function to convert the file name from UTF-8 encoding to GBK encoding, and use the converted file name for the adding operation. Finally, we close the archive and print the results.
Next, let’s take a look at how to decompress a compressed package containing Chinese file names. Before decompressing, we need to convert the file name from GBK encoding to UTF-8 encoding.
The following is a sample code for decompressing a compressed package and processing Chinese file names:
$zip = new ZipArchive(); $filename = 'archive.zip'; if ($zip->open($filename) === true) { $destination = 'extracted/'; // 创建解压目录 if (!is_dir($destination)) { mkdir($destination); } // 解压文件并处理中文文件名 for ($i = 0; $i < $zip->numFiles; $i++) { $entry = $zip->statIndex($i); $file_gb2312 = $entry['name']; // 转换文件名为UTF-8编码 $file_utf8 = iconv('GBK', 'UTF-8//IGNORE', $file_gb2312); // 解压文件 $zip->extractTo($destination, $file_gb2312); // 重命名文件 rename($destination . $file_gb2312, $destination . $file_utf8); } // 关闭压缩包 $zip->close(); echo '解压成功'; } else { echo '打开压缩包失败'; }
In the above code, we first create a ZipArchive object and open the compressed package to be decompressed. We then created an unzipped directory and used a loop to iterate through all the files in the archive. For each file, we get its filename and convert it from GBK encoding to UTF-8 encoding. Then, we use the extractTo method to extract the file, and the rename function to rename the file to the converted file name. Finally, we close the archive and print the results.
Through the above code examples, we can correctly handle the Chinese file names in the compressed package and ensure that the file names can be displayed and operated correctly. File compression and decompression can be easily performed using the ZipArchive extension, and provides a variety of ways to handle Chinese file names. I hope this article can be helpful to your development work!
The above is the detailed content of How does PHP ZipArchive handle Chinese file names in compressed packages?. For more information, please follow other related articles on the PHP Chinese website!