eZ components PHP Cross Reference Developer Tools

Source: /Archive/tests/zip/zip_test.php - 810 lines - 28333 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
   4   * @license http://ez.no/licenses/new_bsd New BSD License
   5   * @filesource
   6   * @package Archive
   7   * @version 1.4.1
   8   * @subpackage Tests
   9   */
  10  
  11  require_once( dirname( __FILE__ ) . "/../testdata.php" );
  12  require_once(dirname(__FILE__) . "/../archive_test_case.php");
  13  
  14  /**
  15   * @package Archive
  16   * @version 1.4.1
  17   * @subpackage Tests
  18   */
  19  class ezcArchiveZipTest extends ezcArchiveTestCase
  20  {
  21      protected $td;
  22      protected $tempDir;
  23  
  24      protected function setUp()
  25      {
  26          date_default_timezone_set( "UTC" );
  27  
  28          $this->tempDir = $this->createTempDir( "ezcArchive_" );
  29          $dataDir = dirname( __FILE__ ) . "/../data/";
  30  
  31          $this->td = new ezcArchiveTestData( $dataDir, $this->tempDir, "zip", "infozip" );
  32      }
  33  
  34      protected function tearDown()
  35      {
  36          $this->removeTempDir();
  37      }
  38  
  39      protected function isWindows()
  40      {
  41          return ( substr( php_uname( 's' ), 0, 7 ) == 'Windows' ) ? true : false;
  42      }
  43  
  44      public function testOpenArchive()
  45      {
  46          $archive = $this->td->getArchive( "2_textfiles" );
  47          $entry = $archive->current();
  48          $this->assertEquals( "file1.txt", $entry->getPath() );
  49      }
  50  
  51      public function testOttExtract()
  52      {
  53          $original = dirname(__FILE__) . "/../data/ezpublish.ott";
  54          $odtFile = $this->tempDir . "/ezpublish.ott";
  55          copy( $original, $odtFile );
  56          $target = $this->tempDir . "/unzipped/";
  57          mkdir( $target );
  58  
  59          $archive = ezcArchive::open( $odtFile );
  60          $archive->extract( $target );
  61  
  62          $this->assertTrue( file_exists( $target . "content.xml" ) );
  63          $this->assertEquals( 9695, filesize( $target . "content.xml" ) );
  64          $this->assertTrue( file_exists( $target . "Configurations2" ) );
  65          $this->assertTrue( file_exists( $target . "Configurations2/floater" ) );
  66          $stat = stat( $target . "Configurations2/accelerator" );
  67          $this->assertEquals( 0777 & ~umask(), $stat['mode'] & 0777 );
  68          $stat = stat( $target . "Configurations2/floater" );
  69          $this->assertEquals( 0777 & ~umask(), $stat['mode'] & 0777 );
  70          $this->assertEquals( 17723, filesize( $target . "styles.xml" ) );
  71          $this->assertEquals( 1693, filesize( $target . "Pictures/100000000000012C0000003CBE7676D8.gif" ) );
  72  
  73          $this->assertFalse( file_exists( $target . "Configurations2/file_does_not_exist" ) );
  74      }
  75  
  76      public function testOttExtract2()
  77      {
  78          $original = dirname(__FILE__) . "/../data/ezpublish2.zip";
  79          $odtFile = $this->tempDir . "/ezpublish.ott";
  80          copy( $original, $odtFile );
  81          $target = $this->tempDir . "/unzipped/";
  82          mkdir( $target );
  83  
  84          $archive = ezcArchive::open( $odtFile );
  85          $archive->extract( $target );
  86  
  87          $this->assertTrue( file_exists( $target . "content.xml" ) );
  88          $this->assertEquals( 9695, filesize( $target . "content.xml" ) );
  89          $this->assertTrue( file_exists( $target . "Configurations2" ) );
  90          $this->assertTrue( file_exists( $target . "Configurations2/floater" ) );
  91          $stat = stat( $target . "Configurations2/accelerator" );
  92          $this->assertEquals( 0777 & ~umask(), $stat['mode'] & 0777 );
  93          $stat = stat( $target . "Configurations2/floater" );
  94          $this->assertEquals( 0777 & ~umask(), $stat['mode'] & 0777 );
  95          $this->assertEquals( 17723, filesize( $target . "styles.xml" ) );
  96          $this->assertEquals( 1693, filesize( $target . "Pictures/100000000000012C0000003CBE7676D8.gif" ) );
  97  
  98          $this->assertFalse( file_exists( $target . "Configurations2/file_does_not_exist" ) );
  99      }
 100  
 101      public function testOdtExtract()
 102      {
 103          $original = dirname(__FILE__) . "/../data/files_and_dirs.odt";
 104          $odtFile = $this->tempDir . "/files_and_dirs.odt";
 105          copy( $original, $odtFile );
 106          $target = $this->tempDir . "/unzipped/";
 107          mkdir( $target );
 108  
 109          $archive = ezcArchive::open( $odtFile );
 110          $archive->extract($target);
 111  
 112          $this->assertTrue( file_exists( $target . "content.xml" ) );
 113          $this->assertEquals( 2553, filesize( $target . "content.xml" ) );
 114          $this->assertTrue( file_exists( $target . "Configurations2" ) );
 115          $this->assertTrue( file_exists( $target . "Configurations2/floater" ) );
 116  
 117          $this->assertFalse( file_exists( $target . "Configurations2/file_does_not_exist" ) );
 118      }
 119  
 120      public function testOdtSeekAndExtract()
 121      {
 122          $original = dirname(__FILE__) . "/../data/files_and_dirs.odt";
 123          $odtFile = $this->tempDir . "/files_and_dirs.odt";
 124          copy( $original, $odtFile );
 125          $target = $this->tempDir . "/unzipped/";
 126          mkdir( $target );
 127  
 128          $archive = ezcArchive::open( $odtFile );
 129  
 130          $archive->seek( 4 ); // 5th file.
 131          $archive->extractCurrent( $target );
 132          $this->assertTrue( file_exists( $target . "Configurations2/popupmenu" ) );
 133      }
 134  
 135      public function testOdtAppend()
 136      {
 137          $original = dirname(__FILE__) . "/../data/files_and_dirs.odt";
 138          $odtFile = $this->tempDir . "/files_and_dirs.odt";
 139          copy( $original, $odtFile );
 140          $target = $this->tempDir . "/unzipped/";
 141          mkdir( $target );
 142  
 143          $archive = ezcArchive::open( $odtFile );
 144  
 145          $archive->seek( 4 ); // 5th file.
 146          file_put_contents( $this->tempDir . "myfile.txt", "Hello world" );
 147          $archive->appendToCurrent( $this->tempDir . "myfile.txt", $this->tempDir );
 148  
 149          $archive->extract( $target );
 150  
 151          $this->assertTrue( file_exists( $target . "Configurations2/popupmenu" ) );
 152          $this->assertTrue( file_exists( $target . "myfile.txt" ) );
 153      }
 154  
 155      public function testOdtMac()
 156      {
 157          $original = dirname(__FILE__) . "/../data/mac_odt.odt";
 158          $odtFile = $this->tempDir . "/mac_odt.odt";
 159          copy( $original, $odtFile );
 160          $target = $this->tempDir . "/unzipped/";
 161          mkdir( $target );
 162  
 163          $archive = ezcArchive::open( $odtFile );
 164          $archive->extract( $target );
 165  
 166          $this->assertTrue( file_exists( $target . "content.xml" ) );
 167          $this->assertTrue( file_exists( $target . "Configurations2" ) );
 168          $this->assertTrue( file_exists( $target . "Configurations2/floater" ) );
 169  
 170          $this->assertFalse( file_exists( $target . "Configurations2/file_does_not_exist" ) );
 171      }
 172  
 173      public function testOdtMacImg()
 174      {
 175          $original = dirname(__FILE__) . "/../data/mac_odt_with_img.odt";
 176          $odtFile = $this->tempDir . "/mac_odt_with_img.odt";
 177          copy( $original, $odtFile );
 178          $target = $this->tempDir . "/unzipped/";
 179          mkdir( $target );
 180  
 181          $archive = ezcArchive::open( $odtFile );
 182          $archive->extract( $target );
 183  
 184          $this->assertTrue( file_exists( $target . "content.xml" ) );
 185          $this->assertTrue( file_exists( $target . "Configurations2" ) );
 186          $this->assertTrue( file_exists( $target . "Configurations2/floater" ) );
 187          $this->assertTrue( file_exists( $target . "Pictures/1000000000000168000000F05D0D62C6.png" ) );
 188  
 189          $this->assertFalse( file_exists( $target . "Configurations2/file_does_not_exist" ) );
 190      }
 191  
 192      public function testOdtWin()
 193      {
 194          $original = dirname(__FILE__) . "/../data/win_odt.odt";
 195          $odtFile = $this->tempDir . "/win_odt.odt";
 196          copy( $original, $odtFile );
 197          $target = $this->tempDir . "/unzipped/";
 198          mkdir( $target );
 199  
 200          $archive = ezcArchive::open( $odtFile );
 201          $archive->extract( $target );
 202  
 203          $this->assertTrue( file_exists( $target . "content.xml" ) );
 204          $this->assertTrue( file_exists( $target . "Configurations2" ) );
 205          $this->assertTrue( file_exists( $target . "Configurations2/floater" ) );
 206  
 207          $this->assertFalse( file_exists( $target . "Configurations2/file_does_not_exist" ) );
 208      }
 209  
 210      public function testOdtWinImg()
 211      {
 212          $original = dirname(__FILE__) . "/../data/win_odt_with_img.odt";
 213          $odtFile = $this->tempDir . "/win_odt_with_img.odt";
 214          copy( $original, $odtFile );
 215          $target = $this->tempDir . "/unzipped/";
 216          mkdir( $target );
 217  
 218          $archive = ezcArchive::open( $odtFile );
 219          $archive->extract( $target );
 220  
 221          $this->assertTrue( file_exists( $target . "content.xml" ) );
 222          $this->assertTrue( file_exists( $target . "Configurations2" ) );
 223          $this->assertTrue( file_exists( $target . "Configurations2/floater" ) );
 224          $this->assertTrue( file_exists( $target . "Pictures/10000000000001F4000001474F0889E4.jpg" ) );
 225  
 226          $this->assertFalse( file_exists( $target . "Configurations2/file_does_not_exist" ) );
 227      }
 228  
 229  
 230      public function testComments()
 231      {
 232          $original = dirname(__FILE__) . "/../data/infozip_comment.zip";
 233          $file = $this->tempDir . "/infozip_comment.zip";
 234          copy( $original, $file );
 235          $target = $this->tempDir . "/unzipped/";
 236          mkdir( $target );
 237  
 238          $archive = ezcArchive::open( $file );
 239          $archive->extract( $target );
 240  
 241          $this->assertTrue( file_exists( $target . "meta.xml" ) );
 242          $this->assertTrue( file_exists( $target . "mimetype" ) );
 243      }
 244  
 245      public function testEmptyArchive()
 246      {
 247          $archive = $this->td->getNewArchive( "does_not_exist" );
 248  
 249          $this->assertFalse( $archive->current(), "Archive should be empty, so no file info available" );
 250          $this->assertFalse( $archive->valid(), "Archive should be empty, so no  file info available" );
 251          $this->assertFalse( $archive->next(), "Archive should be empty, so no file info available" );
 252          $this->assertFalse( $archive->next(), "Archive should be empty, so no file info available" );
 253  
 254          if ( $this->isWindows() )
 255          {
 256              return; // avoid warning
 257          }
 258  
 259          unlink( 'does_not_exist' );
 260      }
 261  
 262  
 263      public function testIteratorOperations()
 264      {
 265          $archive  = $this->td->getArchive( "2_textfiles" );
 266  
 267          $entry = $archive->current();
 268          $entry = $archive->current();
 269          $this->assertEquals( "file1.txt", $entry->getPath() );
 270          $this->assertTrue( $archive->valid(), "Expected a valid archive position." );
 271  
 272          $archive->rewind();
 273          $entry = $archive->current();
 274          $this->assertEquals( "file1.txt", $entry->getPath() );
 275          $this->assertEquals( 0, $archive->key() );
 276          $this->assertTrue( $archive->valid(), "Expected a valid archive position." );
 277  
 278  
 279          $this->assertTrue( $archive->next() !== false );
 280          $entry = $archive->current();
 281          $this->assertEquals( "file2.txt", $entry->getPath() );
 282          $this->assertEquals( 1, $archive->key() );
 283          $this->assertTrue( $archive->valid(), "Expected a valid archive position." );
 284  
 285          $this->assertFalse( $archive->next() );
 286          $this->assertFalse( $archive->current() );
 287          $this->assertFalse( $archive->valid() );
 288          $this->assertFalse( $archive->key() );
 289  
 290          $this->assertFalse( $archive->next() );
 291          $this->assertFalse( $archive->next() );
 292      }
 293  
 294      public function testForEaching()
 295      {
 296          $archive = $this->td->getArchive( "2_textfiles" );
 297  
 298          for ( $i = 0; $i < 2; $i++ )
 299          {
 300              $loopNumber = 0;
 301              foreach ( $archive as $entryNumber => $entry )
 302              {
 303                  if ( $loopNumber == 0 )
 304                  {
 305                      $this->assertEquals( "file1.txt", $entry->getPath() );
 306                      $this->assertEquals( 0, $entryNumber );
 307                  }
 308                  else if ( $loopNumber == 1 )
 309                  {
 310                      $this->assertEquals( "file2.txt", $entry->getPath() );
 311                      $this->assertEquals( 1, $entryNumber );
 312                  }
 313                  else
 314                  {
 315                      $this->fail( "Didn't expect another entry in the archive" );
 316                  }
 317  
 318                  $loopNumber++;
 319              }
 320          }
 321      }
 322  
 323      public function testExtractCurrent()
 324      {
 325          $targetDir = $this->getTempDir() . "/";
 326  
 327          $archive = $this->td->getArchive( "2_textfiles" );
 328          $archive->extractCurrent( $targetDir );
 329  
 330          $file1 = $this->getTempDir() . "/file1.txt";
 331          $this->assertEquals( "Hello world.\nThe first file.\n", file_get_contents( $file1 ) );
 332  
 333          // Remove the file, and extract again.
 334          unlink( $file1 );
 335  
 336          // Check whether the file is really removed (paranoia?).
 337          $fp = @fopen( $file1, "r" );
 338          if ( $fp )
 339          {
 340              $this->assertFail( "No noo nooo. The file shouldn't be here." );
 341          }
 342  
 343          $archive->extractCurrent( $targetDir );
 344          $this->assertEquals( "Hello world.\nThe first file.\n", file_get_contents( $file1 ) );
 345  
 346          // Move on.
 347          $entry = $archive->next();
 348  
 349          $this->assertTrue( $archive->valid(), "Second file is expected here." );
 350  
 351          $archive->extractCurrent( $targetDir );
 352          $file2 = $this->getTempDir() . "/file2.txt";
 353          $this->assertEquals( "Hello world.\nThe second file.\n", file_get_contents( $file2 ) );
 354  
 355          $this->assertFalse( $archive->next(), "No more files in the archive" );
 356      }
 357  
 358      public function testExtractCurrentOverwriteFile()
 359      {
 360          $archive = $this->td->getArchive( "2_textfiles" );
 361  
 362          // Normally it will overwrite the file, if possible.
 363          $dir = $this->getTempDir() . "/";
 364  
 365          $archive->extractCurrent( $dir );
 366          $this->assertEquals( "Hello world.\nThe first file.\n", file_get_contents( "$dir/file1.txt" ) );
 367  
 368          $fp = @fopen( "$dir/file1.txt", "w" );
 369          fwrite( $fp, "Garbage" );
 370          fclose( $fp );
 371  
 372          $this->assertEquals( "Garbage", file_get_contents( "$dir/file1.txt" ) );
 373  
 374          $archive->extractCurrent( $dir );
 375          $this->assertEquals( "Hello world.\nThe first file.\n", file_get_contents( "$dir/file1.txt" ) );
 376      }
 377  
 378      public function testExtractCurrentKeepExistingFile()
 379      {
 380          $archive = $this->td->getArchive( "2_textfiles" );
 381  
 382          // Normally it will overwrite the file, if possible.
 383          $targetDir = $this->getTempDir() . "/";
 384          $archive->extractCurrent( $targetDir, true );
 385  
 386          $file1 = $this->getTempDir() . "/file1.txt";
 387          $this->assertEquals( "Hello world.\nThe first file.\n", file_get_contents( $file1 ) );
 388  
 389          $fp = @fopen( $file1, "w" );
 390          fwrite( $fp, "Garbage" );
 391          fclose( $fp );
 392  
 393          $this->assertEquals( "Garbage", file_get_contents( $file1 ) );
 394  
 395          $archive->extractCurrent( $targetDir, true );
 396          $this->assertEquals( "Garbage", file_get_contents( $file1 ) );
 397      }
 398  
 399      public function testSymlink()
 400      {
 401          if ( $this->isWindows() )
 402          {
 403              return; // symlinks extracted as files in Windows, so there is no sence to call is_link()
 404          }
 405  
 406          $dir = $this->getTempDir();
 407          $archive = $this->td->getArchive( "file_symlink" );
 408  
 409          foreach ( $archive as $entry )
 410          {
 411              $archive->extractCurrent( $dir );
 412          }
 413  
 414          $this->assertTrue( is_link( "$dir/mysym.txt" ) );
 415          $this->assertEquals( "file1.txt", readlink( "$dir/mysym.txt" ) );
 416      }
 417  
 418      public function testSeekPositions()
 419      {
 420          $archive = $this->td->getArchive( "file_dir_symlink_link" );
 421          $entry = $archive->current();
 422          $this->assertEquals( "files/", $entry->getPath() );
 423  
 424          $archive->seek( 2 ); // third file in archive.
 425          $entry = $archive->current();
 426          $this->assertEquals( "files/bla/bin/", $entry->getPath() );
 427  
 428          $archive->seek( 0 ); // first file in archive.
 429          $entry = $archive->current();
 430          $this->assertEquals( "files/", $entry->getPath() );
 431  
 432          $archive->seek( 6 );
 433          $entry = $archive->current();
 434          $this->assertEquals( "files/file2.txt", $entry->getPath() );
 435  
 436          $archive->seek( 8 );
 437          $entry = $archive->current();
 438          $this->assertEquals( "files/file4.txt", $entry->getPath() );
 439  
 440          $archive->seek( 9 );
 441          $this->assertFalse( $archive->current() );
 442  
 443          $archive->seek( 0 );
 444          $entry = $archive->current();
 445          $this->assertEquals( "files/", $entry->getPath() );
 446  
 447          $archive->seek( -1 );
 448          $this->assertFalse( $archive->current() );
 449      }
 450  
 451      public function testSeekEndOfFile()
 452      {
 453          $archive = $this->td->getArchive( "file_dir_symlink_link" );
 454          $archive->seek( 0, SEEK_END ); // nineth and last file.
 455          $entry = $archive->current();
 456          $this->assertEquals( "files/file4.txt", $entry->getPath() );
 457  
 458          $archive->seek( -2, SEEK_END ); // seventh file
 459          $entry = $archive->current();
 460          $this->assertEquals( "files/file2.txt", $entry->getPath() );
 461  
 462          $archive->seek( -8, SEEK_END ); // first file
 463          $entry = $archive->current();
 464          $this->assertEquals( "files/", $entry->getPath() );
 465  
 466          $archive->seek( 1, SEEK_END );  // invalid
 467          $this->assertFalse( $archive->current() );
 468  
 469          $archive->seek( 0, SEEK_END ); // nineth and last file.
 470          $entry = $archive->current();
 471          $this->assertEquals( "files/file4.txt", $entry->getPath() );
 472  
 473          $archive->seek( -9, SEEK_END );  // invalid
 474          $this->assertFalse( $archive->current() );
 475      }
 476  
 477      public function testSeekCur()
 478      {
 479          $archive = $this->td->getArchive( "file_dir_symlink_link" );
 480          $entry = $archive->current();
 481          $this->assertEquals( "files/", $entry->getPath() );
 482  
 483          $archive->seek( 2, SEEK_CUR ); // third file in archive.
 484          $entry = $archive->current();
 485          $this->assertEquals( "files/bla/bin/", $entry->getPath() );
 486  
 487          $archive->seek( 0, SEEK_CUR ); // Third file in archive.
 488          $entry = $archive->current();
 489          $this->assertEquals( "files/bla/bin/", $entry->getPath() );
 490  
 491          $archive->seek( 4, SEEK_CUR ); // Seventh file in the archive.
 492          $entry = $archive->current();
 493          $this->assertEquals( "files/file2.txt", $entry->getPath() );
 494  
 495          $archive->seek( 2, SEEK_CUR );  // nineth and last.
 496          $entry = $archive->current();
 497          $this->assertEquals( "files/file4.txt", $entry->getPath() );
 498  
 499          $archive->seek( 1, SEEK_CUR );
 500          $this->assertFalse( $archive->current() );
 501  
 502          $archive->seek( 2 );
 503          $entry = $archive->current();
 504          $this->assertEquals( "files/bla/bin/", $entry->getPath() );
 505  
 506          $archive->seek( -2, SEEK_CUR ); // First file.
 507          $entry = $archive->current();
 508          $this->assertEquals( "files/", $entry->getPath() );
 509  
 510          $archive->seek( -1, SEEK_CUR );  // And invalid again.
 511          $this->assertFalse( $archive->current() );
 512      }
 513  
 514      public function testExtractOneDirectory()
 515      {
 516          // The subdirectory should be created automatically.
 517          $archive = $this->td->getArchive( "file_dir_symlink_link" );
 518          $archive->seek( 1 );
 519          $targetDir = $this->getTempDir();
 520          $archive->extractCurrent( $targetDir );
 521  
 522          $this->assertTrue( file_exists( $targetDir . "/files/bla/" ), "Cannot find the extracted directory." );
 523      }
 524  
 525      public function testExtractOneFile()
 526      {
 527          // The directory should be created automatically.
 528          $archive = $this->td->getArchive( "file_dir_symlink_link" );
 529          $archive->seek( 4 ); // 5th file.
 530          $targetDir = $this->getTempDir();
 531          $archive->extractCurrent( $targetDir );
 532  
 533          $this->assertTrue( file_exists( $targetDir . "/files/bla/file3.txt"), "Cannot find the extracted file." );
 534          $this->assertEquals( "Hello world.\nThe third file.\n", file_get_contents( $targetDir . "/files/bla/file3.txt" ) );
 535      }
 536  
 537      public function testExtractOneComprssedFile()
 538      {
 539          // The directory should be created automatically.
 540          $archive = $this->td->getArchive( "file_dir_symlink_link" );
 541          $archive->seek( 3 ); // 5th file.
 542          $targetDir = $this->getTempDir();
 543          $archive->extractCurrent( $targetDir );
 544  
 545          $this->assertTrue( file_exists( $targetDir . "/files/bla/bin/true" ), "Cannot find the extracted file." );
 546      }
 547  
 548      // FIXME.. file is written, instead of a link.
 549      public function testExtractOneSymbolicLink()
 550      {
 551          // The directory should be created automatically.
 552          // The link points to an non existing file.
 553          $archive = $this->td->getArchive( "file_dir_symlink_link" );
 554          $targetDir = $this->getTempDir() ;
 555  
 556          if ( $this->isWindows() ) // for windows we extract target file at first.
 557          {
 558              $archive->seek( 4 );
 559              $archive->extractCurrent( $targetDir );
 560          }
 561  
 562          $archive->seek( 7 );
 563          $archive->extractCurrent( $targetDir );
 564          $this->assertTrue( is_array (lstat( $targetDir."/files/file3.txt" ) ) );
 565      }
 566  
 567      public function testAppendNonExistingFile()
 568      {
 569          $archive = $this->td->getArchive( "file_dir_symlink_link" );
 570          try
 571          {
 572              $archive->appendToCurrent( "file_does_not_exist" , "/" );
 573              $this->fail( "Expected a 'file does not exist' exception." );
 574          }
 575          catch ( ezcBaseFileNotFoundException $e )
 576          {
 577          }
 578      }
 579  
 580      public function testAppendEmptyDirectory()
 581      {
 582          $dir = $this->getTempDir();
 583  
 584          $archive = $this->td->getArchive( "2_textfiles" );
 585          $file = $this->td->getFileName( "2_textfiles" );
 586  
 587          copy( "$dir/$file", "$dir/done_with_infozip.zip" );
 588  
 589          $archive->next();
 590  
 591          $path = "$dir/directory/";
 592          mkdir( $path );
 593          $archive->append( $path, '' );
 594  
 595          $this->unzipAndTest( $dir, "done_with_infozip.zip", "$file" );
 596      }
 597  
 598      public function testAppendAtEndOfArchive()
 599      {
 600          $dir = $this->getTempDir();
 601  
 602          $archive = $this->td->getArchive( "2_textfiles" );
 603          $file = $this->td->getFileName( "2_textfiles" );
 604  
 605          copy( "$dir/$file", "$dir/done_with_infozip.zip" );
 606  
 607          $fp = @fopen( "$dir/file3.txt", "w" );
 608          fwrite( $fp, "This is the third file." );
 609          fclose( $fp );
 610  
 611          $archive->next();
 612  
 613          $archive->appendToCurrent( "$dir/file3.txt", $dir );
 614  
 615          // We got file1.txt, file2.txt, file3.txt.
 616          exec( "zip -g -j -y $dir/done_with_infozip.zip $dir/file3.txt " );
 617  
 618          $this->unzipAndTest( $dir, "done_with_infozip.zip", "$file" );
 619      }
 620  
 621      public function testAppendToCurrentInArchive()
 622      {
 623          $dir = $this->getTempDir();
 624  
 625          $archive = $this->td->getArchive( "2_textfiles" );
 626          $file = $this->td->getFileName( "2_textfiles" );
 627  
 628          copy( "$dir/$file", "$dir/done_with_infozip.zip" );
 629  
 630          $fp = @fopen( "$dir/file3.txt", "w" );
 631          fwrite( $fp, "This is the third file." );
 632          fclose( $fp );
 633  
 634          // No next.. so file1.txt and file3.txt should be here.
 635          $archive->appendToCurrent( "$dir/file3.txt", $dir );
 636  
 637          exec( "zip -d $dir/done_with_infozip.zip file2.txt " ); // Remove file2.txt
 638          exec( "zip -g -j -y $dir/done_with_infozip.zip $dir/file3.txt " );
 639  
 640          $this->unzipAndTest( $dir, "done_with_infozip.zip", "$file" );
 641      }
 642  
 643      public function testTruncate()
 644      {
 645          $archive = $this->td->getArchive( "2_textfiles" );
 646          $archive->truncate();
 647          $this->assertFalse( $archive->valid(), "Truncated archive shouldn't contain any elements" );
 648  
 649          $archive->seek( 0 );
 650          $this->assertFalse( $archive->valid(), "Truncated archive shouldn't contain any elements" );
 651  
 652          $archive->seek( 2 );
 653          $this->assertFalse( $archive->valid(), "Truncated archive shouldn't contain any elements" );
 654      }
 655  
 656      public function testTruncatePart()
 657      {
 658          $archive = $this->td->getArchive( "file_dir_symlink_link" );
 659          $archive->truncate( 4 );
 660  
 661          // Without rewind.. should work since we truncated after our position.
 662          $entry = $archive->current();
 663          $this->assertEquals( "files/", $entry->getPath() );
 664  
 665          $entry = $archive->next();
 666          $this->assertEquals( "files/bla/", $entry->getPath() );
 667  
 668          $entry = $archive->next();
 669          $this->assertEquals( "files/bla/bin/", $entry->getPath() );
 670  
 671          $entry = $archive->next();
 672          $this->assertEquals( "files/bla/bin/true", $entry->getPath() );
 673  
 674          $this->assertFalse( $archive->next() );
 675  
 676          $archive->seek( 6 );
 677          $this->assertFalse( $archive->valid(), "Truncated archive shouldn't contain any elements" );
 678      }
 679  
 680      public function testTruncateAfterLastFile()
 681      {
 682          $archive = $this->td->getArchive( "2_textfiles" );
 683          $filename = $this->td->getFileName( "2_textfiles" );
 684          $orgSize = filesize( $this->getTempDir() . "/" .  $filename );
 685  
 686          $archive->truncate( 2 );
 687          clearstatcache();
 688          $this->assertEquals( $orgSize, filesize( $this->getTempDir() . "/" . $filename ) );
 689  
 690          $archive->truncate(1);
 691          clearstatcache();
 692          $this->assertTrue( $orgSize > filesize( $this->getTempDir() . "/" . $filename ) );
 693      }
 694  
 695      public function testAppendToEmptyArchive()
 696      {
 697          $dir = $this->getTempDir();
 698  
 699          $archive = $this->td->getArchive( "2_textfiles" );
 700          $file = $this->td->getFileName( "2_textfiles" );
 701  
 702          // copy( "$dir/$file", "$dir/done_with_infozip.zip" );
 703  
 704          $archive->extractCurrent( $dir );
 705          $archive->truncate();
 706  
 707          $fp = @fopen( "$dir/file3.txt", "w" );
 708          fwrite( $fp, "This is the third file." );
 709          fclose( $fp );
 710  
 711          $archive->appendToCurrent( "$dir/file1.txt", $dir );
 712          $this->assertTrue( $archive->valid() );
 713          $archive->appendToCurrent( "$dir/file3.txt", $dir );
 714  
 715          exec( "zip -j -y $dir/done_with_infozip.zip $dir/file1.txt $dir/file3.txt" );
 716  
 717          $this->unzipAndTest( $dir, "done_with_infozip.zip", "$file" );
 718      }
 719  
 720      public function testAppendArchiveAtOnce()
 721      {
 722          $dir = $this->getTempDir();
 723          $archive = $this->td->getArchive( "file_dir_symlink_link" );
 724          $file = $this->td->getFileName( "file_dir_symlink_link" );
 725  
 726          mkdir( "$dir/original" );
 727          mkdir( "$dir/myzip" );
 728  
 729          // Unzip all the files, and place them in the original directory.
 730          // Store the filepath of the extracted files.
 731          $files = array();
 732          do
 733          {
 734              $archive->extractCurrent( "$dir/original" );
 735              $files[] =  $dir . "/original/" . $archive->current()->getPath();
 736          } while ( $archive->next() );
 737  
 738          // Create a new archive
 739          $myzip = "$dir/my_archive.zip";
 740          $cf = new ezcArchiveCharacterFile( $myzip, true );
 741          $newArchive = new ezcArchiveZip( $cf );
 742  
 743          // Append all the extracted files.
 744          $newArchive->appendToCurrent( $files, $dir . "/original" );
 745  
 746          // Extract all (again).
 747          $newArchive->extract( "$dir/myzip" );
 748  
 749          // Compare the directories.
 750          $this->compareDirectories( "$dir/original", "$dir/myzip" );
 751      }
 752  
 753      // test for issue #13137
 754      public function testAppendToArchive()
 755      {
 756          try 
 757          {
 758              $name = $this->getTempDir() . DIRECTORY_SEPARATOR . 'my_archive.zip';
 759              $archive = ezcArchive::open( $name, ezcArchive::ZIP );
 760              $dir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'testfiles';
 761  
 762              $files = array(
 763                  "$dir/test2.php",
 764                  "$dir/test1.php",
 765                  "$dir/test7.php",
 766                  "$dir/unicode.php"
 767              );
 768  
 769              $archive->append( $files, '' );
 770              $archive->close();
 771          }
 772          catch ( Exception $e )
 773          {
 774              echo $e->__toString();
 775          }
 776      }
 777  
 778  
 779      public function unzipAndTest( $dir, $a, $b )
 780      {
 781          // They are probably not identical..
 782          // Extract both.
 783          mkdir( "$dir/infozip" ); // a
 784          mkdir( "$dir/ezczip" ); // b
 785  
 786          exec( "unzip $dir/$a -d $dir/infozip" );
 787          exec( "unzip $dir/$b -d $dir/ezczip" );
 788  
 789          $this->compareDirectories( "$dir/infozip", "$dir/ezczip" );
 790      }
 791  
 792  //        $dir =  $this->getTempDir();
 793  //        mkdir( $dir . "/php" );
 794  //        mkdir( $dir . "/gnu" );
 795  //
 796  //        foreach ( $this->complexArchive as $entry )
 797  //        {
 798  //            $this->complexArchive->extractCurrent( $dir ."/php" );
 799  //        }
 800  //
 801  //        exec( "tar -xf " . $this->complexFile . " -C $dir/gnu" );
 802  //
 803  //        $this->compareDirectories( "$dir/gnu", "$dir/php" );
 804  
 805      public static function suite()
 806      {
 807          return new PHPUnit_Framework_TestSuite( __CLASS__ );
 808      }
 809  }
 810  ?>

title

Description

title

Description

title

Description

title

title

Body