Sick Gaming
[Tut] How to Create Zip Files using PHP ZipArchive and Download - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: PHP Development (https://www.sickgaming.net/forum-82.html)
+--- Thread: [Tut] How to Create Zip Files using PHP ZipArchive and Download (/thread-99941.html)



[Tut] How to Create Zip Files using PHP ZipArchive and Download - xSicKxBot - 09-13-2022

How to Create Zip Files using PHP ZipArchive and Download

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/09/how-to-create-zip-files-using-php-ziparchive-and-download.jpg" width="550" height="377" title="" alt="" /></div><div><div class="modified-on" readability="7.1666666666667"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on September 12th, 2022.</div>
<p>Creating a zip from a folder full of files can be done in PHP using the ZipArchive class. This class instance creates a handle to read or write files to a compressed archive.</p>
<p>This class includes several properties and methods to zip file archives.</p>
<p>In this article, we will see an example of,</p>
<ol>
<li>How to create a zip archive file.</li>
<li>How to download the compressed zip file.</li>
</ol>
<p>If you want to know how to <a href="https://phppot.com/php/php-compress-image/">compress more than one image in PHP</a> image compression refer to this earlier article.</p>
<h2>How to create a zip archive file</h2>
<p>This file parses the input directory and compresses its files into a zip file. It proceeds with the following steps to create the zip file of a directory.</p>
<ol>
<li>Create a PHP ZipArchive class instance.</li>
<li>Open a zip file archive with the instance. It accepts the output zip file name and the mode to open the archive.</li>
<li>Apply a recursive parsing in the input directory.</li>
<li>If the directory includes a file, then it adds to the zip archive using&nbsp;<em>addFile()</em>.</li>
</ol>
<p>It handles the use cases of getting the possibilities of being unable to read or archive the directory. Once the zip is created, it displays a message to the browser.</p>
<p class="code-heading">create-zip-file.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
// Important: You should have read and write permissions to read
// the folder and write the zip file
$zipArchive = new ZipArchive();
$zipFile = "./example-zip-file.zip";
if ($zipArchive-&gt;open($zipFile, ZipArchive::CREATE) !== TRUE) { exit("Unable to open file.");
}
$folder = 'example-folder/';
createZip($zipArchive, $folder);
$zipArchive-&gt;close();
echo 'Zip file created.'; function createZip($zipArchive, $folder)
{ if (is_dir($folder)) { if ($f = opendir($folder)) { while (($file = readdir($f)) !== false) { if (is_file($folder . $file)) { if ($file != '' &amp;&amp; $file != '.' &amp;&amp; $file != '..') { $zipArchive-&gt;addFile($folder . $file); } } else { if (is_dir($folder . $file)) { if ($file != '' &amp;&amp; $file != '.' &amp;&amp; $file != '..') { $zipArchive-&gt;addEmptyDir($folder . $file); $folder = $folder . $file . '/'; createZip($zipArchive, $folder); } } } } closedir($f); } else { exit("Unable to open directory " . $folder); } } else { exit($folder . " is not a directory."); }
}
?&gt;
</code></pre>
<h3>Output</h3>
<pre><code>//If succeeded it returns Zip file created. //If failed it returns Unable to open directory example-folder.
[or] "example-folder is not a director.
</code></pre>
<p><img loading="lazy" class="alignnone size-large wp-image-19334" src="https://phppot.com/wp-content/uploads/2022/09/php-create-zip-550x377.jpg" alt="php create zip" width="550" height="377" srcset="https://phppot.com/wp-content/uploads/2022/09/php-create-zip-550x377.jpg 550w, https://phppot.com/wp-content/uploads/2022/09/php-create-zip-300x206.jpg 300w, https://phppot.com/wp-content/uploads/2022/09/php-create-zip-768x527.jpg 768w, https://phppot.com/wp-content/uploads/2022/09/php-create-zip.jpg 1000w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>How to download the compressed zip file</h2>
<p>In the last step, the zip file is created using the PHP ZipArchive class. That zip file can be downloaded by using the PHP code below.</p>
<p>It follows the below steps to download the zip file created.</p>
<ol>
<li>Get the absolute path of the zip file.</li>
<li>Set the header parameters like,
<ul>
<li>Content length.</li>
<li>Content type.</li>
<li>Content encoding, and more.</li>
</ul>
</li>
</ol>
<p class="code-heading">download-zip-file.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
$filename = "example-zip-file.zip";
if (file_exists($filename)) { // adjust the below absolute file path according to the folder you have downloaded // the zip file // I have downloaded the zip file to the current folder $absoluteFilePath = __DIR__ . '/' . $filename; header('Pragma: public'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Cache-Control: private', false); // content-type has to be defined according to the file extension (filetype) header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="' . basename($filename) . '";'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($absoluteFilePath)); readfile($absoluteFilePath); exit();
}
?&gt;
</code></pre>
<p>This file just has the links to trigger the function to <a href="https://phppot.com/php/create-zip-file-of-multiple-uploaded-files-using-php/">create a zip file containing the compressed archive of the directory</a>. Then, the action to download the output zip archive is called.</p>
<p class="code-heading">index.php</p>
<pre class="prettyprint"><code class="language-html">&lt;div class='container'&gt; &lt;h2&gt;Create and Download Zip file using PHP&lt;/h2&gt; &lt;p&gt; &lt;a href="create-zip-file.php"&gt;Create Zip File&lt;/a&gt; &lt;/p&gt; &lt;p&gt; &lt;a href="download-zip-file.php"&gt;Download Zip File&lt;/a&gt; &lt;/p&gt;
&lt;/div&gt;
</code></pre>
<h2>Some methods of PHP ZipArchive class</h2>
<p>We can do more operations by using the <a href="https://www.php.net/manual/en/class.ziparchive.php" target="_blank" rel="noopener">methods and properties of the PHP ZipArchive class</a>. This list of methods is provided by this PHP class.</p>
<ol>
<li>count() – used to get the number of files in the zip archive file.</li>
<li>extractTo() – extracts the archive content.</li>
<li>renameIndex() – rename a particular archive entry by index.</li>
<li>replaceFile() – replace a file in the zip archive with a new file by specifying a new path.</li>
</ol>
<h3>ZipArchive methods used in this example</h3>
<p>Some of the methods are used in this example listed below. These are frequently used methods of this class to work with this.</p>
<ol>
<li>open() – Open a zip archive file by specifying the .zip file name.</li>
<li>addFile() – To add a file from the input directory to the zip archive.</li>
<li>addEmptyDir() – adds an empty directory into the archive to load the subdirectory file of the input directory.</li>
<li>close() – closes the active ZipArchive with the reference of the handle.</li>
</ol>
<p><a class="download" href="https://phppot.com/downloads/php/php-zip-create-download.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/php-create-zip-ziparchive-files-download/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2022/09/12/how-to-create-zip-files-using-php-ziparchive-and-download/