Return to Snippet

Revision: 32642
at October 1, 2010 00:47 by nvcesar


Initial Code
<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Clase usada para comprimir los xml dentro de un zip
 * el archivo se llama ArchivoZip.inc.php
 * @author Cesar Nava Camacho
 */

class ArchivoZip{
    var $zip;
    var $nombreArchivo;
    function ArchivoZip(){
        //Se crea un archivo temporal en la carpeta temporal default del sistema servidor
        $this->nombreArchivo = tempnam(sys_get_temp_dir(), "zip");
        $this->zip = new ZipArchive();
        $this->zip->open($this->nombreArchivo, ZipArchive::OVERWRITE);
        $this->zip->addEmptyDir("xmlRedalyc");
    }

    function agregarArchivo($archivo, $nombre){
        $nombre = str_replace(" ", "_", $nombre);
        $this->zip->addFromString("xmlRedalyc/ $nombre.xml", $archivo);
    }

    function cerrarZip(){
        $this->zip->close();
        header("Content-type: application/zip");
        header("Content-Disposition: attachment; filename=$this->nombreArchivo.zip");
        header("Content-Transfer-Encoding: binary");
        readfile($this->nombreArchivo);
        unlink($this->nombreArchivo);//Destruye el archivo temporal
    }
}

?>

Initial URL


Initial Description
Lo dicho, una clase en PHP que agrega archivos a un zip y forza su descarga, para este caso los archivos que agrega son XML.

Initial Title
Agregar Archivos a un zip y forzar su descarga

Initial Tags


Initial Language
PHP