JArchive::create for Joomla 2.5?
Asked Answered
D

2

6

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.

Divinity answered 18/6, 2012 at 13:16 Comment(3)
is there any specific reason for using relative path ?Purism
@Anand: yes because when I use an absolute path, it actually zips the whole absolute path, starting from the public_html folder.Divinity
i think $zipFilesArray should be the array containing the files with full path , not just the file name. Also data is not required in $zipFilesArrayPurism
D
2

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();
Divinity answered 27/8, 2012 at 15:21 Comment(0)
F
0
    jimport( 'joomla.filesystem.archive' );

    $zipFilesArray = array();
    $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($tmp_path.DS.'files.zip', $zipFilesArray);

This works for me. You may try and see.

Feather answered 1/7, 2012 at 20:45 Comment(1)
I have tried this and it zips the absolute path. I need it to zip the relative path.Divinity

© 2022 - 2024 — McMap. All rights reserved.