PHP : How to use ZipArchive class

in #utopian-io8 years ago (edited)

codeigniter-logo.png

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.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

You should add your example codes in code blocks instead of screenshots. Readers will have hard time using them. After this, I can approve your contribution.

Your contribution cannot be approved because it does not follow the Utopian Rules.

You can contact us on Discord.
[utopian-moderator]

Done updating screenshots to codeblocks. Thanks.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @gentlemanoi I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.084
BTC 63900.89
ETH 1725.75
USDT 1.00
SBD 0.42