public function createZip() { /* Check if file is cached and still valid */ $cachedfolder = $this->getFolder(); if ($cachedfolder === false || $cachedfolder['folder'] === false) { return new WP_Error('broke', __("Requested directory isn't allowed", 'useyourdrive')); } $folder = $cachedfolder['folder']->getItem(); /* Check if entry is allowed */ if (!$this->_isEntryAuthorized($cachedfolder['folder'])) { return new WP_Error('broke', __("Requested directory isn't allowed", 'useyourdrive')); } /* Create upload dir if needed */ $zip_filename = '_zip_' . basename($folder->getTitle()) . '_' . uniqid() . '.zip'; $json_options = 0; if (defined('JSON_PRETTY_PRINT')) { $json_options |= JSON_PRETTY_PRINT; // Supported in PHP 5.4+ } if (isset($_REQUEST['files'])) { $dirlisting = array('folders' => array(), 'files' => array(), 'bytes' => 0, 'bytes_total' => 0); foreach ($_REQUEST['files'] as $fileid) { $cached_file = $this->getEntry($fileid); $data = $this->_getRecursiveFiles($cached_file, '', true); $dirlisting['files'] = array_merge($dirlisting['files'], $data['files']); $dirlisting['folders'] = array_merge($dirlisting['folders'], $data['folders']); $dirlisting['bytes_total'] += $data['bytes_total']; } } else { $dirlisting = $this->_getRecursiveFiles($cachedfolder['folder']); } if (count($dirlisting['folders']) > 0 || count($dirlisting['files']) > 0) { /* Create zip file */ if (!function_exists('PHPZip\\autoload')) { try { require_once "PHPZip/autoload.php"; } catch (Exception $ex) { return new WP_Error('broke', __('Something went wrong... See settings page', 'useyourdrive')); } } $zip = new PHPZip\Zip\File\Zip($zip_filename); /* Add folders */ if (count($dirlisting['folders']) > 0) { foreach ($dirlisting['folders'] as $key => $folder) { $zip->addDirectory($folder); unset($dirlisting['folders'][$key]); } } /* Add files */ if (count($dirlisting['files']) > 0) { foreach ($dirlisting['files'] as $key => $file) { usleep(125000); set_time_limit(60); /* get file */ $request = new Google_Http_Request($file['url'], 'GET'); try { $httpRequest = $this->client->getAuth()->authenticatedRequest($request); } catch (Exception $ex) { continue; } if ($httpRequest->getResponseHttpCode() == 200) { ob_flush(); $stream = fopen('php://memory', 'r+'); fwrite($stream, $httpRequest->getResponseBody()); rewind($stream); try { $zip->addLargeFile($stream, $file['path']); } catch (Exception $ex) { } fclose($stream); $dirlisting['bytes'] += $file['bytes']; unset($dirlisting['files'][$key]); } } } /* Close zip */ $result = $zip->sendZip($zip_filename); die; } else { die('No files or folders selected'); } }
$zip->addFile("Hello World!", "hello.txt"); @$handle = opendir($fileDir); if ($handle) { /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { if (strpos($file, ".php") !== false) { $pathData = pathinfo($fileDir . $file); $fileName = $pathData['filename']; $zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file), NULL, TRUE, Zip::getFileExtAttr($file)); } } } $zip->sendZip("ZipErrorCatcher1.zip", "application/zip", "ZipErrorCatcher1.zip"); // If non-fatal errors occurred during execution, this will append them // to the end of the generated file. // It'll create an invalid Zip file, however chances are that it is invalid // already due to the error happening in the first place. // The idea is that errors will be very easy to spot. if (!empty($errors)) { echo "\n<pre>\n**************\n*** ERRORS ***\n**************\n\n$errors\n</pre>\n"; } function customError($error_level, $error_message, $error_file, $error_line) { global $errors; switch ($error_level) { case 1: $e_type = 'E_ERROR'; $exit_now = true; break; case 2: $e_type = 'E_WARNING'; break;
$zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file), NULL, TRUE, Zip::getFileExtAttr($file)); } } } // Uses my Lipsum generator from https://github.com/Grandt/PHPLipsumGenerator if(file_exists('./LipsumGenerator.php')) { require_once './LipsumGenerator.php'; $lg = new com\grandt\php\LipsumGenerator(); $zip->openStream("big one3.txt"); for ($i = 1 ; $i <= 20 ; $i++) { $zip->addStreamData("Chapter $i\r\n\r\n" . $lg->generate(300, 2500) . "\r\n"); } $zip->closeStream(); } $zip->sendZip("ZipExample3.zip", "application/zip", "ZipExample3.zip"); // If non-fatal errors occurred during execution, this will append them // to the end of the generated file. // It'll create an invalid Zip file, however chances are that it is invalid // already due to the error happening in the first place. // The idea is that errors will be very easy to spot. if (!empty($errors)) { echo "\n<pre>\n**************\n*** ERRORS ***\n**************\n\n$errors\n</pre>\n"; } function customError($error_level, $error_message, $error_file, $error_line) { global $errors; switch ($error_level) { case 1: $e_type = 'E_ERROR'; $exit_now = true; break; case 2: $e_type = 'E_WARNING'; break;
$zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file), NULL, TRUE, Zip::getFileExtAttr($file)); } } } */ // Add a directory, first recursively, then the same directory, but without recursion. // Naturally this requires you to change the path to ../test to point to a directory of your own. // $zip->addDirectoryContent("testData/test", "recursiveDir/test"); // $zip->addDirectoryContent("testData/test", "recursiveDir/testFlat", FALSE); //$zip->sendZip("ZipExample1.zip"); //$zip->sendZip("ZipExample1_€2,000.zip"); $zip->appendZip("../testData/test.zip", "appendedZip1/appendedZip2/../appendedZip3/appendedZip4/"); $zip->appendZip('../testData/test.zip', '/'); $zip->sendZip("ZipExample1_€2,000.zip", "application/zip", "ZipExample1_€2,000_utf8.zip"); // If non-fatal errors occurred during execution, this will append them // to the end of the generated file. // It'll create an invalid Zip file, however chances are that it is invalid // already due to the error happening in the first place. // The idea is that errors will be very easy to spot. if (!empty($errors)) { echo "\n<pre>\n**************\n*** ERRORS ***\n**************\n\n$errors\n</pre>\n"; } function customError($error_level, $error_message, $error_file, $error_line) { global $errors; switch ($error_level) { case 1: $e_type = 'E_ERROR'; $exit_now = true; break; case 2: $e_type = 'E_WARNING'; break;