我需要在zip文件中读取单个文件“ test.txt”的内容。整个zip文件是一个非常大的文件(2gb),其中包含很多文件(10,000,000),因此,提取整个文件对我而言不是可行的解决方案。如何读取单个文件?
尝试使用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;
如何判断文件是否存在?if(!@ $ test = file_get_contents(...?
@ user1243068:我不确定。那应该工作。您可以
is_file
用来检查是否.zip
存在。至于zip中的文件,我不确定。它还适用于文件,例如$ content = file('zip://test.zip#test.txt');
我有
.tar.gz
文件而不是.zip
文件,如何使用file_get_contents('zip://test.zip#test.txt');
?@Sami:
phar://
包装器似乎支持这一点。file_get_contents('phar://yourfile.tar.gz/path/to/file.txt');
请参阅:stackoverflow.com/a/4878849