PHP : How to use ZipArchive class

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@gentlemanoi·
0.000 HBD
PHP : How to use ZipArchive class
<center>![codeigniter-logo.png](https://s3.amazonaws.com/assets.doesnotscale.com/blog/2016/08/php_500x326-1471131047927.png)
</center>

#### What Will I Learn?
This tutorial is about how to use ZipArchive class in PHP to open and add files in a zip file.

- How to  open a zip file
- How to add files in a zip
- How to overwrite files in a zip

#### Requirements
- PHP version 5.3+
- Basic knowledge about PHP

#### Difficulty
- Basic

#### Tutorial Contents
As of PHP version 5.3 in Windows, zip extention is already built-in. In order to use zip functionalities you need PHP version 5.3 and up. To check your PHP version, just run this code:
````
phpinfo();
````
First, you need to check if ZipArchive class is exist or available.
````
echo class_exists('ZipArchive');
````
If it is not then you'll need to reinstall your php with a latest version.

###### How to open a zip file
First, you need to initialize the ZipArchive class. Then use this syntax to open a zip file.
````
open ( string $filename [, int $flags ] )
````
If the `$filename` doesn't exist, you need to set the `$flag` value to `ZIPARCHIVE::CREATE`. Otherwise `ZIPARCHIVE::OVERWRITE`.
````
<?php

$zip = new ZipArchive();

$zipfile = 'D:\SampleZIP.zip';
$flag = (file_exists($zipfile))? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE;

$zip->open($zipfile, $flag);

?>
````

In this example, the variable flag was set to CREATE if the file doesn't exist. Otherwise, it will set to OVERWRITE.

To check if the file was successfully opened, you just need an if else statement on the returned value of the open function.
````
<?php

$zip = new ZipArchive();

$zipfile = 'D:\SampleZIP.zip';
$flag = (file_exists($zipfile))? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE;

if($zip->open($zipfile, $flag) === true){
	echo "Successfully opened.";
}
else{
	echo "Error";
}

?>
````

Note : If the directory of the zip file is not exist, then it will throw an error. So you should create first a valid directory before you open or add a zip file.

###### How to add files in a zip
So after you can open a zip file, you can already add multiple files in it. 
This is the syntax of adding the file:
````
addFile ( string $filename [, string $localname = NULL [, int $start = 0 [, int $length = 0 ]]] )
````
Example:
````
$zip->addFile('D:\SampleText.txt');
````

If you will not set the local name, then the default name and directory of the file will be the first parameter. And if the file doesn't exist, nothing will happen and it will return a false value.
````
<?php

$zip = new ZipArchive();

$zipfile = 'D:\SampleZIP.zip';
$flag = (file_exists($zipfile))? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE;

if($zip->open($zipfile, $flag) === true){

	$zip->addFile('D:\SampleText.txt', 'SampleText.txt');
	$zip->close();
}
else{
	echo "Error";
}

?>
````

You can add multiple files just by adding the `addFile` function multiple times.
````
$zip->addFile('D:\SampleText.txt', 'SampleText.txt');
$zip->addFile('D:\SampleText2.txt', 'SampleText2.txt');
$zip->addFile('D:\SampleText3.txt', 'SampleText3.txt');
````

Note that it will add only those files that are exist. To ensure that your file will be added, you can check first if the file exist using the `file_exists` function before you will add the file.

To add a file with an input string, you can use the `addFromString` function:
````
addFromString ( string $localname , string $contents )
````

Example:
````
<?php

$zip = new ZipArchive();

$zipfile = 'D:\SampleZIP.zip';
$flag = (file_exists($zipfile))? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE;

if($zip->open($zipfile, $flag) === true){

	$zip->addFromString('sample.txt', 'This is a sample text.');
	$zip->close();
}
else{
	echo "Error";
}

?>
````

Lastly, if your zip file is already exist and has an existing files. The ZipArchive class will remove all of it  and add the new files.

#### What's next?
It will be more about PHP Classes and functions.
    

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@gentlemanoi/php-how-to-use-ziparchive-class">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍 , , , , , , , , , , ,