Laravel 5 Extract zip/archive file using chumper/zipper composer package.

How to extract zip file using Laravel `chumper/zipper` package step by step. Let’s, first of all, install “chumper/zipper” package. So, Open command prompt and goto the root directory of the Laravel and fire below command.

composer require chumper/zipper

After installing the package open the app config file from “config/app.php” file then append below Provides and Aliases to app.php file.

'providers' => [
    'Chumper\Zipper\ZipperServiceProvider',
],
'aliases' => [
    'Zipper' => 'Chumper\Zipper\Zipper'
],

You can now access Zipper with the Zipper alias for extracting the zip file. So let’s extract zip file. Make sure you have the “test.zip” file inside of the public directory and Also make sure the “thecodingstuff” directory exists inside of the public directory. If not exists please create both file and directory.

Then used below code for extract the zip file of images.

public function extractZip()
{
    $zipPath = public_path('test.zip');
    \Zipper::make($zipPath)->extractTo('thecodingstuff')->close();
}

If you face any issue after using above code written the comment below.

Laravel 5 create and download zip/archive file using chumper/zipper composer package.

How to create zip file using Laravel `chumper/zipper` package. Let’s see how to create zip file or folder step by step. Let’s, first of all, install “chumper/zipper” package. So, Open command prompt and goto the root directory of the Laravel and fire below command.

composer require chumper/zipper

After installing the package open the app config file from “config/app.php” file then append below Provides and Aliases to app.php file.

'providers' => [
    'Chumper\Zipper\ZipperServiceProvider',
],
'aliases' => [
    'Zipper' => 'Chumper\Zipper\Zipper'
],

You can now access Zipper with the Zipper alias for creating the zip file. So let’s create the zip file of images existing on the public folder. Make sure you have the “images” folder inside of the public directory and Also make sure the “thecodingstuff” directory exists inside of the public directory. If not exists please create both directories.

Then used below code for creating the zip file of images.

public function downloadZip()
{
    $images = glob(public_path('images/*'));
    \Zipper::make('thecodingstuff/filename.zip')->add($files)->close();
    return response()->download(public_path('thecodingstuff/filename.zip'));
}

If you face any issue after using above code written the comment below.

PHP programmatically create archive file of directory or project folder.

How to create archive file programmatically using PHP. let’s see the easy way to create an archive file using PHP. for creating archive file just copy below function to your code file.

<?php
function Zip($source, $destination)
{
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }
    $zip = new ZipArchive();
    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
        return false;
    }
    $source = str_replace('\\', '/', realpath($source));
    if (is_dir($source) === true)
    {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
        foreach ($files as $file)
        {
            $file = str_replace('\\', '/', $file);
            // Ignore "." and ".." folders
            if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
                continue;
            $file = realpath($file);
            if (is_dir($file) === true)
            {
                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
            }
            else if (is_file($file) === true)
            {
                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
            }
        }
    }
    else if (is_file($source) === true)
    {
        $zip->addFromString(basename($source), file_get_contents($source));
    }
    return $zip->close();
}

For creating the archive file just call that function like.

Zip(DIRECTORY_PATH, ‘zipfilename.zip’);

For creating the archive file of the current directory just use below code like.

Zip(., ‘zipfilename.zip’);

If you face any after adding this code please write a comment.