Processing Zip files in PHP, especially extracting specific types of files (such as .txt text files), can be done through the ZipArchive class or the underlying zip_read function. Although ZipArchive is more modern and easy to use, zip_read is still valuable in some older versions or special scenarios. This article will introduce how to use zip_read to read all .txt file contents in Zip files.
First, make sure that zip extension is enabled in your PHP environment. Check if the following line is included in php.ini :
extension=zip
You can also check by executing the following PHP code:
<?php
if (!function_exists('zip_open')) {
die("ZIP Extension not enabled!");
}
?>
Here is a complete example showing how to open a Zip file, iterate through all entries in it, and read only the contents of the file with the extension .txt .
<?php
$zipFile = '/path/to/archive.zip'; // Replace with the actual path
$zip = zip_open($zipFile);
if (is_resource($zip)) {
while ($entry = zip_read($zip)) {
$fileName = zip_entry_name($entry);
// Process only .txt document
if (pathinfo($fileName, PATHINFO_EXTENSION) === 'txt') {
if (zip_entry_open($zip, $entry, 'r')) {
echo "读取document: $fileName\n";
$contents = zip_entry_read($entry, zip_entry_filesize($entry));
echo "The content is as follows:\n$contents\n\n";
zip_entry_close($entry);
}
}
}
zip_close($zip);
} else {
echo "Unable to open ZIP document。\n";
}
?>
Path verification : The file path returned by zip_entry_name() may contain a directory structure or attempt to perform path traversal. Be sure to verify its security when using it.
Limit read size : Some .txt files can be very large and should be restricted when zip_entry_read() to prevent memory overflow.
Error handling : The above code does not include complete error handling logic. Logs and exception handling mechanisms should be added according to specific circumstances in the production environment.
Suppose you are developing an online batch processing platform that allows users to upload Zip packages containing multiple .txt files and perform statistical or format conversion of content. Through the above method, the text content can be easily extracted and subsequent processing logic can be called.
You can integrate this logic into the upload processing script, for example:
$uploadedZip = $_FILES['file']['tmp_name'];
// 对上传document进行检查后调用读取函数
processTxtFilesFromZip($uploadedZip);
Combining the upload form and security verification functions, a simple Zip file processing platform can be built. Visit https://m66.net/tools/zip-reader to get more actual demonstrations and source code references for such tools.
Although the zip_read interface is a bit cumbersome than ZipArchive , it is still of reference value when performing fine operations on Zip files. By reasonably combining zip_read and zip_entry_* series functions, you can flexibly read specific types of files such as .txt for various automation processing and background batch tasks.