2011-04-15 88 views
1

我想要的代码,使树中的所有文件在根ZipArchive .zip文件显示所有文件在根

<?PHP 
// create object 
$zip = new ZipArchive(); 

// open archive 
if ($zip->open('my-archive.zip', ZIPARCHIVE::CREATE) !== TRUE) { 
    die ("Could not open archive"); 
} 

// list of files to add 
// list of files to add 
$fileList = array(
    'im/asd.pdf', 
    'im/df.pdf', 
    'im/d/qoyyum.txt' 
); 

// add files 
foreach ($fileList as $f) { 
    $zip->addFile($f) or die ("ERROR: Could not add file: $f"); 
} 

// close and save archive 
$zip->close(); 
echo "Archive created successfully.";  
?> 

例如文件夹.zip文件/ IM含有

/im/asd.pdf 
/im/df.pdf 
/im/d/qoyyum.txt 

的“my-archive.zip”提取物应该是这样的

my-archive/asd.pdf 
my-archive/df.pdf 
my-archive/qoyyum.txt 

我想阻止的文件夹层次提取拉链时。让每一个文件都应该在提取的ZIP文件夹的根

请建议小费做

回答

2

有一个称为localname(见here),它可以让你设置文件的本地名称在ZipArchive::addFile()第二个参数在zip档案中。使用它来覆盖默认的目录结构。

foreach ($fileList as $f) { 
    $filename_parts = explode('/', $f); // Split the filename up by the '/' character 
    $zip->addFile($f, end($filename_parts)) or die ("ERROR: Could not add file: $f"); 
} 
+0

谢谢,它的工作 – 2011-04-15 09:50:02

+0

伟大的解决方案,jackbot。 – 2bard 2011-04-15 11:14:23