createTempDir( "ezcArchive_" ); date_default_timezone_set("UTC"); } public function tearDown() { $this->removeTempDir(); } public function testRecognizePaxTar() { $archive = ezcArchive::getInstance( dirname( __FILE__ ) . "/data/tar_pax_2_textfiles.tar" ); $this->assertNotNull( $archive ); $this->assertEquals( ezcArchive::TAR_PAX, $archive->getAlgorithm() ); $this->assertFalse( $archive->algorithmCanWrite() ); } public function testRecognizeGnuTar() { $archive = ezcArchive::getInstance( dirname( __FILE__ ) . "/data/tar_gnu_2_textfiles.tar" ); $this->assertNotNull( $archive ); $this->assertEquals( ezcArchive::TAR_GNU, $archive->getAlgorithm() ); $this->assertFalse( $archive->algorithmCanWrite() ); } public function testRecognizeUstar() { $archive = ezcArchive::getInstance( dirname( __FILE__ ) . "/data/tar_ustar_2_textfiles.tar" ); $this->assertNotNull( $archive ); $this->assertEquals( ezcArchive::TAR_USTAR, $archive->getAlgorithm() ); $this->assertTrue( $archive->algorithmCanWrite() ); } public function testRecognizeV7Tar() { $archive = ezcArchive::getInstance( dirname( __FILE__ ) . "/data/tar_v7_2_textfiles.tar" ); $this->assertNotNull( $archive ); $this->assertEquals( ezcArchive::TAR_V7, $archive->getAlgorithm() ); $this->assertTrue( $archive->algorithmCanWrite() ); } public function testRecognizeZip() { $archive = ezcArchive::getInstance( dirname( __FILE__ ) . "/data/infozip_2_textfiles.zip" ); $this->assertNotNull( $archive ); $this->assertEquals( ezcArchive::ZIP, $archive->getAlgorithm() ); $this->assertTrue( $archive->algorithmCanWrite() ); } public function testExtractAll() { // Just choose one type. The specific algorithms are already tested. $dir = $this->getTempDir(); $archive = ezcArchive::getInstance( dirname( __FILE__ ) . "/data/tar_pax_2_textfiles.tar" ); $archive->extract( $dir ); clearstatcache(); $this->assertTrue( file_exists( "$dir/file1.txt" ) ); $this->assertTrue( file_exists( "$dir/file2.txt" ) ); } // Extracting works fine. But adding files breaks. public function testReadGzippedTarAuto() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/tar_ustar_2_textfiles.tar", "$dir/mytar.tar"); exec( "gzip $dir/mytar.tar" ); $archive = ezcArchive::getInstance( "$dir/mytar.tar.gz" ); $archive->extract( $dir ); clearstatcache(); $this->assertTrue( file_exists( "$dir/file1.txt" ) ); $this->assertTrue( file_exists( "$dir/file2.txt" ) ); } // Extracting works fine. But adding files breaks. public function testReadGzippedTar() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/tar_ustar_2_textfiles.tar", "$dir/mytar.tar"); exec( "gzip $dir/mytar.tar" ); $archive = ezcArchive::getInstance( "compress.zlib://$dir/mytar.tar.gz" ); $archive->extract( $dir ); $archive->rewind(); $archive->next(); try { file_put_contents( "$dir/file3.txt", "Hahaha"); $archive->appendToCurrent( "$dir/file3.txt", $dir); $this->fail( "Read only exception expected"); } catch (ezcBaseFilePermissionException $e ) { // Expect read-only exception. } clearstatcache(); $this->assertTrue( file_exists( "$dir/file1.txt" ) ); $this->assertTrue( file_exists( "$dir/file2.txt" ) ); } // Extracting works fine. But adding files breaks. public function testReadBzippedTar() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/tar_ustar_2_textfiles.tar", "$dir/mytar.tar"); exec( "bzip2 $dir/mytar.tar" ); $archive = ezcArchive::getInstance( "compress.bzip2://$dir/mytar.tar.bz2" ); //echo ( $archive ); $archive->extract( $dir ); $archive->rewind(); $archive->next(); try { file_put_contents( "$dir/file3.txt", "Hahaha"); $archive->appendToCurrent( "$dir/file3.txt", $dir); $this->fail( "Read only exception expected"); } catch (ezcBaseFilePermissionException $e ) { // Expect read-only exception. } clearstatcache(); $this->assertTrue( file_exists( "$dir/file1.txt" ) ); $this->assertTrue( file_exists( "$dir/file2.txt" ) ); } public function testReadBzippedTarAuto() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/tar_ustar_2_textfiles.tar", "$dir/mytar.tar"); exec( "bzip2 $dir/mytar.tar" ); $archive = ezcArchive::getInstance( "$dir/mytar.tar.bz2" ); $archive->extract( $dir ); $archive->rewind(); $archive->next(); try { file_put_contents( "$dir/file3.txt", "Hahaha"); $archive->appendToCurrent( "$dir/file3.txt", $dir); $this->fail( "Read only exception expected"); } catch (ezcBaseFilePermissionException $e ) { // Expect read-only exception. } clearstatcache(); $this->assertTrue( file_exists( "$dir/file1.txt" ) ); $this->assertTrue( file_exists( "$dir/file2.txt" ) ); } public function readBzippedGzippedTar() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/tar_ustar_2_textfiles.tar", "$dir/mytar.tar"); exec( "gzip $dir/mytar.tar" ); exec( "bzip2 $dir/mytar.tar.gz" ); $archive = ezcArchive::getInstance( "$dir/mytar.tar.gz.bz2" ); $archive->extract( $dir ); clearstatcache(); $this->assertTrue( file_exists( "$dir/file1.txt" ) ); $this->assertTrue( file_exists( "$dir/file2.txt" ) ); } public function testTarIncorrectBlockSizeException() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/infozip_2_textfiles.zip", "$dir/mytar.tar"); try { $archive = ezcArchive::getInstance( "$dir/mytar.tar", ezcArchive::TAR_V7 ); $entry = $archive->current(); $this->fail("This is not an Tar, so throw an exception"); } catch ( ezcArchiveBlockSizeException $e ) { } } public function testWriteBzippedTar() { $dir = $this->getTempDir(); try { $archive = ezcArchive::getInstance( "compress.bzip2://$dir/mytar.tar.bz2" ); file_put_contents( "$dir/file3.txt", "Hahaha"); $archive->appendToCurrent( "$dir/file3.txt", $dir); $this->fail( "Read only exception expected"); } catch (ezcArchiveUnknownTypeException $e ) { } } public function testWriteBzippedTarAuto() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/tar_ustar_2_textfiles.tar", "$dir/mytar.tar"); exec( "bzip2 $dir/mytar.tar" ); $archive = ezcArchive::getInstance( "$dir/mytar.tar.bz2" ); try { file_put_contents( "$dir/file3.txt", "Hahaha"); $archive->appendToCurrent( "$dir/file3.txt", $dir); $this->fail( "Read only exception expected"); } catch (ezcBaseFilePermissionException $e ) { // Expect read-only exception. } } /* public function testGzippedGzippedTar() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/tar_pax_2_textfiles.tar", "$dir/mytar.tar"); exec( "gzip $dir/mytar.tar" ); rename( "$dir/mytar.tar.gz", "$dir/mytar.tar.a"); exec( "gzip $dir/mytar.tar.a" ); $archive = ezcArchive::getInstance( "$dir/mytar.tar.a.gz" ); $archive->extract( $dir ); clearstatcache(); $this->assertTrue( file_exists( "$dir/file1.txt" ) ); $this->assertTrue( file_exists( "$dir/file2.txt" ) ); } */ public function testForceUstarTar() { $dir = $this->getTempDir(); // Filesize is smaller than the blocksize. copy( dirname( __FILE__ ) . "/data/infozip_2_textfiles.zip", "$dir/myzip.zip"); try { // File size too small. $archive = ezcArchive::getInstance( "$dir/myzip.zip", ezcArchive::TAR_V7 ); $archive->extract( $dir ); $this->fail( "Exception expected since we cannot extract a Zip archive with the Tar handler. "); } catch (ezcArchiveException $e) { // Okay. } } public function testForceUstarTarPart2() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/infozip_file_dir_symlink_link.zip", "$dir/myzip.zip"); try { // CRC is incorrect. $archive = ezcArchive::getInstance( "$dir/myzip.zip", ezcArchive::TAR_V7 ); $archive->extract( $dir ); $this->fail( "Exception expected since we cannot extract a Zip archive with the Tar handler. "); } catch (ezcArchiveException $e) { // Okay. } } public function testCreateNewArchive() { $dir = $this->getTempDir(); $archive = ezcArchive::getInstance( "$dir/myzip.zip", ezcArchive::ZIP ); file_put_contents( "$dir/bla.txt", "Hello world"); file_put_contents( "$dir/bla2.txt", "Hello world2"); $archive->append("$dir/bla.txt", "$dir"); $archive->append("$dir/bla2.txt", "$dir"); $archive->rewind(); $this->assertEquals( "bla.txt", $archive->current()->getPath() ); $this->assertEquals( "bla2.txt", $archive->next()->getPath() ); } public function testCreateNewArchiveWithoutType() { $dir = $this->getTempDir(); try { $archive = ezcArchive::getInstance( "$dir/myzip.zip" ); $this->fail( "Exception expected, because the type is missing"); } catch ( ezcArchiveException $e ) { } } /* Doesn't work, because we cannot read and write to an gzipped file at the same time. public function testCreateNewGzippedTar() { $dir = $this->getTempDir(); $archive = ezcArchive::getInstance( "compress.zlib://$dir/my.tar.gz", ezcArchive::TAR ); file_put_contents( "$dir/bla.txt", "Hello world"); file_put_contents( "$dir/bla2.txt", "Hello world2"); $archive->append("$dir/bla.txt", "$dir"); $archive->append("$dir/bla2.txt", "$dir"); // First check: okay in archive. $archive->rewind(); $this->assertEquals( "bla.txt", $archive->current()->getPath() ); $this->assertEquals( "bla2.txt", $archive->next()->getPath() ); // Second check: Reread the archive.. and read it. $archive = ezcArchive::getInstance( "$dir/my.tar.gz" ); $this->assertEquals( "bla.txt", $archive->current()->getPath() ); $this->assertEquals( "bla2.txt", $archive->next()->getPath() ); } */ public function testWinzipExtract() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/winzip_1_textfile.zip", "$dir/myzip.zip"); $archive = ezcArchive::getInstance( "$dir/myzip.zip" ); $archive->extract( $dir ); $this->assertEquals( "Hello world 2!!", file_get_contents( "$dir/ray2.txt" ) ); } public function testWinzipAppend() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/winzip_1_textfile.zip", "$dir/myzip.zip"); $archive = ezcArchive::getInstance( "$dir/myzip.zip" ); file_put_contents( "$dir/myfile.txt", "Hi"); $archive->append( "$dir/myfile.txt", $dir ); $archive->rewind(); $this->assertEquals( "ray2.txt", $archive->current()->getPath() ); $this->assertEquals( "myfile.txt", $archive->next()->getPath() ); unset( $archive ); $archive = ezcArchive::getInstance( "$dir/myzip.zip" ); $this->assertEquals( "ray2.txt", $archive->current()->getPath() ); $this->assertEquals( "myfile.txt", $archive->next()->getPath() ); } public function testListing() { $archive = ezcArchive::getInstance( dirname( __FILE__ ) . "/data/tar_pax_2_textfiles.tar" ); $list = $archive->getListing(); $this->assertEquals( "file1.txt", substr( $list[0], -9 ) ); $this->assertEquals( "file2.txt", substr( $list[1], -9 ) ); } public function testAppendWithWrongPrefix() { $dir = $this->getTempDir(); copy( dirname( __FILE__ ) . "/data/tar_ustar_2_textfiles.tar", "$dir/mytar.tar"); $archive = ezcArchive::getInstance( "$dir/mytar.tar" ); file_put_contents( "$dir/haha.txt", "Hahahah"); try { $archive->append( "$dir/haha.txt", "aap"); } catch ( ezcArchiveEntryPrefixException $e ) { //$this->assertEquals( ezcArchiveException::INVALID_PREFIX, $e->getCode() ); } } public static function suite() { return new ezcTestSuite( "ezcArchiveTest"); } } ?>