JArchive :: create for Joomla 2.5?

I am basically trying to compress a directory from a relative path using the Joomla JArchive::create() function. So far I can zip the directory, but it zips the entire full path.

The code I use is that the zip absolute path is 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 this has something to do with using the JPATH_SITE structure, which I tried to modify in the JURI::root structure, but then provides an error saying that this is not a valid path.

I could tell me how to encode the relative path in Joomla based on the code I provided, but that would be very helpful.

+6
source share
2 answers

Finally, I have some code that was kindly provided 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(); 
+2
source
  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 can try and see.

0
source

Source: https://habr.com/ru/post/918366/


All Articles