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 | |
//Start of class | |
class xZip { | |
public function __construct() {} | |
public function __destruct() {} | |
public function zip($source = NULL, $destination = "./",$max_size_of_1_zip_archive) { | |
if (!$destination || trim($destination) == "") { | |
$destination = "./"; | |
} | |
$input = array(); | |
$this->_rglobRead($source, $input); | |
//$maxinput = count($input); | |
//$splitinto = (($maxinput / $limit) > round($maxinput / $limit, 0)) ? round($maxinput / $limit, 0) + 1 : round($maxinput / $limit, 0); | |
//Add code to ensure that a max of $max_size_of_1_zip_archive MB are added to zip archive | |
$total_number_of_images = sizeof($input); | |
echo "Total Number of Images:".$total_number_of_images."\n"; | |
$current_image_array_counter_position = 0; | |
$beginning_image_array_index_for_archive = 0; | |
$archive_counter = 0; | |
$current_archive_size = 0; | |
while($current_image_array_counter_position < $total_number_of_images) | |
{ | |
$current_image_file_size = filesize($input[$current_image_array_counter_position]); | |
$current_image_file_size = ($current_image_file_size/1024);//to convert from bytes to Kilobytes. | |
/*echo "beginning_image_array_counter_position value: ".$beginning_image_array_index_for_archive."\n"; | |
echo "current_image_array_counter_position value: ".$current_image_array_counter_position."\n"; | |
*/ | |
//sleep(1); | |
if(($current_archive_size + ($current_image_file_size) ) > ($max_size_of_1_zip_archive*1024)) | |
{ | |
$this->_zip(array_slice($input, $beginning_image_array_index_for_archive, ($current_image_array_counter_position-$beginning_image_array_index_for_archive), true), $archive_counter++, $destination); | |
$beginning_image_array_index_for_archive = $current_image_array_counter_position; | |
echo "Archive Number: ".($archive_counter)."\n"; | |
echo "Finished Archive Size: ".$current_archive_size."\n\n"; | |
$current_archive_size = 0; | |
} | |
else | |
{ | |
$current_archive_size += ($current_image_file_size); | |
$current_image_array_counter_position++; | |
echo "Current Image File Size: ".$current_image_file_size."\n"; | |
echo "Current Archive Size: ".$current_archive_size."\n"; | |
echo "Number of Files in Archive: ".($current_image_array_counter_position-$beginning_image_array_index_for_archive)."\n"; | |
} | |
} | |
if($current_archive_size > 0) | |
{ | |
$this->_zip(array_slice($input, $beginning_image_array_index_for_archive, ($current_image_array_counter_position-$beginning_image_array_index_for_archive), true), $archive_counter++, $destination); | |
$beginning_image_array_index_for_archive = $current_image_array_counter_position; | |
echo "Archive Number: ".($archive_counter)."\n"; | |
echo "Finished Archive Size: ".$current_archive_size."\n"; | |
} | |
//echo "size of input array: ".(sizeof($input))."\n"; | |
//echo "value of begining of array index: ".$beginning_image_array_index_for_archive."\n"; | |
//echo "value of current of array index: ".$current_image_array_counter_position."\n"; | |
unset($input); | |
return; | |
} | |
public function unzip($source, $destination) { | |
@mkdir($destination, 0777, true); | |
foreach ((array) glob($source . "/*.zip") as $key => $value) { | |
$zip = new ZipArchive; | |
if ($zip->open(str_replace("//", "/", $value)) === true) { | |
$zip->extractTo($destination); | |
$zip->close(); | |
} | |
} | |
} | |
private function _zip($array, $part, $destination) { | |
$zip = new ZipArchive; | |
@mkdir($destination, 0777, true); | |
if ($zip->open(str_replace("//", "/", "{$destination}/partz{$part}.zip"), ZipArchive::CREATE)) { | |
foreach ((array) $array as $key => $value) { | |
$value_of_filename_in_ziparchive = ""; | |
$value_of_filename_in_ziparchive = substr($value, strrpos($value, '/') + 1); | |
//echo (sizeof($array))." ".$part." ".$value_of_filename_in_ziparchive."\n"; | |
$zip->addFile($value, $value_of_filename_in_ziparchive); | |
} | |
$zip->close(); | |
} | |
} | |
private function _rglobRead($source, &$array = array()) { | |
if (!$source || trim($source) == "") { | |
$source = "."; | |
} | |
foreach ((array) glob($source . "/*/") as $key => $value) { | |
$this->_rglobRead(str_replace("//", "/", $value), $array); | |
} | |
foreach ((array) glob($source . "*.*") as $key => $value) { | |
$array[] = str_replace("//", "/", $value); | |
} | |
} | |
} | |
//End of Class | |
//Parent process code: | |
//Defining session specific variables below: | |
$max_size_of_1_zip_archive = 5; //This value is in MBs. | |
$input_path_of_image_files = "~/source_pictures/"; | |
$output_path_of_zip_archives = "~/destination_archives/"; | |
//Invoking class xZip: | |
$zip = new xZip(); | |
$zip->zip($input_path_of_image_files, $output_path_of_zip_archives,$max_size_of_1_zip_archive); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modified from http://php.net/manual/en/class.ziparchive.php