How To Use PHP Zip Functions Tutorial Example

PHP 8 , PHP

Nicesnippets

388

10-07-2021


How To Use PHP Zip Functions Tutorial Example

Hi guys,

Today i will explained PHP Zip Functions. This example is so easy to use in php.

PHP Zip Functions is provided to the php default functions. The php zip function is use to zip open,close,read,write,etc to use in zip functions.

So let's start to the example.

zip_open() & zip_close()


The zip_close() function closes a ZIP file and opened by the zip_open() function.

<?php

$zip = zip_open("test.zip");

zip_read($zip);

zip_close($zip);

?>

zip_entry_open() & zip_entry_close()

zip_entry_close() function is closes a ZIP directory and opened by zip_entry_open().

<?php

$zip = zip_open("test.zip");

if ($zip) {

while ($zip_entry = zip_read($zip)) {

if (zip_entry_open($zip, $zip_entry)) {

zip_entry_close($zip_entry);

}

}

zip_close($zip);

}

?>

zip_read()

zip_read() function is reads the next file in a open ZIP file archive.

<?php

$zip = zip_open("test.zip");

zip_read($zip);

zip_close($zip);

?>

zip_entry_compressedsize()

zip_entry_compressedsize() function is a compressed to the zip file size to return.

<?php

$zip = zip_open("test.zip");

if ($zip) {

while ($zip_entry = zip_read($zip)) {

echo "<p>";

echo "Name: " . zip_entry_name($zip_entry) . "<br>";

echo "Compressed size: " . zip_entry_compressedsize($zip_entry);

echo "</p>";

}

zip_close($zip);

}

?>

OutPut

Name: ziptest.txt

Compressed size: 56

Name: htmlziptest.html

Compressed size: 101

zip_entry_compressionmethod()

The zip_entry_compressionmethod() function returns the compression method of a ZIP directory entry.

<?php

$zip = zip_open("test.zip");

if ($zip) {

while ($zip_entry = zip_read($zip)) {

echo "<p>";

echo "Name: " . zip_entry_name($zip_entry) . "<br>";

echo "Compression method: " . zip_entry_compressionmethod($zip_entry);

echo "</p>";

}

zip_close($zip);

}

?>

OutPut

Name: ziptest.txt

Compression method: deflated

Name: htmlziptest.html

Compression method: deflated

zip_entry_filesize()

The zip_entry_compressionmethod() function returns the compression method of a ZIP directory entry.

<?php

$zip = zip_open("test.zip");

if ($zip) {

while ($zip_entry = zip_read($zip)) {

echo "<p>";

echo "Name: " . zip_entry_name($zip_entry) . "<br>";

echo "Filesize: " . zip_entry_filesize($zip_entry);

echo "</p>";

}

zip_close($zip);

}

?>

OutPut

Name: ziptest.txt

Filesize: 59

Name: htmlziptest.html

Filesize: 124

zip_entry_name()

This zip_entry_name() function is returns to the name of a ZIP directory entry.

<?php

$zip = zip_open("test.zip");

if ($zip) {

while ($zip_entry = zip_read($zip)) {

echo "Name: " . zip_entry_name($zip_entry) . "<br>";

}

zip_close($zip);

}

?>

OutPut

Name: ziptest.txt

Name: htmlziptest.html

zip_entry_read()

zip_entry_read() function reads from an open directory entry.

<?php

$zip = zip_open("test.zip");

if ($zip) {

while ($zip_entry = zip_read($zip)) {

echo "<p>Name: " . zip_entry_name($zip_entry) . "<br>";

if (zip_entry_open($zip, $zip_entry)) {

echo "File Contents:<br>";

$contents = zip_entry_read($zip_entry);

echo "$contents<br>";

zip_entry_close($zip_entry);

}

echo "</p>";

}

zip_close($zip);

}

?>

OutPut

Name: ziptest.txt

File Contents:

Hello World! This is a test.

Name: htmlziptest.html

File Contents:

Hello World!

This is a test for the zip functions in PHP.

Now you can check your own.

I hope it can help you...


Recommended Posts