PHP Zipper package using PHP ZipArchive
- 2021年12月21日
- 技術情報, Web Service
Today I would like to share a PHP package that I wrote last weekend. This was written to make Zipping processes easily and automatically using PHP ZipArchive. Let’s take a look.
First of all, install the package with composer.
composer require waithaw/phpzipper
After creating Zip Object, you can use the following methods.
include_once "vendor/autoload.php";
use WaiThaw\PhpZipper\Zip;
$zip = new Zip();
These are the example file lists.
$filelists= [
'D:\testfolder\test1.txt',
'D:\testfolder\test1folder\test2.txt'
];
$file = 'D:\testfolder\test1.txt';
Creating a Zip file from Single file or multiple files
You can create an archive zip file from single file or multiple files.
1st parameter – output zip path
2nd parameter – a file or files to be zipped
3rd parameter – setting a password
- Zipping a single file or mutiple files with no password
$zip->createFromFiles('backup.zip', $file);
//OR
$zip->createFromFiles('backup.zip', $filelists);
- Zipping a single file or mutiple files with password
$zip->createFromFiles('backup.zip', $file ,'password');
//OR
$zip->createFromFiles('backup.zip', $filelists,'password');
Creating a Zip file from a directory including sub directories.
You can archive all files and subfolders in a directory into a zip file.
- Zipping a directory with no password
$zip->createFromDir('backup.zip','D:\testfolder');
- Zipping a directory with password
$zip->createFromDir('backup.zip','D:\testfolder','password');
Extracting a simple or password-protected zip file
- Extracting a simple zip file.
$zip->extractTo('backup.zip','D:\outputpath');
- Extracting a password-protected zip file
$zip->extractTo('backup.zip','D:\outputpath', 'password');
Downloading zip files
You can download the zip file at once archiving.
$zip->createFromFiles('backup.zip', $file)->download();
$zip>createFromDir('backup.zip','D:\testfolder')->download();
And you can also delete the zip file after downloaded, by passing ‘delete’ string in download() method.
$zip->createFromFiles('backup.zip', $file)->download('delete');
$zip>createFromDir('backup.zip','D:\testfolder')->download('delete');
This is all for now. I will continue to update this package for more features and details.
Hope you enjoy that.
By Asahi
waithaw at 2021年12月21日 10:00:00