我有一个目录,里面包含一系列压缩文件。每个压缩文件中都有一个foo.txt文件。
如何从每个zip文件中读取这个文本文件?
我知道可以使用“glob”来列出目录中的所有文件。
如何从每个zip文件中读取这个文本文件?
我知道可以使用“glob”来列出目录中的所有文件。
PHP有一个扩展程序,允许您处理ZIP档案。
请注意,要访问该文件,您不需要解压整个存档文件。ZipArchive::extractTo方法允许您指定要提取的内容。
$zip = new ZipArchive;
$res = $zip->open('test.zip');
$zip->extractTo('my/extract/folder/', 'foo.txt');
foreach(glob('*.zip') as $zipFile) {
$zip = new ZipArchive;
if ($zip->open($zipFile) === TRUE) {
// get the filename without extension.
$filename = pathinfo($zipFile,PATHINFO_FILENAME);
// extract.
$zip->extractTo($filename);
$zip->close();
echo "Extracted contents of $zipFile to $filename","\n";
} else {
echo "Failed to open $zipFile","\n";
}
}
// you could extract the txt-file only and read in the content afterwards
$value = 'myzipfile.zip';
$entry = $zip->getNameIndex('foo.txt');
copy('zip://'.dirname(__FILE__).'/zip_files/'.$value.'#'.$entry, 'txt_files/'.$value.'.txt');
}
$zip->close();
// now you can access the file and do with the content what everyou like