Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JArchive::create for Joomla 2.5?

Basically I'm trying to compress a directory from a relative path using the Joomla JArchive::create() function. So far I can zip a directory but it zips the entire absolute path.

The code I am using that zip the absolute path is as shown below:

$zipFilesArray = array();
    $new_component_path = JPATH_SITE.'/'.'modules'.'/'.'mod_module_gen'.'/'.'package'.'/'.$new_folder_name;
    $dirs = JFolder::folders($new_component_path, '.', true, true);
    array_push($dirs, $new_component_path);
    foreach ($dirs as $dir) {
        $files = JFolder::files($dir, '.', false, true);
        foreach ($files as $file) {
            $data = JFile::read($file);
            $zipFilesArray[] = array('name' => str_replace($new_component_path.DS, '', $file), 'data' => $data);
        }
    }
    $zip = JArchive::getAdapter('zip');
    $zip->create($new_component_path.'/'.$new_folder_name.'.zip', $zipFilesArray);

I think is has something to do with using the JPATH_SITE structure which I have tried changing to the JURI::root structure but then provides an error saying that its not a valid path.

I anyone could tell me how to zip relative path in Joomla based on the code I have provided then this would be much appreciated.

like image 321
Lodder Avatar asked Jun 18 '12 13:06

Lodder


1 Answers

Finally have the code which was kindly given to me by another extension developer.

$folder_path = JPATH_SITE.'/modules/mod_xxxxxxxxx/package/'.$new_folder_name;
$new_folder_name_final = $folder_path . '.zip';

$zip = new ZipArchive();

if ($zip->open($new_folder_name_final, ZIPARCHIVE::CREATE) !== TRUE) {
    return false;
}

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder_path));

foreach ($iterator as $key=>$value) {
    $key = str_replace('\\', '/', $key);
    if (!is_dir($key)) {
        if(!$zip->addFile(realpath($key), substr($key, strlen($folder_path) - strlen(basename($folder_path))))) {
            return false;
        }
    }
$zip->close();
like image 158
Lodder Avatar answered Oct 22 '22 13:10

Lodder