2
0

Zip64Test.php 2.8 KB

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