The solution to php decompressing zip garbled code: first open the corresponding PHP file; then use PHP's zip encapsulation protocol to decompress to solve the ZipArchive garbled code.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php decompression zip file garbled problem
Official operation method:
<?php $zip = new \ZipArchive; $zipfile = "./test.zip"; $res = $zip->open($zipfile); $toDir = "./test"; $s = $zip->extractTo($toDir); print_r(scandir($toDir));
Existing problems
If the zip is compressed on the windows platform, then the above code will appear garbled when executed on the Linux server, and at the same time The decompressed files will also be missing
Solution
Problem analysis:
ZipArchive garbled characters are caused by inconsistent encoding, Windows file system is gbk encoded, while the file names in PHP ZipArchive are UTF-8 encoded. If the file name in the Zip archive contains special characters such as Chinese, it is inevitable to use PHP ZipArchive to decompress the garbled characters. The same reason applies when ZipArchive compressed files appear garbled or cannot be compressed.
Solution
Don’t use the built-in ZipArchive::extractTo() method to decompress ZipArchive documents, use PHP instead The zip packaging protocol is decompressed to solve the ZipArchive garbled code.
Solution code
<?php $zip = new \ZipArchive; $zipfile = "./test.zip"; $res = $zip->open($zipfile); $toDir = "./test"; if(!file_exists($toDir)) { mkdir($toDir); } $docnum = $zip->numFiles; for($i = 0; $i < $docnum; $i++) { $statInfo = $zip->statIndex($i); if($statInfo['crc'] == 0) { //新建目录 mkdir($toDir.'/'.substr($statInfo['name'], 0,-1)); } else { //拷贝文件 copy('zip://'.$zipfile.'#'.$statInfo['name'], $toDir.'/'.$statInfo['name']); } } print_r(scandir($toDir));
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if the php decompression zip is garbled. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
