/** * Test creating and packing files/dirs into a ZipArchive * * @dataProvider providerPackArchive * @param $source * @param $archive * @param $options * @param $packOptions * @param $expected */ public function testPackArchive($source, $archive, $options, $packOptions, $expected) { if (!empty(xPDOTestHarness::$debug)) { print "\n" . __METHOD__ . " = "; } $result = false; $sourcePath = xPDOTestHarness::$properties['xpdo_test_path'] . "fs/zip/{$source}"; $archivePath = xPDOTestHarness::$properties['xpdo_test_path'] . "fs/{$archive}"; try { $this->xpdo->loadClass('compression.xPDOZip', XPDO_CORE_PATH, true, true); $zip = new xPDOZip($this->xpdo, $archivePath, $options); $result = $zip->pack($sourcePath, $packOptions); while (list($idx, $entry) = each($result)) { $result[$idx] = str_replace($sourcePath, $source, $entry); } $this->xpdo->log(xPDO::LOG_LEVEL_INFO, "Pack results for {$archive}: " . print_r($result, true)); } catch (Exception $e) { $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, $e->getMessage(), '', __METHOD__, __FILE__, __LINE__); } $this->assertEquals($expected, $result, "Error packing xPDOZip archive {$archive} from {$source}"); // $this->assertTrue(file_exists($archivePath), "Error creating xPDOZip archive at {$archive}"); }
/** * Pack the resources from path relative to source into an archive with filename. * * @uses compression.xPDOZip OR compression.PclZip * @todo Refactor this to be implemented in a service class external to xPDOTransport. * * @param xPDO &$xpdo A reference to an xPDO instance. * @param string $filename A valid zip archive filename. * @param string $path An absolute file system path location of the resources to pack. * @param string $source A relative portion of path to include in the archive. * @return boolean True if packed successfully. */ public static function _pack(&$xpdo, $filename, $path, $source) { $packed = false; $packResults = false; $errors = array(); if ($xpdo->getOption(xPDOTransport::ARCHIVE_WITH, null, 0) != xPDOTransport::ARCHIVE_WITH_PCLZIP && class_exists('ZipArchive', true) && $xpdo->loadClass('compression.xPDOZip', XPDO_CORE_PATH, true, true)) { if ($xpdo->getDebug() === true) { $xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Using xPDOZip / native ZipArchive", null, __METHOD__, __FILE__, __LINE__); } $archive = new xPDOZip($xpdo, $filename, array(xPDOZip::CREATE => true, xPDOZip::OVERWRITE => true)); if ($archive) { $packResults = $archive->pack("{$path}{$source}", array(xPDOZip::ZIP_TARGET => "{$source}/")); $archive->close(); if (!$archive->hasError() && !empty($packResults)) { $packed = true; } else { $errors = $archive->getErrors(); } } } elseif (class_exists('PclZip') || (include XPDO_CORE_PATH . 'compression/pclzip.lib.php')) { $archive = new PclZip($filename); if ($archive) { $packResults = $archive->create("{$path}{$source}", PCLZIP_OPT_REMOVE_PATH, "{$path}"); if ($packResults) { $packed = true; } else { $errors = $archive->errorInfo($xpdo->getDebug() === true); } } } if (!$packed) { $xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error packing {$path}{$source} to {$filename}: " . print_r($errors, true)); } if ($xpdo->getDebug() === true) { $xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Results of packing {$path}{$source} to {$filename}: " . print_r($packResults, true), null, __METHOD__, __FILE__, __LINE__); } return $packed; }