SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    tearing out hair using PHP, bzip2 and zlib

    I want to compress and download a series of PDF's from the server.

    I have both bzip2 and zlib installed on the server.

    I have TWO scripts (below) that are working, however I am having different problems with each of them. If you can help with either script, it would be great.


    Script #1
    Problem: This script works great locally with WAMPP installed, however when I try it on the web server, it does not create the zip file in the directory of the script.

    Code PHP:
    <?php
     
    /* creates a compressed zip file */
    function create_zip($files = array(),$destination = '',$overwrite = false) {
    	//if the zip file already exists and overwrite is false, return false
    	if(file_exists($destination) && !$overwrite) { return false; }
    	//vars
    	$valid_files = array();
    	//if files were passed in...
    	if(is_array($files)) {
    		//cycle through each file
    		foreach($files as $file) {
    			//make sure the file exists
    			if(file_exists($file)) {
    				$valid_files[] = $file;
    			}
    		}
    	}
    	//if we have good files...
    	if(count($valid_files)) {
    		//create the archive
    		$zip = new ZipArchive();
    		if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
    			return false;
    		}
    		//add the files
    		foreach($valid_files as $file) {
    			$zip->addFile($file,$file);
    		}
    		//debug
    		//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
     
    		//close the zip -- done!
    		$zip->close();
     
    		//check to make sure the file exists
    		return file_exists($destination);
    	}
    	else
    	{
    		return false;
    	}
    }
     
     
    $files_to_zip = Array();
    $files_to_zip = array("test1.pdf","test2.pdf");
     
    //if true, good; if false, zip creation failed
    $result = create_zip($files_to_zip,'zipped-files.zip');
     
    ?>

    Script #2
    Problem: This script works on the server, however when the ZIP file is downloaded, the PDF files are 0kb in size and the pdf shows up as corrupt when opened.

    Code PHP:
    <?php
     
    class zipfile
    {
    var $datasec = array();
    var $ctrl_dir = array();
    var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
    var $old_offset = 0;
     
    function add_dir($name) {
    $name = str_replace("", "/", $name);
     
    $fr = "\x50\x4b\x03\x04";
    $fr .= "\x0a\x00";
    $fr .= "\x00\x00";
    $fr .= "\x00\x00";
    $fr .= "\x00\x00\x00\x00";
     
    $fr .= pack("V",0);
    $fr .= pack("V",0);
    $fr .= pack("V",0);
    $fr .= pack("v", strlen($name) );
    $fr .= pack("v", 0 );
    $fr .= $name;
    $fr .= pack("V", 0);
    $fr .= pack("V", 0);
    $fr .= pack("V", 0);
     
    $this -> datasec[] = $fr;
    $new_offset = strlen(implode("", $this->datasec));
     
    $cdrec = "\x50\x4b\x01\x02";
    $cdrec .="\x00\x00";
    $cdrec .="\x0a\x00";
    $cdrec .="\x00\x00";
    $cdrec .="\x00\x00";
    $cdrec .="\x00\x00\x00\x00";
    $cdrec .= pack("V",0);
    $cdrec .= pack("V",0);
    $cdrec .= pack("V",0);
    $cdrec .= pack("v", strlen($name) );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $ext = "\x00\x00\x10\x00";
    $ext = "\xff\xff\xff\xff";
    $cdrec .= pack("V", 16 );
    $cdrec .= pack("V", $this -> old_offset );
    $cdrec .= $name;
     
    $this -> ctrl_dir[] = $cdrec;
    $this -> old_offset = $new_offset;
    return;
    }
     
    function add_file($data, $name) {
    $fp = fopen($data,"r");
    $data = fread($fp,filesize($data));
    fclose($fp);
    $name = str_replace("", "/", $name);
    $unc_len = strlen($data);
    $crc = crc32($data);
    $zdata = gzcompress($data);
    $zdata = substr ($zdata, 2, -4);
    $c_len = strlen($zdata);
    $fr = "\x50\x4b\x03\x04";
    $fr .= "\x14\x00";
    $fr .= "\x00\x00";
    $fr .= "\x08\x00";
    $fr .= "\x00\x00\x00\x00";
    $fr .= pack("V",$crc);
    $fr .= pack("V",$c_len);
    $fr .= pack("V",$unc_len);
    $fr .= pack("v", strlen($name) );
    $fr .= pack("v", 0 );
    $fr .= $name;
    $fr .= $zdata;
    $fr .= pack("V",$crc);
    $fr .= pack("V",$c_len);
    $fr .= pack("V",$unc_len);
     
    $this -> datasec[] = $fr;
    $new_offset = strlen(implode("", $this->datasec));
     
    $cdrec = "\x50\x4b\x01\x02";
    $cdrec .="\x00\x00";
    $cdrec .="\x14\x00";
    $cdrec .="\x00\x00";
    $cdrec .="\x08\x00";
    $cdrec .="\x00\x00\x00\x00";
    $cdrec .= pack("V",$crc);
    $cdrec .= pack("V",$c_len);
    $cdrec .= pack("V",$unc_len);
    $cdrec .= pack("v", strlen($name) );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("V", 32 );
    $cdrec .= pack("V", $this -> old_offset );
     
    $this -> old_offset = $new_offset;
     
    $cdrec .= $name;
    $this -> ctrl_dir[] = $cdrec;
    }
     
    function file() {
    $data = implode("", $this -> datasec);
    $ctrldir = implode("", $this -> ctrl_dir);
     
    return
    $data .
    $ctrldir .
    $this -> eof_ctrl_dir .
    pack("v", sizeof($this -> ctrl_dir)) .
    pack("v", sizeof($this -> ctrl_dir)) .
    pack("V", strlen($ctrldir)) .
    pack("V", strlen($data)) .
    "\x00\x00";
    }
    }
     
    // Test this class
    $zipTest = new zipfile();
     
    $zipTest->add_file("test1.pdf", "test1.pdf");
    $zipTest->add_file("test2.pdf", "test2.pdf");
     
     
    // Return Zip File to Browser
    Header("Content-type: application/octet-stream");
    Header ("Content-disposition: attachment; filename=zipTest.zip");
    echo $zipTest->file();
     
     
    ?>

    Can anyone help with either of these two scripts or a new one?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Using the zlib libary functions.
    PHP Code:
    <?php

    $file 
    realpath__DIR__ '/file.tar' );
    $file preg_replace'~[\\\\/]+~''/'$file );
    $size filesize$file );

    $gf gzopen__DIR__ '/file.tar.gz''wb9' );
    $sf fopen$file'rb' );

    while ( !
    feof$sf ) ) gzwrite$gffread$sf$size ) );

    gzclose$gf );
    fclose$sf );

    print 
    'Complete';
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how could I loop through an array of .PDF's to create a tar ball then?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •