Deltik

https://stackoverflow.com/a/53997599 test case

Jan 10th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. root@demo:~# ls -l
  2. total 2
  3. drwxr-xr-x 5 root root    8 Jan 10 12:24 36287554
  4. -rw-r--r-- 1 root root 1408 Jan 10 12:27 36287554.php
  5. root@demo:~# ls -l 36287554/
  6. total 3
  7. -rw------- 1 root root 0 Jan 10 12:23 file_a
  8. -rw-r----- 1 root root 0 Jan 10 12:23 file_b
  9. -rw-r--r-- 1 root root 0 Jan 10 12:23 file_c
  10. drwx------ 2 root root 2 Jan 10 12:24 folder_a
  11. drwxrwx--- 2 root root 2 Jan 10 12:24 folder_b
  12. drwxrwxrwx 2 root root 2 Jan 10 12:24 folder_c
  13. root@demo:~# php --version
  14. PHP 5.6.10 (cli) (built: Jan 10 2019 12:07:52)
  15. Copyright (c) 1997-2015 The PHP Group
  16. Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
  17. root@demo:~# php 36287554.php
  18. root@demo:~# zipinfo 36287554.zip
  19. Archive:  36287554.zip
  20. Zip file size: 770 bytes, number of entries: 7
  21. drwxr-xr-x  2.0 unx        0 b- stor 19-Jan-10 12:27 36287554/
  22. -rw-------  2.0 unx        0 b- stor 19-Jan-10 12:23 36287554/file_a
  23. drwxrwxrwx  2.0 unx        0 b- stor 19-Jan-10 12:27 36287554/folder_c/
  24. -rw-r-----  2.0 unx        0 b- stor 19-Jan-10 12:23 36287554/file_b
  25. drwxrwx---  2.0 unx        0 b- stor 19-Jan-10 12:27 36287554/folder_b/
  26. drwx------  2.0 unx        0 b- stor 19-Jan-10 12:27 36287554/folder_a/
  27. -rw-r--r--  2.0 unx        0 b- stor 19-Jan-10 12:23 36287554/file_c
  28. 7 files, 0 bytes uncompressed, 0 bytes compressed:  0.0%
  29. root@demo:~# cat 36287554.php
  30. <?php
  31. $templateArchive = new ZipArchive();
  32. $templateArchive->open('36287554.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
  33.  
  34. $files = new RecursiveIteratorIterator(
  35.         new RecursiveDirectoryIterator('36287554'),
  36.         RecursiveIteratorIterator::LEAVES_ONLY
  37. );
  38.  
  39. foreach ($files as $name => $file)
  40. {
  41.         // Get real and relative path for current file
  42.         $filePath = $file->getRealPath();
  43.  
  44.         $relativePath = $name;
  45.  
  46.         // Add regular files
  47.         if (!$file->isDir())
  48.         {
  49.                 // Add current file to archive
  50.                 $templateArchive->addFile($filePath, $relativePath);
  51.         }
  52.         elseif (substr($relativePath, -2) === "/.")
  53.         {
  54.                 // Remove the dot representing the current directory
  55.                 $relativePath = substr($relativePath, 0, -1);
  56.                 // Add current directory to archive
  57.                 $templateArchive->addEmptyDir($relativePath);
  58.         }
  59.         else
  60.         {
  61.                 continue;
  62.         }
  63.  
  64.         $templateArchive->setExternalAttributesName($relativePath,
  65.                 ZipArchive::OPSYS_UNIX,
  66.                 fileperms($filePath) << 16);
  67. }
  68.  
  69. // Zip archive will be created only after closing object
  70. $templateArchive->close();
Add Comment
Please, Sign In to add comment