PHP从zip文件目录中读取文本文件
答
PHP有an extension,可让您与ZIP档案工作列出所有文件。
请注意,要访问一个文件,您不需要解压缩整个存档。 ZipArchive::extractTo方法允许您指定要提取的内容。
$zip = new ZipArchive;
$res = $zip->open('test.zip');
$zip->extractTo('my/extract/folder/', 'foo.txt');
答
您可以使用PHP zip扩展为:
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";
}
}
+1
你为什么要提取整个档案? – 2010-12-13 10:29:09
答
你是否采取了看P HP5's ZipArchive
functions?
// 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
你必须解压缩它。但不是整个档案。您只能解压缩所需的文件。 – 2010-12-13 10:24:39