|
Written by Alex Balyuk
|
|
Wednesday, 08 September 2010 19:14 |
|
I was working on a custom Joomla 1.5 component for a company that required downloading multiple CSV files. Downloading files one at a time would be annoying to the user, besides the size of these files was over 2MB each. Compression is the way to go! Joomla 1.5 has a wonderful class JArchive, that can compress files gzip style. The client wanted download a zip file, likely Joomla has another class called JArchiveZip . This class can create zip archives, unfortunately it is not as easy to use as JArchive. So I went ahead and created a wrapper class that makes using JArchiveZip a peace of cake!
Unlike JArchive, JArchiveZip does not just take paths to files that you want to archive. Instead you need to generate array with 'data' and 'name' keys and pass the array. No more. Below is a class that eliminates coding labor and makes it extremely easy to create zip archive withing Joomla Framework. The class is for archiving only. For extraction use JArchive class and call extract method, see documentation .
If you want to create zip archives with one or multiple files this class will make it much easier. It will save you head ache and time!
Below is a the code for the class.
<?php
/**
* @version $Id$
* @package Joomla
* @subpackage Random Code Samples
* @link http://www.boolcast.com/development/joomla-zip-archiver
* @copyright Copyright (C) 2010 Oleksandr Balyuk (www.boolcast.com)
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL v2
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.filesystem.file');
require_once JPATH_LIBRARIES . 'joomla' . DS . 'filesystem' . DS . 'archive' . DS . 'zip.php';
/**
* ZipArchiver is a wrapper class for JArchiveZip
* Makes it easy to archive collection of files into a zip archive.
*
* @author Oleksandr Balyuk <obalyuk@boolcast.com>
*/
class ZipArchiver extends JObject {
/**
* Exports file
*
* @param array $files array with files paths
* @param string $archive name of the archive
* @param string $path path to the archive without slash at the end. .e.g. /path/to/directory
* @param bool $overwrite if true will overwrite the archive if it exists
* @return true on success
*/
public function archive($files, $archive, $path, $overwrite=true) {
// Check to see if directory for archive exists
if (!JFolder::exists($path)) {
// Try to create the directory if it does not exists
if (!JFolder::create($path)) {
// Raise error, unable to create dir
$this->setError('Unable to create directory for archive');
return false;
}
}
// Check if archive exists
$archiveFilePath = $path . DS . $archive;
if (JFile::exists($archiveFilePath)) {
// If overwrite flag is set
if ($overwrite) {
// Delete archive
JFile::delete($archiveFilePath);
} else {
// Set error and return
$this->setError('Archive exists');
return false;
}
}
// Prepare files for the zip archiver
$filesToZip = array();
foreach ($files as $file) {
if (JFile::exists($file)) {
$filesToZip[] = array(
'data' => JFile::read($file),
'name' => JFile::getName($file)
);
}
}
// Create ZIP archiver
$archiver = new JArchiveZip();
// Create Archive
$isSuccess = $archiver->create($archiveFilePath, $filesToZip);
// Check of operation was successful
if (!$isSuccess) {
// Set Error
$this->setError('Unable to create archive');
}
//
return $isSuccess;
}
}
You can download ZipArchiver and example controller below. Controller shows how to add files to archive and how to call methods to create the archive.
| Joomla Zip Archiver
Version:1.0
|
GNU/GPL v2
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
Joomla 1.5 3.46 KB |
 |
|
|
Last Updated on Wednesday, 08 September 2010 20:40 |
Comments
I've tested your class, thanks for it !
Great job. Only one issue, it missed a DS when calling a library file :
require_once JPATH_LIBRARIES . DS . 'joomla' . DS . 'filesystem' . DS . 'archive' . DS . 'zip.php';