From 309d7012a737f140076cabbe8873dc39f25331b2 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 4 Dec 2014 11:47:26 +0200 Subject: Zip library changes related to PR #3341 - Drop compression_encoding option, it requires PHP 5.4. - Change default compression_level to 2 as this was previously the hard-coded default. - Improve on the doc changes made in the PR. --- system/libraries/Zip.php | 24 ++++++-------- user_guide_src/source/changelog.rst | 2 +- user_guide_src/source/libraries/zip.rst | 56 ++++++++++++++++++++------------- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/system/libraries/Zip.php b/system/libraries/Zip.php index 5208c9149..2f6ab8b68 100644 --- a/system/libraries/Zip.php +++ b/system/libraries/Zip.php @@ -95,19 +95,15 @@ class CI_Zip { * @var int */ public $now; - - /** - * The level of compression. 0 to 9, 9 being the highest level of - * compression. - * @var int - */ - public $compression_level = 6; - - /** - * Which encoding to use. One of the ZLIB_ENCODING_* constants. - * @var int - */ - public $compression_encoding = ZLIB_ENCODING_DEFLATE; + + /** + * The level of compression + * + * Ranges from 0 to 9, with 9 being the highest level. + * + * @var int + */ + public $compression_level = 2; /** * Initialize zip compression class @@ -261,7 +257,7 @@ class CI_Zip { $uncompressed_size = strlen($data); $crc32 = crc32($data); - $gzdata = substr(gzcompress($data, $this->compression_level, $this->compression_encoding), 2, -4); + $gzdata = substr(gzcompress($data, $this->compression_level), 2, -4); $compressed_size = strlen($gzdata); $this->zipdata .= diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 08b692168..98026dc59 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -423,7 +423,7 @@ Release Date: Not Released - :doc:`Zip Library ` changes include: - Method ``read_file()`` can now also alter the original file path/name while adding files to an archive. - - Added support for changing the compression level and encoding used by gzcompress. + - Added support for changing the compression level. - :doc:`Trackback Library ` method ``receive()`` will now utilize ``iconv()`` if it is available but ``mb_convert_encoding()`` is not. diff --git a/user_guide_src/source/libraries/zip.rst b/user_guide_src/source/libraries/zip.rst index a6b98c2ac..0f255755e 100644 --- a/user_guide_src/source/libraries/zip.rst +++ b/user_guide_src/source/libraries/zip.rst @@ -25,7 +25,9 @@ your controller using the $this->load->library function:: $this->load->library('zip'); -Once loaded, the Zip library object will be available using: $this->zip +Once loaded, the Zip library object will be available using: + + $this->zip Usage Example ============= @@ -52,6 +54,14 @@ Class Reference .. class:: CI_Zip + .. attribute:: $compression_level = 2 + + The compression level to use. + + It can range from 0 to 9, with 9 being the highest and 0 effectively disabling compression:: + + $this->zip->compression_level = 0; + .. method:: add_data($filepath[, $data = NULL]) :param mixed $filepath: A single file path or an array of file => data pairs @@ -60,7 +70,8 @@ Class Reference Adds data to the Zip archive. Can work both in single and multiple files mode. - When adding a single file, the first parameter must contain the name you would like given to the file and the second must contain the file contents:: + When adding a single file, the first parameter must contain the name you would + like given to the file and the second must contain the file contents:: $name = 'mydata1.txt'; $data = 'A Data String!'; @@ -70,7 +81,8 @@ Class Reference $data = 'Another Data String!'; $this->zip->add_data($name, $data); - When adding multiple files, the first parameter must contain *file => contents* pairs and the second parameter is ignored:: + When adding multiple files, the first parameter must contain *file => contents* pairs + and the second parameter is ignored:: $data = array( 'mydata1.txt' => 'A Data String!', @@ -79,7 +91,8 @@ Class Reference $this->zip->add_data($data); - If you would like your compressed data organized into sub-directories, simply include the path as part of the filename(s):: + If you would like your compressed data organized into sub-directories, simply include + the path as part of the filename(s):: $name = 'personal/my_bio.txt'; $data = 'I was born in an elevator...'; @@ -88,18 +101,14 @@ Class Reference The above example will place my_bio.txt inside a folder called personal. - You can change the level of compression and encoding that is used by the gzcompress function during compression:: - - $this->zip->compression_level = 0; // Disable compression - $this->zip->compression_encoding = ZLIB_ENCODING_GZIP; - .. method:: add_dir($directory) :param mixed $directory: Directory name string or an array of multiple directories :rtype: void - Permits you to add a directory. Usually this method is unnecessary since you can place your data into directories when using - ``$this->zip->add_data()``, but if you would like to create an empty directory you can do so:: + Permits you to add a directory. Usually this method is unnecessary since you can place + your data into directories when using ``$this->zip->add_data()``, but if you would like + to create an empty directory you can do so:: $this->zip->add_dir('myfolder'); // Creates a directory called "myfolder" @@ -161,15 +170,16 @@ Class Reference // Download the file to your desktop. Name it "my_backup.zip" $this->zip->download('my_backup.zip'); - By default the Zip archive will place all directories listed in the first parameter inside the zip. - If you want the tree preceding the target directory to be ignored you can pass FALSE (boolean) in the second parameter. Example:: + By default the Zip archive will place all directories listed in the first parameter + inside the zip. If you want the tree preceding the target directory to be ignored, + you can pass FALSE (boolean) in the second parameter. Example:: $path = '/path/to/your/directory/'; $this->zip->read_dir($path, FALSE); - This will create a ZIP with a directory named "directory" inside, then all sub-directories stored correctly inside that, but will not include the - */path/to/your* part of the path. + This will create a ZIP with a directory named "directory" inside, then all sub-directories + stored correctly inside that, but will not include the */path/to/your* part of the path. .. method:: archive($filepath) @@ -177,8 +187,9 @@ Class Reference :returns: TRUE on success, FALSE on failure :rtype: bool - Writes the Zip-encoded file to a directory on your server. Submit a valid server path ending in the file name. - Make sure the directory is writable (755 is usually OK). Example:: + Writes the Zip-encoded file to a directory on your server. Submit a valid server path + ending in the file name. Make sure the directory is writable (755 is usually OK). + Example:: $this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip @@ -187,7 +198,8 @@ Class Reference :param string $filename: Archive file name :rtype: void - Causes the Zip file to be downloaded from your server. You must pass the name you would like the zip file called. Example:: + Causes the Zip file to be downloaded from your server. + You must pass the name you would like the zip file called. Example:: $this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip" @@ -200,7 +212,8 @@ Class Reference :returns: Zip file content :rtype: string - Returns the Zip-compressed file data. Generally you will not need this method unless you want to do something unique with the data. Example:: + Returns the Zip-compressed file data. Generally you will not need this method unless you + want to do something unique with the data. Example:: $name = 'my_bio.txt'; $data = 'I was born in an elevator...'; @@ -213,8 +226,9 @@ Class Reference :rtype: void - The Zip class caches your zip data so that it doesn't need to recompile the Zip archive for each method you use above. - If, however, you need to create multiple Zip archives, each with different data, you can clear the cache between calls. Example:: + The Zip class caches your zip data so that it doesn't need to recompile the Zip archive + for each method you use above. If, however, you need to create multiple Zip archives, + each with different data, you can clear the cache between calls. Example:: $name = 'my_bio.txt'; $data = 'I was born in an elevator...'; -- cgit v1.2.3-24-g4f1b