Top 5 Free Dynamic ZIP Generation Libraries in PHP

Today, We want to share with you Top 5 Free Dynamic ZIP Generation Libraries in PHP.In this post we will show you generate ZIP file in php code, hear for Best of PHP scripts for generating ZIP files we will give you demo and example for implement.In this post, we will learn about Top 5 : Best open source ZIP generation libraries for PHP with an example.

Top 5 Free Dynamic ZIP Generation Libraries in PHP

There are the Following The simple About Top 5 Free Dynamic ZIP Generation Libraries in PHP Full Information With Example and source code.

As I will cover this Post with live Working example to develop php ziparchive download, so the some major generate ZIP file in php code for this example is following below.

1: using ZIPARCHIVE

Create Zip File After Upload Using PHP
upload_file.html

<html>
<body>
<form method="post" action="create_zip.php" enctype="multipart/form-data">
 <input type="file" name='file'>
 <input type="submit" name="upload_file">
</form>
</body>
</html>

create_zip.php

<?php
if(isset($_POST['upload_file']))
{
 $uploadfile=$_FILES["file"]["tmp_name"];
 $folder="images/";
 $file_name=$_FILES["file"]["name"];
 move_uploaded_file($_FILES["file"]["tmp_name"], "$folder".$_FILES["file"]["name"]);

 $zip = new ZipArchive(); // Load zip library 
 $zip_name ="upload.zip"; // Zip name
 if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
 { 
  echo "Sorry ZIP creation failed at this time";
 }
 $zip->addFile("images/".$file_name);
 $zip->close();
}
?>

2. PHP Zip File Functions

<?php
$zip = zip_open("myfilename.zip");

if ($zip)
  {
  while ($zip_entry = zip_read($zip))
    {
    echo "<p>";
    echo "zip Name: " . zip_entry_name($zip_entry) . "<br />";
    echo "Compressed Size: "
    . zip_entry_compressedsize($zip_entry);
    echo "</p>";
    }
  zip_close($zip);
  }
?>

Note : Full Examples – https://www.w3schools.com/php/php_ref_zip.asp

3. Create a Zip archive

using main PHP libs : http://php.net/manual/en/zip.examples.php

<?php

$zip = new ZipArchive();
$filename = "./product112.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}

$zip->addFromString("productfilephp.txt" . time(), "#1  php create zip file from directory as productfilephp.txt.\n");
$zip->addFromString("productfilephp2.txt" . time(), "#2  php create zip file from directory productfilephp2.txt.\n");
$zip->addFile($thisdir . "/too.php","/bookfromproductfile.php");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>

4. using Zip() libs

you can use PCLZIP . : http://www.phpconcept.net/pclzip/

<?php
require_once('pclzip.lib.php');
$files_to_zip = array(
'product-images/1.jpg',
'product-images/2.jpg',
'product-images/5.jpg',
'ietms/ringo.gif',
'computer.jpg',
'love.gif'
 );

$dir = 'd:/projectname/upload/docs/'; 
$new_files = array();
  foreach($files_to_zip as $value){
   $new_files[] = $dir.$value;
}
print_r($new_files);
// Create PHP Object
$archive = new PclZip("compressed.zip");

$files_archive = $archive->add($new_files, PCLZIP_OPT_REMOVE_PATH, $dir, PCLZIP_OPT_ADD_PATH, 'myFiles');
if ($files_archive == 0) {
    die("Error : ".$archive->errorInfo(true));
}   
else{
    echo "Your Zip Archive Files Created";
}

5. Unpack a Zip Files

How to Unpack a Zip File using PHP?

<?php
 
$file = file.zip; 
$zip = new ZipArchive;
 
$zipped = $zip->open($file);
 
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);
 
if ($zipped) {
 
  $extract = $zip->extractTo($path);
   if($extract){
    echo "Your file extracted to $folder_name";
  } else {
    echo "your file not extracted";
  }
 
  //close the zip
  $zip->close();  
}
?>

Extra Bonus:

downloading zip file from url using php

$zip_file_name = "product-items-csv/".$cat_id."-box."."zip";
downloadZipFile($items['liveReportUrl'], $zip_file_name);

function downloadZipFile($url, $filepath){
    $fp = fopen($filepath, 'w');
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    $data = curl_exec($ch);
    curl_close($ch);
    fclose($fp);
 }

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Top 5 Free Dynamic ZIP Generation Libraries in PHP.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.