2010-12-13 177 views

回答

3

PHP有an extension,可让您与ZIP档案工作列出所有文件。

请注意,要访问一个文件,您不需要解压缩整个存档。 ZipArchive::extractTo方法允许您指定要提取的内容。

$zip = new ZipArchive; 
$res = $zip->open('test.zip'); 
$zip->extractTo('my/extract/folder/', 'foo.txt'); 
1

据我所知,压缩数据不能以这种方式访问​​,你必须首先解压。

如果你是舒服,虽然,你可以使用这个PHP扩展:http://www.php.net/manual/en/book.zip.php

+1

你必须解压缩它。但不是整个档案。您只能解压缩所需的文件。 – 2010-12-13 10:24:39

2

您可以使用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

1

你是否采取了看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 

一个乐于助人的SO question: https://stackoverflow.com/questions/4085333/modifying-a-single-text-file-in-a-zip-file-in-php