Zip64Test.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace PhpZip\Tests\SlowTests;
  3. use PhpZip\Constants\ZipCompressionMethod;
  4. use PhpZip\Exception\ZipException;
  5. use PhpZip\Tests\ZipTestCase;
  6. use PhpZip\Util\FilesUtil;
  7. use PhpZip\ZipFile;
  8. /**
  9. * @internal
  10. *
  11. * @large
  12. */
  13. class Zip64Test extends ZipTestCase
  14. {
  15. /**
  16. * @throws ZipException
  17. */
  18. public function testCreateLargeZip64File()
  19. {
  20. if (\PHP_INT_SIZE === 4) { // php 32 bit
  21. static::markTestSkipped('Only php-64 bit.');
  22. return;
  23. }
  24. if (!self::existsProgram('fallocate')) {
  25. static::markTestSkipped('Cannot find the program "fallocate" for the test');
  26. return;
  27. }
  28. $basedir = \dirname($this->outputFilename);
  29. $tmpLargeFile = $basedir . '/large_bin_file.bin';
  30. $sizeLargeBinFile = (int) (4.2 * 1024 * 1024 * 1024);
  31. $needFreeSpace = $sizeLargeBinFile * 4;
  32. $diskFreeSpace = disk_free_space($basedir);
  33. if ($needFreeSpace > $diskFreeSpace) {
  34. static::markTestIncomplete(
  35. sprintf(
  36. 'Not enough disk space for the test. Need to free %s',
  37. FilesUtil::humanSize($needFreeSpace - $diskFreeSpace)
  38. )
  39. );
  40. return;
  41. }
  42. try {
  43. $commandCreateLargeBinFile = 'fallocate -l ' . escapeshellarg($sizeLargeBinFile) . ' ' . escapeshellarg($tmpLargeFile);
  44. exec($commandCreateLargeBinFile, $output, $returnCode);
  45. if ($returnCode !== 0) {
  46. static::markTestIncomplete('Cannot create large file. Error code: ' . $returnCode);
  47. return;
  48. }
  49. $zipFile = new ZipFile();
  50. $zipFile
  51. ->addFile($tmpLargeFile, 'large_file1.bin', ZipCompressionMethod::STORED)
  52. ->addFile($tmpLargeFile, 'large_file2.bin', ZipCompressionMethod::DEFLATED)
  53. ->saveAsFile($this->outputFilename)
  54. ->close()
  55. ;
  56. if (is_file($tmpLargeFile)) {
  57. unlink($tmpLargeFile);
  58. }
  59. self::assertCorrectZipArchive($this->outputFilename);
  60. if (!is_dir($this->outputDirname)) {
  61. mkdir($this->outputDirname, 0755, true);
  62. }
  63. $zipFile->openFile($this->outputFilename);
  64. $zipFile->extractTo($this->outputDirname);
  65. static::assertTrue(is_file($this->outputDirname . '/large_file1.bin'));
  66. static::assertTrue(is_file($this->outputDirname . '/large_file2.bin'));
  67. $zipFile->deleteFromName('large_file1.bin');
  68. $zipFile->saveAsFile($this->outputFilename);
  69. self::assertCorrectZipArchive($this->outputFilename);
  70. } finally {
  71. if (is_file($tmpLargeFile)) {
  72. unlink($tmpLargeFile);
  73. }
  74. }
  75. }
  76. }