Zip64Test.php 3.0 KB

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