Created
July 20, 2017 18:26
-
-
Save ptasker/8274aaf120d14e249fab9efb475a92dd to your computer and use it in GitHub Desktop.
PHP Zip times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$PATH = '/your/folder/path'; | |
//ZipArchive | |
//Test 1 | |
$time_start = microtime( true ); | |
if ( ! class_exists( 'ZipArchive' ) ) { | |
return false; | |
} | |
// Get real path for our folder | |
$rootPath = realpath( $PATH ); | |
// Initialize archive object | |
$zip = new ZipArchive(); | |
$zip->open( 'file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE ); | |
// Create recursive directory iterator | |
/** @var SplFileInfo[] $files */ | |
$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $rootPath ), RecursiveIteratorIterator::LEAVES_ONLY ); | |
foreach ( $files as $name => $file ) { | |
// Skip directories (they would be added automatically) | |
if ( ! $file->isDir() ) { | |
// Get real and relative path for current file | |
$filePath = $file->getRealPath(); | |
$relativePath = substr( $filePath, strlen( $rootPath ) + 1 ); | |
// Add current file to archive | |
$zip->addFile( $filePath, $relativePath ); | |
} | |
} | |
// Zip archive will be created only after closing object | |
$zip->close(); | |
//Test 2 | |
$time_end = microtime( true ); | |
$time = $time_end - $time_start; | |
echo "PHP Zip Process Time: {$time}"; | |
$time_start = microtime( true ); | |
exec( ' zip -r clizip.zip '. $PATH ); | |
$time_end = microtime( true ); | |
$time = $time_end - $time_start; | |
echo "\nCLI Zip Process Time: {$time}"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment