public function testBzip2WithTarFile() { $tar = false; $includePath = explode(PATH_SEPARATOR, get_include_path()); foreach ($includePath as $path) { if (file_exists($path . DIRECTORY_SEPARATOR . 'Archive' . DIRECTORY_SEPARATOR . 'Tar.php')) { $tar = true; } } if ($tar && function_exists('bzopen')) { $a = new Archive(__DIR__ . '/../tmp/test.tar'); $a->addFiles(__DIR__ . '/../tmp'); $compressed = Bzip2::compress(__DIR__ . '/../tmp/test.tar'); copy($compressed, __DIR__ . '/../tmp/test.tbz2'); copy($compressed, __DIR__ . '/../tmp/test.tbz'); $this->fileExists(__DIR__ . '/../tmp/test.tar'); $this->fileExists(__DIR__ . '/../tmp/test.tar.bz2'); $this->fileExists(__DIR__ . '/../tmp/test.tbz2'); $this->fileExists(__DIR__ . '/../tmp/test.tbz'); if (file_exists(__DIR__ . '/../tmp/test.tar')) { unlink(__DIR__ . '/../tmp/test.tar'); } // Test *.tar.bz2 $decompressed = Bzip2::decompress(__DIR__ . '/../tmp/test.tar.bz2'); $this->fileExists(__DIR__ . '/../tmp/test.tar'); if (file_exists(__DIR__ . '/../tmp/test.tar')) { unlink(__DIR__ . '/../tmp/test.tar'); } if (file_exists(__DIR__ . '/../tmp/test.tar.bz2')) { unlink(__DIR__ . '/../tmp/test.tar.bz2'); } // Test *.tbz2 $decompressed = Bzip2::decompress(__DIR__ . '/../tmp/test.tbz2'); $this->fileExists(__DIR__ . '/../tmp/test.tar'); if (file_exists(__DIR__ . '/../tmp/test.tar')) { unlink(__DIR__ . '/../tmp/test.tar'); } if (file_exists(__DIR__ . '/../tmp/test.tbz2')) { unlink(__DIR__ . '/../tmp/test.tbz2'); } // Test *.tbz $decompressed = Bzip2::decompress(__DIR__ . '/../tmp/test.tbz'); $this->fileExists(__DIR__ . '/../tmp/test.tar'); if (file_exists(__DIR__ . '/../tmp/test.tar')) { unlink(__DIR__ . '/../tmp/test.tar'); } if (file_exists(__DIR__ . '/../tmp/test.tbz')) { unlink(__DIR__ . '/../tmp/test.tbz'); } } }
/** * Method to compress an archive file with Gzip or Bzip2 * * @param string $ext * @return \Pop\Archive\Archive */ public function compress($ext = 'gz') { if ($ext == 'bz') { $ext .= '2'; } switch ($ext) { case 'gz': $newArchive = Compress\Gzip::compress($this->fullpath); break; case 'tgz': $tmpArchive = Compress\Gzip::compress($this->fullpath); $newArchive = str_replace('.tar.gz', '.tgz', $tmpArchive); rename($tmpArchive, $newArchive); break; case 'bz2': $newArchive = Compress\Bzip2::compress($this->fullpath); break; case 'tbz': $tmpArchive = Compress\Bzip2::compress($this->fullpath); $newArchive = str_replace('.tar.bz2', '.tbz', $tmpArchive); rename($tmpArchive, $newArchive); break; case 'tbz2': $tmpArchive = Compress\Bzip2::compress($this->fullpath); $newArchive = str_replace('.tar.bz2', '.tbz2', $tmpArchive); rename($tmpArchive, $newArchive); break; default: $newArchive = $this->fullpath; } if (file_exists($this->fullpath)) { unlink($this->fullpath); } self::__construct($newArchive); return $this; }