Symfony2 ZIP Service Bundle with minimum memory usage
Using the vendors script
Add the following lines in your deps
file:
[itze88ZipBundle]
git=git://github.com/itze88/itze88ZipBundle.git
target=bundles/itze88/ZipBundle
Now, run the vendors script to download the bundle:
$ php bin/vendors install
Using submodules
If you prefer instead to use git submodules, the run the following:
$ git submodule add git://github.com/itze88/itze88ZipBundle.git vendor/bundles/itze88/ZipBundle
$ git submodule update --init
Add the itze88
namespace to your autoloader:
<?php
// app/autoload.php
$loader->registerNamespaces(array(
// ...
'itze88' => __DIR__.'/../vendor/bundles',
));
Finally, enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new itze88\ZipBundle\itze88ZipBundle(),
);
}
The ZIP Manager is available in the container as the itze88.zip.manager
service.
$zipManager = $container->get('itze88.zip.manager');
A new ZIP-File can be created by setting the output-path in the ZIP-Manager.
$zipManager->setOutputFile('/var/www/symfony2/web/downloadableContent/');
Note:
The path always have to endup with /
Now you can add some directorys to your zip
$zm->addDir('zipFolder1/');
Note:
With this method you have to add each directory
For expample:
//First add 2 folders in the root of the zip
$zm->addDir('folder1/');
$zm->addDir('folder2/');
//Then you have to add the subfolders
$zm->addDir('folder1/subfolder1');
$zm->addDir('folder1/subfolder2');
$zm->addDir('folder1/subfolder1/subsubfolder1');
$zm->addDir('folder2/subfolder1');
Now you can add a file to your zip
$zm->addFile('/var/www/symfony2/availableContent/zipThis/thisFile.txt', 'thisFile.txt');
Note:
With this method you have to create the hole structure
For expample:
If you want to add a File /var/www/symfony2/availableContent/folder1/thisFile.txt and you want to hold the structure form folder1/
//First add the Folder to the zip
$zm->addDir('zipThis/');
//Then you have to add the files
//First parameter is where the file exists on file system
//and second where it should appear in zip and with which name
$zm->addFile('/var/www/symfony2/availableContent/zipThis/thisFile.txt', 'zipThis/thisFile.txt');
$zm->addDirectory('/var/www/symfony2/availableContent/zipThis/');
For expample:
If zipThis/ has over 100 Files and you download it and unzip it, all files will appear in the directory where you unzip the file If you have more than one File you should use the next Method
$zm->addDirectory('/var/www/symfony2/availableContent/zipThis/', 'addEverythingToThisZipFolder/');
For expample:
If zipThis/ has over 100 Files and you download it and unzip it, all files will appear in one directory addEverythingToThisZipFolder/
branch 'master' of https://github.com/itze88/itze88ZipBundle.git
Credits Original source (CreateZipFile class) was created by Rochak Chauhan - www.rochakchauhan.com. Modified by ironhawk, attilaw '@' cygnussystems.hu Bundled and extended by Marc Itzenthaler (github(at)web-mi.de), on GitHub:(https://github.com/itze88)