Updated April 10, 2023
Introduction to Laravel Zip
The zip in laravel is used for file backup and file compression. The library comprised of the zip is compiled to automate and simplify the management of zip files with the support of PHP archive zip. The composer JSON package is used for installation and helps to update the composer who is used to download the package. The service provider requires any soft that updates the configuration files and are used to provide arrays in the app.php file. The user can work on the zip service provider class to façade the minimal code.
Overview of Laravel Zip
Zip usage at the top can be modified in the location, zip/zip façade:: class.
The file location is used to manage the zip files where all the basic operations can be done.
$ zip = Zip :: open (‘file.zip’);
To create the zip file, use the following command, $ zip = Zip :: create (‘file.zip’);
To check on the zip file, use $ is_valid = Zip :: check (‘file.zip’);
To extract the zip file with entire archive, use $ zip -> extract (‘/ path/ to/ uncompressed/ files’);
To extract the file, use, $ zip -> extract (‘/path/ to/ uncompressed/ files’, ‘file’);
To extract the multiple files, $ zip -> extract (‘/path/to/uncompressed/files’, array(‘file1′,’file2’));
To check if some zip folder has file, use $zip->has (‘/ path/ to/ file/ in/ archive’);
To check if there is some insensitive file case in the archive option use
$zip -> has (‘/ path/ to/ file/ in/ archive’, Zip Archive :: FL_NOCASE);
To ensure, if archive file has some directory component, use $ zip -> has (‘file’, ZipArchive :: FL _ NODIR);
To include the directory of file to zip, use $ zip -> add (‘/ path/ to/ my/ file’);
To declare the path, use $ zip -> set path -> add file;
To include directory, use $ zip -> add directory (‘/ path/ to/ my/ directory’);
To add the file contents in directory $ zip -> add (‘/ path/ to/ my/ directory’, true);
To add multiple directories or files to zip, the user can work on the array, chain methods, and path declaration.
To declare any path, use $ zip -> set Path (‘/path/to/my’) -> add(‘file1’) -> add(‘file2’);
To delete any directory or file from the zip, use $ zip -> delete (‘file’);
It can be used to delete multiple files or directories from the zip.
To use array as a parameter, $ zip -> delete ( array (‘file1’, ‘file2’) );
$ zip -> delete (‘file1’) -> delete (‘file2’);
List content of zip file use $
$ zip -> listFiles ();
To close the zip file use, $ zip -> close();
To skip the hidden files when adding directories and to use set mode, type the following command
$ zip -> set Skipped (‘HIDDEN’), to include get mode, use $ mode = $ zip -> get skipped ();.
At the time of password application, for zip retrieval and to set a password, type the command:
Code:
$ zip -> set Password ("welome123");
$ password = $ zip -> get password();
How to Create a Zip File in Laravel?
In this section, the creation of a zip file in laravel using the archive option in zipping. The user has to build functionality in the application or project to compress the documents as zipping, and then it is downloaded as a whole and unit zip file. The user can include multiple laravel packages to achieve practical functionality. But here, we don’t use any package, and the archive class of zip does the creation of a zip file.
1. Route creation: The user has to create a single route to display the simple creation of the zip option.
Route :: get ( ‘create-zip’, ‘ZipArchive Controller @ index’) -> name (‘create-zip’);
2. Button blade development: A single-blade file should be created to show the unit zip download of the bootstrap option, and when the user chooses it, he should be able to download a few images and files from the zip which has already been combined to make it as a unit zip.
Code:
<head>
<title> Create Zip </title>
<link ref="stylesheet" type = "text/css" h ref </head>
<body>
<div class= "container"
{ route ('create newzip', ['downloadzip' =>'zip']) }}" class="btn btn-info" >Download ZIP</a>
</div>
</body>
3. Controller creation: The zip archive controller should be created using the code below.
Code:
namespace Application \ Http\ Controllers;
use App\ Http\ Requests;
use Illuminate\ Http\ Request;
use Zip Archive;
class Zip Archive Controller extends Controller
{
/**
* displays the dashboard of the application
*
* @return \ Illuminate\ Http\ Response
*/
public function index (Request $request)
{
If ( $ request -> has ('download'))
{
Director folder definition
$ public_dir = public_path();
Extract the file name
$ zip FileName = 'All Documents.zip';
$zip = new Zip Archive;
if ($ zip -> == true {
include the file
$zip->add File(file-path,'file_name');
// Close Zip Archive
$zip - > close();
}
To set the header file,
$ header file= array (
'Content-Type' => 'application / octet-stream',
);
$ filetopath=$ public_dir.'/'. $ zip File Name;
Download response creation
If (file_exists ($ filetopath)) {
return response() -> download ($ filetopath,$ zipFileName,$ headers);
}
}
return view('create Zip');
}
}
Output:
If the user wants to include more space, then any code snippet can be added with a zip archive.
To add multiple files, use:
Code:
For each ( $ files as $file)
{
$zip -> add File ($ file -> path, $file -> name);
}
$ zip -> closefile ();
}
The user can now execute the example code using the PHP artisan server and then view the same using the browser URL.
Conclusion
Hence these are some overviews and creations of laravel zip files, which can be customized according to user preference. Moreover, the user can imply and execute the code instantly.
Recommended Articles
We hope that this EDUCBA information on “Laravel Zip” was beneficial to you. You can view EDUCBA’s recommended articles for more information.