How to make a Zip file using PHP

Hello Everyone

In this tutorial, we are going to make a zip file using PHP. and we also unzip the zip file using PHP. we may require this when we creating applications with PHP. if you want to download a gallery as zip or you have to upload zip file to and program needs to unzip it.  Both are really simple and we can do with a few lines of PHP code.

PHP has built-in extensions for dealing with compressed files. There should be no need to use system calls for this. ZipArchivedocs is one option.

Let’s write the code.

Create a new zip file

$zip = new ZipArchive;
if ($zip->open('test_new.zip', ZipArchive::CREATE) === TRUE)
{
    // Add files to the zip file
    $zip->addFile('test.txt');
    $zip->addFile('test.pdf');
 
    // Add random.txt file to zip and rename it to newfile.txt
    $zip->addFile('random.txt', 'newfile.txt'); 
  
 
    // All files are added, so close the zip file.
    $zip->close();
}

 

Overwrite an existing zip file

$zip = new ZipArchive;
if ($zip->open('test_folder.zip', ZipArchive::CREATE) === TRUE)
{
    // Add files to the zip file inside demo_folder
    $zip->addFile('text.txt', 'demo_folder/test.txt');
    $zip->addFile('test.pdf', 'demo_folder/test.pdf');
 
    // Add random.txt file to zip and rename it to newfile.txt and store in demo_folder
    $zip->addFile('random.txt', 'demo_folder/newfile.txt');
 
    // Add a file demo_folder/new.txt file to zip using the text specified
    $zip->addFromString('demo_folder/new.txt', 'text to be added to the new.txt file');
 
    // All files are added, so close the zip file.
    $zip->close();
}

Create a zip file with all files from a directory

$zip = new ZipArchive;
if ($zip->open('test_dir.zip', ZipArchive::CREATE) === TRUE)
{
    if ($handle = opendir('demo_folder'))
    {
        // Add all files inside the directory
        while (false !== ($entry = readdir($handle)))
        {
            if ($entry != "." && $entry != ".." && !is_dir('demo_folder/' . $entry))
            {
                $zip->addFile('demo_folder/' . $entry);
            }
        }
        closedir($handle);
    }
 
    $zip->close();
}

 



Okay that’s perfect. Now we have to unzip the file

Unzip a Zip File

$zip = new ZipArchive;
$res = $zip->open('file.zip');
if ($res === TRUE) {
  $zip->extractTo('/myzips/extract_path/');
  $zip->close();
  echo 'woot!';
} else {
  echo 'doh!';
}

That’s all .. so simple right .. try this .. and hope this helps.

 

Download Source Code : 

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now