How to Create, Extract Zip and Delete Files or Folders from Zip In Laravel

Published on Feb 27, 2020

In this article, I’m going to share an easy method to zip & unzip in Laravel. I’m testing in Laravel 6.2. Let’s get started:

Table of Contents

  1. Install Package
  2. Create Zip
  3. Extract Zip
  4. Delete Files/Folders from Zip
  5. Basic Operations

Install Package

We’ll use zanysoft/laravel-zip package. Install the package using this command:

composer require zanysoft/laravel-zip

Create Zip

We’re able to create zip using this package. I’m showing a few exmaples.

use ZanySoft\Zip\Zip;

$zip = Zip::create(''); // create zip
$zip->add(public_path()); // add public path to the zip file

We can also add more files/folders to the zip file like these:


// declaring path

// add directory

// add directory (only its content)
$zip->add('/path/to/my/directory', true);

We can also add multiple files/directories to zip:

// using array as parameter
$zip->add( array('/path/to/my/file1', '/path/to/my/file2');

// chaining methods

// declaring path

Extract Zip

We can easily unzip any zip file. Have a look at the example:

$zip = Zip::open('');
$zip->extract(public_path() . '/uncompressed');

We can also unzip multiple multiple files too. Here are some examples:

// extract whole archive

// extract a file
$zip->extract('/path/to/uncompressed/files', 'file');

// extract multiple files
$zip->extract('/path/to/uncompressed/files', array('file1','file2'));

Delete Files/Folders from Zip

By using this package we’re able to delete files/folders from the compressed zip file. Take a look at the example:

$zip = Zip::open('');

We can also delete multiple files/directories from zip:

// using array as parameter
$zip->delete( array('file1', 'file2') );

// chaining methods

Basic Operations

Here are some of functionalities of the package:

Check zip file:

$is_valid = Zip::check('');

List content of zip file:


Use password for zip extraction:

// set password

// get password
$password = $zip->getPassword();

Close zip file:


There are more additional functionalities. Please check these from GitHub.

Comments (0)