switched zip file creation to ZipStreamer to create zip files directly in memory

This commit is contained in:
Nicolai Ehemann 2014-01-22 12:49:52 +01:00
parent 791772abea
commit 99ad4e8000
1 changed files with 9 additions and 33 deletions

View File

@ -20,6 +20,7 @@
* License along with this library. If not, see <http://www.gnu.org/licenses/>. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
require_once( 'ZipStreamer/ZipStreamer.php' );
class GET_TYPE { class GET_TYPE {
const FILE = 1; const FILE = 1;
@ -32,8 +33,6 @@ class GET_TYPE {
* *
*/ */
class OC_Files { class OC_Files {
static $tmpFiles = array();
static public function getFileInfo($path, $includeMountPoints = true){ static public function getFileInfo($path, $includeMountPoints = true){
return \OC\Files\Filesystem::getFileInfo($path, $includeMountPoints); return \OC\Files\Filesystem::getFileInfo($path, $includeMountPoints);
} }
@ -103,12 +102,7 @@ class OC_Files {
} }
} else { } else {
self::validateZipDownload($dir, $files); self::validateZipDownload($dir, $files);
$zip = new ZipArchive(); $zip = new ZipStreamer(false);
$filename = OC_Helper::tmpFile('.zip');
if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==true) {
$l = OC_L10N::get('lib');
throw new Exception($l->t('cannot open "%s"', array($filename)));
}
} }
OC_Util::obEnd(); OC_Util::obEnd();
if ($zip or \OC\Files\Filesystem::isReadable($filename)) { if ($zip or \OC\Files\Filesystem::isReadable($filename)) {
@ -132,9 +126,9 @@ class OC_Files {
foreach ($files as $file) { foreach ($files as $file) {
$file = $dir . '/' . $file; $file = $dir . '/' . $file;
if (\OC\Files\Filesystem::is_file($file)) { if (\OC\Files\Filesystem::is_file($file)) {
$tmpFile = \OC\Files\Filesystem::toTmpFile($file); $fh = \OC\Files\Filesystem::fopen($file, 'r');
self::$tmpFiles[] = $tmpFile; $zip->addFileFromStream($fh, basename($file));
$zip->addFile($tmpFile, basename($file)); fclose($fh);
} elseif (\OC\Files\Filesystem::is_dir($file)) { } elseif (\OC\Files\Filesystem::is_dir($file)) {
self::zipAddDir($file, $zip); self::zipAddDir($file, $zip);
} }
@ -143,20 +137,7 @@ class OC_Files {
$file = $dir . '/' . $files; $file = $dir . '/' . $files;
self::zipAddDir($file, $zip); self::zipAddDir($file, $zip);
} }
$zip->close(); $zip->finalize();
if ($xsendfile) {
$filename = OC_Helper::moveToNoClean($filename);
self::addSendfileHeader($filename);
} else {
$handle = fopen($filename, 'r');
if ($handle) {
$chunkSize = 8 * 1024; // 1 MB chunks
while (!feof($handle)) {
echo fread($handle, $chunkSize);
flush();
}
}
}
set_time_limit($executionTime); set_time_limit($executionTime);
} else { } else {
if ($xsendfile) { if ($xsendfile) {
@ -170,11 +151,6 @@ class OC_Files {
\OC\Files\Filesystem::readfile($filename); \OC\Files\Filesystem::readfile($filename);
} }
} }
foreach (self::$tmpFiles as $tmpFile) {
if (file_exists($tmpFile) and is_file($tmpFile)) {
unlink($tmpFile);
}
}
} }
private static function addSendfileHeader($filename) { private static function addSendfileHeader($filename) {
@ -210,9 +186,9 @@ class OC_Files {
$filename=$file['name']; $filename=$file['name'];
$file=$dir.'/'.$filename; $file=$dir.'/'.$filename;
if(\OC\Files\Filesystem::is_file($file)) { if(\OC\Files\Filesystem::is_file($file)) {
$tmpFile=\OC\Files\Filesystem::toTmpFile($file); $fh = \OC\Files\Filesystem::fopen($file, 'r');
OC_Files::$tmpFiles[]=$tmpFile; $zip->addFileFromStream($fh, $internalDir.$filename);
$zip->addFile($tmpFile, $internalDir.$filename); fclose($fh);
}elseif(\OC\Files\Filesystem::is_dir($file)) { }elseif(\OC\Files\Filesystem::is_dir($file)) {
self::zipAddDir($file, $zip, $internalDir); self::zipAddDir($file, $zip, $internalDir);
} }