Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- root@demo:~# ls -l
- total 2
- drwxr-xr-x 5 root root 8 Jan 10 12:24 36287554
- -rw-r--r-- 1 root root 1408 Jan 10 12:27 36287554.php
- root@demo:~# ls -l 36287554/
- total 3
- -rw------- 1 root root 0 Jan 10 12:23 file_a
- -rw-r----- 1 root root 0 Jan 10 12:23 file_b
- -rw-r--r-- 1 root root 0 Jan 10 12:23 file_c
- drwx------ 2 root root 2 Jan 10 12:24 folder_a
- drwxrwx--- 2 root root 2 Jan 10 12:24 folder_b
- drwxrwxrwx 2 root root 2 Jan 10 12:24 folder_c
- root@demo:~# php --version
- PHP 5.6.10 (cli) (built: Jan 10 2019 12:07:52)
- Copyright (c) 1997-2015 The PHP Group
- Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
- root@demo:~# php 36287554.php
- root@demo:~# zipinfo 36287554.zip
- Archive: 36287554.zip
- Zip file size: 770 bytes, number of entries: 7
- drwxr-xr-x 2.0 unx 0 b- stor 19-Jan-10 12:27 36287554/
- -rw------- 2.0 unx 0 b- stor 19-Jan-10 12:23 36287554/file_a
- drwxrwxrwx 2.0 unx 0 b- stor 19-Jan-10 12:27 36287554/folder_c/
- -rw-r----- 2.0 unx 0 b- stor 19-Jan-10 12:23 36287554/file_b
- drwxrwx--- 2.0 unx 0 b- stor 19-Jan-10 12:27 36287554/folder_b/
- drwx------ 2.0 unx 0 b- stor 19-Jan-10 12:27 36287554/folder_a/
- -rw-r--r-- 2.0 unx 0 b- stor 19-Jan-10 12:23 36287554/file_c
- 7 files, 0 bytes uncompressed, 0 bytes compressed: 0.0%
- root@demo:~# cat 36287554.php
- <?php
- $templateArchive = new ZipArchive();
- $templateArchive->open('36287554.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
- $files = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator('36287554'),
- RecursiveIteratorIterator::LEAVES_ONLY
- );
- foreach ($files as $name => $file)
- {
- // Get real and relative path for current file
- $filePath = $file->getRealPath();
- $relativePath = $name;
- // Add regular files
- if (!$file->isDir())
- {
- // Add current file to archive
- $templateArchive->addFile($filePath, $relativePath);
- }
- elseif (substr($relativePath, -2) === "/.")
- {
- // Remove the dot representing the current directory
- $relativePath = substr($relativePath, 0, -1);
- // Add current directory to archive
- $templateArchive->addEmptyDir($relativePath);
- }
- else
- {
- continue;
- }
- $templateArchive->setExternalAttributesName($relativePath,
- ZipArchive::OPSYS_UNIX,
- fileperms($filePath) << 16);
- }
- // Zip archive will be created only after closing object
- $templateArchive->close();
Add Comment
Please, Sign In to add comment