Skip to content

Instantly share code, notes, and snippets.

@fretn
Created January 29, 2015 13:18
Show Gist options
  • Select an option

  • Save fretn/e1fca984124150ddaf58 to your computer and use it in GitHub Desktop.

Select an option

Save fretn/e1fca984124150ddaf58 to your computer and use it in GitHub Desktop.
<?php
// written by frlae - 2015 01 27
// replacement class for the buggy zipstreamer class (on osx you cannot unzip the zip files)
class ZipStreamer {
private $archivename = "";
private $isFinalized = false;
private $filelist = array();
private $dirlist = array();
private $cwd = "";
function __construct($dummy = false) {
$this->archivename = tempnam(OC_Config::getValue( 'datadirectory' ) . "/tmp", "owncloud_tmp_zip");
$this->debug("New Archive: " . $this->archivename);
}
function __destruct() {
$this->isFinalized = true;
$this->cwd = "";
}
private function debug($msg) {
return;
system("echo '".escapeshellcmd($msg)."' >> /tmp/output");
}
private function addfile($file) {
array_push($this->filelist, $file);
}
private function addir($dir) {
array_push($this->dirlist, $dir);
}
public function addFileFromStream($fh, $filename_basename) {
if ($this->isfinalized) {
return false;
}
$meta_data = stream_get_meta_data($fh);
$filename_fullpath = $meta_data["uri"];
if ($this->cwd == "") {
$i = strpos($filename_fullpath, $filename_basename);
$dir = substr($filename_fullpath, 0, $i);
$this->cwd = $dir;
}
$i = strrpos($filename_basename, "/");
$createdir = substr($filename_basename, 0, $i);
$this->addir($createdir);
$this->addfile($filename_basename);
}
public function addEmptyDir($dirname) {
if ($this->isfinalized) {
return false;
}
return true;
}
public function finalize() {
//ignore_user_abort(true);
if (!$this->isfinalized) {
// create folders
for ($i=0; $i<sizeof($this->dirlist); $i++) {
system("cd " . escapeshellcmd($this->cwd) . " && zip '" . escapeshellcmd($this->archivename) . "' '" . escapeshellcmd($this->dirlist[$i]) . '\' > /dev/null 2>&1 ');
$this->debug("cd " . $this->cwd . " && zip " . $this->archivename . " " . $this->dirlist[$i] . ' > /dev/null 2>&1');
}
$this->debug("finished creating folders");
// add the files
for ($i=0; $i<sizeof($this->filelist); $i++) {
system("cd " . escapeshellcmd($this->cwd) . " && zip '" . escapeshellcmd($this->archivename) . "' '" . escapeshellcmd($this->filelist[$i]) . '\' > /dev/null 2>&1');
$this->debug("cd " . $this->cwd . " && zip " . $this->archivename . " " . $this->filelist[$i] . ' > /dev/null 2>&1');
}
$this->debug("finished zipping files");
$file = $this->archivename . ".zip";
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
// this fails on osx
// readfile($file);
// flush();
$fh = fopen($file,"rb");
if ($fh) {
while (!feof($fh)) {
echo fread($fh, 8192);
}
fclose($fh);
}
$this->debug("#####################");
$this->debug("\n");
$this->isFinalized = true;
@unlink($this->archivename);
@unlink($this->archivename . ".zip");
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment