温馨提示:本文翻译自stackoverflow.com,查看原文请点击:php - How to read a single file inside a zip archive
gzip php zip zlib

php - 如何读取zip存档中的单个文件

发布于 2020-04-25 16:04:11

我需要在zip文件中读取单个文件“ test.txt”的内容。整个zip文件是一个非常大的文件(2gb),其中包含很多文件(10,000,000),因此,提取整个文件对我而言不是可行的解决方案。如何读取单个文件?

查看更多

提问者
e-info128
被浏览
14
36.9k 2013-08-11 05:51

尝试使用zip://包装器

$handle = fopen('zip://test.zip#test.txt', 'r'); 
$result = '';
while (!feof($handle)) {
  $result .= fread($handle, 8192);
}
fclose($handle);
echo $result;

您也可以使用file_get_contents

$result = file_get_contents('zip://test.zip#test.txt'); 
echo $result;