How To Create ZIP File And Download In Laravel

How to create a ZIP file and download it?

How to add files/folders and create a ZIP file to download?

How to create a ZIP file, download it and immediately delete it (ZIP from the server)?

How to create a ZIP file, add files/folders and get the download link?

How To Extract ZIP File And Store Data In Laravel

Laravel has a lot of packages that help to make the task easy. Here we are going to create a ZIP file using ZanySoft/laravel-zip laravel package that is actually a simplified version of PHP ZipArchive

How to create a ZIP file?

To create a ZIP file you just need to get the file path and the ZIP file name. Here suppose “freefiles.zip” is the name for your new ZIP file.

Now it needs to be in a place where you can get it to let the users download it. So, the code will be:

$filename = 'freefiles.zip';
$zip = Zip::create(storage_path('app/public/zipped/'.$filename));
$zip->close();

Here “zipped” folder is created just to make things clean and keep the files separate. For now, there are no files or folders in this new ZIP file created as “freefiles.zip” that is present in the storage path.

How to add files and folders in ZIP files?

You can add a single file or multiple files or a full folder that has a number of files. Select the files or add the folder.

Here let’s add a file first. The below code adds a file called “file.txt” that is present in the storage folder.

$filename = 'freefiles.zip';
$zip = Zip::create(storage_path('app/public/zipped/'.$filename));
$zip->add(storage_path('app/public/file.txt');
$zip->close();

Now add a folder that surely has multiple files. Below “files” is a folder under the storage folder. This may have files.

By this, you will get a ZIP file named freefiles.zip that will have the folder “files” and its files. And the new ZIP file will be in the storage folder under the “zipped” folder.

$filename = 'freefiles.zip';
$zip = Zip::create(storage_path('app/public/zipped/'.$filename));
$zip->add(storage_path('app/public/files');
$zip->close();

Add one more extra file in the new ZIP

Suppose you just want to add a READ_ME text file or a certificated file (TXT or PDF or PNG) or a promotional file.

You can use this:

$zip->add(storage_path('app/public/files')->add(storage_path('app/public/read/filename.txt');

Getting a download link

Below will hit the download link and a download dialogue box will open as usual.

        return response()->download($pathToFile);

Suppose you wanna the new ZIP file removed just after getting the file downloaded. Use this: deleteFileAfterSend(true);

        return response()->download($pathToFile)->deleteFileAfterSend(true);

Now do some extra

Below the function is the same just a few things added. Like the file, ID is passed to get the file in the new ZIP file.

Just replaced the spaces to underscore(_)

public function download(File $file)
    {
        $zip = new Zip();
        $fname = str_replace(' ', '_', $file->name);
        $filename = $fname . '_freefiles.zip';
        $zip = Zip::create(storage_path('app/public/zipped/'.$filename));
        $zip->add(storage_path('app/public/files/'.$fname))->add(storage_path('app/public/read/filename.txt'));
        $zip->close();



        $pathToFile = storage_path('app/public/zipped/'.$filename);

        return response()->download($pathToFile)->deleteFileAfterSend(true);
    }

   

Related Posts