2010-09-30 18 views

Respuesta

14

Aquí se muestra un ejemplo:

<?php 

// Adding files to a .zip file, no zip file exists it creates a new ZIP file 

// increase script timeout value 
ini_set('max_execution_time', 5000); 

// create object 
$zip = new ZipArchive(); 

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

// initialize an iterator 
// pass it the directory to be processed 
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("themes/")); 

// iterate over the directory 
// add each file found to the archive 
foreach ($iterator as $key=>$value) { 
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); 
} 

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

código genial ... ¿cómo puedo editar esto para ZIP una carpeta anidada excluyendo todas las carpetas que conducen a ella? mi carpeta deseada vive en "/var/www/vhosts/mysite.com/dev/wp-content/themes/mytheme/". cuando ejecuto este script, obtengo una carpeta de inicio de var luego www luego vhosts luego mysite.com etc. ¿Qué me estoy perdiendo? –

+0

Entiendo bien que el código itera también a través de "." y "..." directorios dentro de "temas /"? Parece que esto causa problemas cuando intenta descomprimir el archivo. – Tamara

+0

este código causa un problema al descomprimir el archivo. –

3

Cuidado con un posible problema en el ejemplo de Adnan: Si el myarchive.zip de destino está en la carpeta de la fuente, entonces usted necesita para excluirlo en el bucle, o para ejecuta el iterador antes de crear el archivo de almacenamiento (si no existe ya). Aquí hay una secuencia de comandos revisada que utiliza la última opción y agrega algunas variables de configuración arriba. Este no debe usarse para agregar a un archivo existente.

<?php 
// Config Vars 

$sourcefolder = "./"   ; // Default: "./" 
$zipfilename = "myarchive.zip"; // Default: "myarchive.zip" 
$timeout  = 5000   ; // Default: 5000 

// instantate an iterator (before creating the zip archive, just 
// in case the zip file is created inside the source folder) 
// and traverse the directory to get the file list. 
$dirlist = new RecursiveDirectoryIterator($sourcefolder); 
$filelist = new RecursiveIteratorIterator($dirlist); 

// set script timeout value 
ini_set('max_execution_time', $timeout); 

// instantate object 
$zip = new ZipArchive(); 

// create and open the archive 
if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) { 
    die ("Could not open archive"); 
} 

// add each file in the file list to the archive 
foreach ($filelist as $key=>$value) { 
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); 
} 

// close the archive 
$zip->close(); 
echo "Archive ". $zipfilename . " created successfully."; 

// And provide download link ?> 
<a href="http:<?php echo $zipfilename;?>" target="_blank"> 
Download <?php echo $zipfilename?></a> 
+0

No se descomprime. muestra error al descomprimir –

Cuestiones relacionadas