Skip to content

Commit

Permalink
Use native zip extension if possible (Xerte_Zip_Factory::factory(...)).
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGoodwin committed May 2, 2014
1 parent 921e45d commit ff77318
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
22 changes: 22 additions & 0 deletions library/Xerte/Zip/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Creates ZipFileobjects which abide to Xerte_Zip_Interface.
*
* @see zip_file
* @see Xerte_Zip_Interface
* @see export.php
*/
class Xerte_Zip_Factory {

public static function factory($tempfilename, $options) {
if (extension_loaded('zip')) {
return new Xerte_Zip_Native($tempfilename, $options);
} else {
// Use the legacy Zip thing - note it may hit memory limit(s). :-(
$zip = new zip_file($tempfilename);
$zip->set_options($options);
return $zip;
}
}
}
23 changes: 23 additions & 0 deletions library/Xerte/Zip/Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php


/**
* functions required in an object which zips stuff up.
* These seem to be the ones used by export.php....
*/
interface Xerte_Zip_Interface {

/* array or single string file */
public function add_files($file_or_list);
public function set_options(array $array);

/* signal we've added everything and we're ready to package it all up */
public function create_archive();

/**
* write stuff out to the browser; provide $downloadName
* as the attachment file name that appears to the user
* @param string $downloadName
*/
public function download_file($downloadName);
}
84 changes: 84 additions & 0 deletions library/Xerte/Zip/Native.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of Native
*
* @author david
*/
class Xerte_Zip_Native implements Xerte_Zip_Interface {
/* @var $zip ZipArchive */

private $zip = null;
public $error = array();

/* @var $filename string file path to where the zip thing is */
private $filename = null;

public function __construct($filename, $options) {
_debug("Welcome");
$this->filename = $filename;
$this->options = $options;
$this->zip = new ZipArchive();

$ok = $this->zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
if (!$ok) {
$this->error[] = "Can't read $filename / create archive / overwrite archive";
error_log("Failed to create zip : $filename, $ok");
throw new Exception("Failed to create Zip: $filename");
}
}

public function add_files($things) {

if (!is_array($things)) {
$things = array($things);
}

foreach ($things as $thing) {
$localName = $thing;
if (isset($this->options['basedir'])) {
$localName = $thing;
$thing = $this->options['basedir'] . DIRECTORY_SEPARATOR . $thing;
}
_debug("Adding $localName (from: $thing) to zip");
$this->zip->addFile($thing, $localName);
}
}

public function set_options(array $options) {

}

public function create_archive() {
$this->zip->close();
}

public function download_file($downloadFilename) {
$fp = fopen($this->filename, 'rb');
$downloadFilename = preg_replace('/[^-_a-z0-9\.]/i', '', $downloadFilename);

header("Content-Type: application/zip");
header("Pragma: public");
header('Content-disposition: attachment; filename="' . $downloadFilename . '.zip"');
//header("Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0");
//header("Expires: Sat, 01 Jan 2000 12:00:00 GMT");
header("Content-Transfer-Encoding: binary");
$filesize = filesize($this->filename);

header("Content-Length: {$filesize}");

while (!feof($fp)) {
echo fread($fp, 8192);
}
//$bytes = fpassthru($fp);
_debug("Wrote : {$filesize} . bytes ... hopefully");
fclose($fp);
}

}

0 comments on commit ff77318

Please sign in to comment.