{"id":11244,"date":"2021-12-21T10:00:00","date_gmt":"2021-12-21T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=11244"},"modified":"2021-12-21T10:05:09","modified_gmt":"2021-12-21T01:05:09","slug":"php-zipper-package-using-php-ziparchive","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/11244","title":{"rendered":"PHP Zipper package using PHP ZipArchive"},"content":{"rendered":"\n<p>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&#8217;s take a look.<\/p>\n\n\n\n<p>First of all, install the package with composer. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require waithaw\/phpzipper<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4>After creating Zip Object, you can use the following methods.<\/h4>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>include_once \"vendor\/autoload.php\";\n\nuse WaiThaw\\PhpZipper\\Zip;\n\n$zip = new Zip();<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p><\/p>\n\n\n\n<p>These are the example file lists.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$filelists= &#091;\n    'D:\\testfolder\\test1.txt',\n    'D:\\testfolder\\test1folder\\test2.txt'\n];\n\n$file = 'D:\\testfolder\\test1.txt';<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4><strong>Creating a Zip file from Single file or multiple files  <\/strong><font><\/h4>\n\n\n\n<p>You can create an archive zip file from single file or multiple files.<\/p>\n\n\n\n<p>1st parameter  &#8211; output zip path<\/p>\n\n\n\n<p>2nd parameter &#8211; a file or files to be zipped<\/p>\n\n\n\n<p>3rd parameter &#8211; setting a password<\/p>\n\n\n\n<ul><li><strong>Zipping a single file or mutiple files<\/strong> <strong>with no password<\/strong> <\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\n$zip-&gt;createFromFiles('backup.zip', $file);\n\n\/\/OR\n\n$zip-&gt;createFromFiles('backup.zip', $filelists);\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p><\/p>\n\n\n\n<ul><li><strong>Zipping a single file or mutiple files with password<\/strong><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$zip-&gt;createFromFiles('backup.zip', $file ,'password');\n\n\/\/OR\n\n$zip-&gt;createFromFiles('backup.zip', $filelists,'password');<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h4><strong>Creating a Zip file from a directory including sub directories.<\/strong><\/h4>\n\n\n\n<p>You can archive all files and subfolders in a directory into a zip file.<\/p>\n\n\n\n<ul><li> <strong>Zipping a directory with no password<\/strong><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$zip-&gt;createFromDir('backup.zip','D:\\testfolder');<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<ul><li><strong>Zipping a directory with password<\/strong><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$zip-&gt;createFromDir('backup.zip','D:\\testfolder','password');<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4><strong>Extracting a simple or password-protected zip file<\/strong><\/h4>\n\n\n\n<ul><li><strong>Extracting a simple zip file.<\/strong><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$zip-&gt;extractTo('backup.zip','D:\\outputpath');<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<ul><li><strong>Extracting a password-protected zip file<\/strong><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$zip-&gt;extractTo('backup.zip','D:\\outputpath', 'password');<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4><strong>Downloading zip files <\/strong><\/h4>\n\n\n\n<p>You can download the zip file at once archiving. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$zip-&gt;createFromFiles('backup.zip', $file)-&gt;download();\n\n$zip&gt;createFromDir('backup.zip','D:\\testfolder')-&gt;download();<\/code><\/pre>\n\n\n\n<p><font><\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p><\/p>\n\n\n\n<p>And you can also delete the zip file after downloaded, by passing &#8216;delete&#8217; string in download() method.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$zip-&gt;createFromFiles('backup.zip', $file)-&gt;download('delete');\n\n$zip&gt;createFromDir('backup.zip','D:\\testfolder')-&gt;download('delete');\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p><font><\/p>\n\n\n\n<p>This is all for now. I will continue to update this package for more features and details. <\/p>\n\n\n\n<p>Hope you enjoy that.<\/p>\n\n\n\n<p>By Asahi<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11244\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/11244\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"PHP Zipper package using PHP ZipArchive\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11244\" data-text=\"PHP Zipper package using PHP ZipArchive\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11244\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11244\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Today I would like to share a PHP package that I wrote last weekend. This was written to make Zipping processe [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100,35],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11244"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=11244"}],"version-history":[{"count":25,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11244\/revisions"}],"predecessor-version":[{"id":11288,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11244\/revisions\/11288"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=11244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=11244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=11244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}