Analyze ZIP File Contents using PHP
This code script is intended to show you how to get the information about the contents inside zipped file using php zip functions. This information is useful if you are going to develop an application like document storage which contains zip files. When you have list of zip files it’s better to have details about the contents without extracting and viewing files. In this example I’ll explain how to generate the following information about a zip file.
1. Number of folders inside the zip file
2. Number of files inside the zip file
3. File size when zipped
4. File size when unzipped
5. File types inside zip file and count of each type
Generate Zip File Information
First I’ll provide the full source code for generating this information and explain it line by line.
function zip_info_generator($zip_file_name) { $zip = zip_open($zip_file_name); $folder_count = 0; $file_count = 0; $unzipped_size = 0; $ext_array = array (); $ext_count = array (); if ($zip) { while ($zip_entry = zip_read($zip)) { if (is_dir(zip_entry_name($zip_entry))) { $folder_count++; }else { $file_count++; } $path_parts = pathinfo(zip_entry_name($zip_entry)); $ext = strtolower(trim(isset ($path_parts['extension']) ? $path_parts['extension'] : '')); if($ext != '') { $ext_count[$ext]['count'] = isset ( $ext_count[$ext]['count']) ? $ext_count[$ext]['count'] : 0; $ext_count[$ext]['count']++; } $unzipped_size = $unzipped_size + zip_entry_filesize($zip_entry); } } $zipped_size = get_file_size_unit(filesize($zip_file_name)); $unzipped_size = get_file_size_unit($unzipped_size); $zip_info = array ("folders"=>$folder_count, "files"=>$file_count, "zipped_size"=>$zipped_size, "unzipped_size"=>$unzipped_size, "file_types"=>$ext_count ); zip_close($zip); return $zip_info ; } function get_file_size_unit($file_size){ if($file_size/1024 < 1){ return $file_size."Bytes"; }else if($file_size/1024 >= 1 && $file_size/(1024*1024) < 1){ return ($file_size/1024)."KB"; }else{ return $file_size/(1024*1024)."MB"; } }
- In the above code i have created a function called zip_info_generator which takes the zip file name for reading.
- Next we have to open the zip file for reading using php zip_open function.
- I have defined 5 variables for folder,file count,unzipped file size,file types and file type count respectively.
- Then if the zip file is successfully opened we can read the zip using zip_read function.
- Inside the while loop each file inside the zip file will be assigned to $zip_entry variable for processing.
- Then we check if the zip_entry is actually a file or folder using is_dir function. Counts are updated according to the type.
- Then we get the location and paths to the zip entry by passing zip entry name to the php pathinfo function.
- pathinfo function generates the extension of a given file and next line we have assigned it to $ext variable.
- Then we update the count on the selected file extension using next 2 lines.
- Unzipped file size is calculated by summing the size of each file using the zip_entry_filesize function.
- Then we calculate the zipped file size using php filesize function.
- Next i have passed both file sizes to simple function called get_file_size_unit to get the user friendly unit for display(KB,MB..)
- Then we add the all the calculated details to the $zip_info array and return.
Following code can be used to display the gathered information to the browser.
$zip_info = zip_info_generator("test.zip"); echo "File Types
"; echo "==============================================
"; foreach ($zip_info['file_types'] as $ext=>$count) { echo "File Type : ".$ext." File Count : ".$count['count']."
"; } echo "Zipped Filesize: " . $zip_info['zipped_size'] . "
"; echo "UnZipped Filesize: " .$zip_info['unzipped_size'] . "
"; echo $zip_info['folders']."
"; echo $zip_info['files'];
Leave a Reply