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

e-i*_*128 32 php zip gzip zlib

我需要在zip文件中读取单个文件"test.txt"的内容.整个zip文件是一个非常大的文件(2gb),包含很多文件(10,000,000),因此提取整个文件对我来说不是一个可行的解决方案.我怎样才能读取单个文件?

Roc*_*mat 53

尝试使用zip://包装器:

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

你也可以用file_get_contents:

$result = file_get_contents('zip://test.zip#test.txt'); 
echo $result;
Run Code Online (Sandbox Code Playgroud)

  • 它也适用于文件,如$ content = file('zip://test.zip#test.txt'); (2认同)
  • @Sami:[`phar://`包装器](http://php.net/manual/en/phar.using.stream.php)似乎支持这一点.`file_get_contents('phar://yourfile.tar.gz/path/to/file.txt');`参见:http://stackoverflow.com/a/4878849 (2认同)