/** * Get comment of archive or file. * @param string $mixed * @return bool @params mixed, string $mixed : comment by index(int), name(string), entire archive ('archive') */ function getComment($mixed = 'archive') { if (strtolower($mixed) === 'archive') { return $this->zip->getArchiveComment($mixed); } else { $constant = is_string($mixed) ? 'Name' : 'Index'; return $this->zip->{'getComment' . $constant}($mixed); } }
/** * */ public function adminGenerator() { $xml = new DOMDocument('1.0', 'utf-8'); $xml->preserveWhiteSpace = false; $xml->formatOutput = true; $root = $xml->createElement('backups'); $xml->appendChild($root); $root->appendChild($xml->createElement('suggest'))->nodeValue = date('YmdHis'); $list = scandir(__DIR__ . '/data/'); $zip = new ZipArchive(); foreach ($list as $filename) { if (pathinfo($filename, PATHINFO_EXTENSION) == 'zip') { $zip->open(__DIR__ . '/data/' . $filename); $description = $zip->getArchiveComment(); $zip->close(); $desc_node = $xml->createElement('backup'); $desc_node->appendChild($xml->createElement('filename'))->nodeValue = iconv('windows-1251', 'utf-8', $filename); $desc_node->appendChild($xml->createElement('filesize'))->nodeValue = $this->fileSizeDynamic(filesize(__DIR__ . '/data/' . $filename)); $desc_node->appendChild($xml->createElement('description'))->nodeValue = $description; $root->appendChild($desc_node); } } return XSLTransform($xml->saveXML($root), __DIR__ . '/form.xsl'); }
function get_comment($zip_file) { // Use ZipArchive if available if (in_array('ziparchive', $this->_zip_methods)) { // Make doubly sure it is available if (class_exists('ZipArchive', false)) { $za = new ZipArchive(); $result = $za->open($zip_file); // Make sure that at least the zip file opened ok if ($result === true) { // Get the comment or false on failure for some reason $comment = $za->getArchiveComment(); $za->close(); // If we have a comment (even if empty) then return it if ($comment !== false) { // Note: new archives will return an empty comment if one was not added at creation pb_backupbuddy::status('details', sprintf(__('ZipArchive retrieved comment in file %1$s', 'it-l10n-backupbuddy'), $zip_file)); // Format has changed and no longer encoding as htmlemtities when setting comment // For older backups may need to remove encoding - action _should_ be null if N/A // Only spanner would be if someone had put an entity in their comment but that is // really an outsider and in any case the correction is simply to edit and resave // TODO: Remove this when new format has been in use for some time $comment = html_entity_decode($comment); return $comment; } else { // If we failed to get the commnent then log it (?) and drop through pb_backupbuddy::status('details', sprintf(__('ZipArchive failed to retrieve comment in file %1$s', 'it-l10n-backupbuddy'), $zip_file)); } } else { // If we couldn't open the zip file then log it (?) and drop through $error_string = $this->ziparchive_error_info($result); pb_backupbuddy::status('details', sprintf(__('ZipArchive failed to open file to retrieve comment in file %1$s - Error Info: %2$s', 'it-l10n-backupbuddy'), $zip_file, $error_string)); } } else { // Something fishy - the methods indicated ziparchive but we couldn't find the class pb_backupbuddy::status('details', __('ziparchive indicated as available method but ZipArchive class non-existent', 'it-l10n-backupbuddy')); } } // Dropped through because ZipArchive not available or failed for some reason if (in_array('pclzip', $this->_zip_methods)) { // Make sure we have it if (!class_exists('PclZip', false)) { // It's not already loaded so try and find/load it from possible locations if (file_exists(ABSPATH . 'wp-admin/includes/class-pclzip.php')) { // Running under WordPress @(include_once ABSPATH . 'wp-admin/includes/class-pclzip.php'); } elseif (file_exists(pb_backupbuddy::plugin_path() . '/lib/pclzip/pclzip.php')) { // Running Standalone (importbuddy) @(include_once pb_backupbuddy::plugin_path() . '/lib/pclzip/pclzip.php'); } } // Make sure we did load it if (class_exists('PclZip', false)) { $za = new PclZip($zip_file); // Make sure we opened the zip ok and it has properties if (($properties = $za->properties()) !== 0) { // We got properties so should have a comment to return, even if empty pb_backupbuddy::status('details', sprintf(__('PclZip retrieved comment in file %1$s', 'it-l10n-backupbuddy'), $zip_file)); $comment = $properties['comment']; // Format has changed and no longer encoding as htmlemtities when setting comment // For older backups may need to remove encoding - action _should_ be null if N/A // Only spanner would be if someone had put an entity in their comment but that is // really an outsider and in any case the correction is simply to edit and resave // TODO: Remove this when new format has been in use for some time $comment = html_entity_decode($comment); return $comment; } else { // If we failed to get the commnent then log it (?) and drop through $error_string = $za->errorInfo(true); pb_backupbuddy::status('details', sprintf(__('PclZip failed to retrieve comment in file %1$s - Error Info: %2$s', 'it-l10n-backupbuddy'), $zip_file, $error_string)); } } else { // Something fishy - the methods indicated pclzip but we couldn't find the class pb_backupbuddy::status('details', __('pclzip indicated as available method but class PclZip non-existent', 'it-l10n-backupbuddy')); } } // We couldn't get a comment at all - either no available method or all methods failed pb_backupbuddy::status('details', __('Unable to get comment: No compatible zip method found.', 'it-l10n-backupbuddy')); return false; }
$temp_file = $_FILES['file']['tmp_name']; // target path $modules_target_dir = dirname(__FILE__) . $ds . $modules_dir . $ds; // module file $module_zip = $modules_target_dir . $_FILES['file']['name']; // get extension $ext = pathinfo($module_zip, PATHINFO_EXTENSION); if ($ext === 'zip') { // move upload file to modules/custom move_uploaded_file($temp_file, $module_zip); // create new zip archive instance $zip = new ZipArchive(); // open zip if ($zip->open($module_zip) === TRUE) { // get comment $comment = $zip->getArchiveComment(); // check if it's a semplice module if ($comment === 'semplicelabs') { // extract zip $zip->extractTo($modules_target_dir); } // close zip $zip->close(); } else { echo 'Module installation failed!'; } // delete zip unlink($module_zip); } else { echo "Please upload a valid module zip file!"; }
<?php $dirname = dirname(__FILE__) . '/'; $file = $dirname . 'test_with_comment.zip'; include $dirname . 'utils.inc'; $zip = new ZipArchive(); if (!$zip->open($file)) { exit('failed'); } echo $zip->getArchiveComment() . "\n"; $idx = $zip->locateName('foo'); echo $zip->getCommentName('foo') . "\n"; echo $zip->getCommentIndex($idx); echo $zip->getCommentName('') . "\n"; echo $zip->getCommentName() . "\n"; $zip->close();
if (!$zip->open($file, ZIPARCHIVE::CREATE)) { exit('failed'); } $zip->addFromString('entry1.txt', 'entry #1'); $zip->addFromString('entry2.txt', 'entry #2'); $zip->addFromString('dir/entry2d.txt', 'entry #2'); $zip->addFromString('entry4.txt', 'entry #1'); $zip->addFromString('entry5.txt', 'entry #2'); var_dump($zip->setCommentName('entry1.txt', 'entry1.txt')); var_dump($zip->setCommentName('entry2.txt', 'entry2.txt')); var_dump($zip->setCommentName('dir/entry2d.txt', 'dir/entry2d.txt')); var_dump($zip->setArchiveComment('archive')); var_dump($zip->setCommentIndex(3, 'entry4.txt')); var_dump($zip->setCommentIndex(4, 'entry5.txt')); var_dump($zip->setArchiveComment('archive')); if (!$zip->status == ZIPARCHIVE::ER_OK) { echo "failed to write zip\n"; } $zip->close(); if (!$zip->open($file)) { @unlink($file); exit('failed'); } var_dump($zip->getCommentIndex(0)); var_dump($zip->getCommentIndex(1)); var_dump($zip->getCommentIndex(2)); var_dump($zip->getCommentIndex(3)); var_dump($zip->getCommentIndex(4)); var_dump($zip->getArchiveComment()); $zip->close(); @unlink($file);
/** * @param string $buildZipPath * * @return boolean */ private function getBuildZipArchiveComment($buildZipPath) { if (!file_exists($buildZipPath)) { return false; } $buildZip = new \ZipArchive(); if (!$buildZip->open($buildZipPath)) { return false; } $buildZipComment = $buildZip->getArchiveComment(); $buildZip->close(); return $buildZipComment; }
/** * Motif install process. * * 1. Extract files to the appropriate directory. * 2. If this is a cabin-specific motif, update motifs.json. * Otherwise, it's a global Motif. Enable for all cabins. * 3. Create symbolic links. * 4. Clear cache files. * * @param InstallFile $fileInfo * @return bool */ public function install(InstallFile $fileInfo) : bool { $path = $fileInfo->getPath(); $zip = new \ZipArchive(); $res = $zip->open($path); if ($res !== true) { $this->log('Could not open the ZipArchive.', LogLevel::ERROR); return false; } // Extraction destination directory $dir = \implode(DIRECTORY_SEPARATOR, [ROOT, 'Motifs', $this->supplier->getName(), $this->package]); if (!\is_dir($dir)) { \mkdir($dir, 0775, true); } // Grab metadata $metadata = \Airship\parseJSON($zip->getArchiveComment(\ZipArchive::FL_UNCHANGED), true); if (isset($metadata['cabin'])) { $cabin = $this->expandCabinName($metadata['cabin']); if (!\is_dir(ROOT . '/Cabin/' . $cabin)) { $this->log('Could not install; cabin "' . $cabin . '" is not installed.', LogLevel::ERROR); return false; } } else { $cabin = null; } // Extract the new files to the current directory if (!$zip->extractTo($dir)) { $this->log('Could not extract Motif to its destination.', LogLevel::ERROR); return false; } // Add to the relevant motifs.json files if ($cabin) { $this->addMotifToCabin($cabin); } else { foreach (\glob(ROOT . '/Cabin/') as $cabin) { if (\is_dir($cabin)) { $this->addMotifToCabin(\Airship\path_to_filename($cabin)); } } } self::$continuumLogger->store(LogLevel::INFO, 'Motif install successful', $this->getLogContext($fileInfo)); // Finally, nuke the cache: return $this->clearCache(); }