#native_company# #native_desc#
#native_cta#

Working with Zip Archives in PHP Page 2

By Octavia Andreea Anghel
on April 6, 2010

Getting the Details of an Entry Defined by Its Index

To get the details of a specific file you can use the statIndex method:

mixed ZipArchive::statIndex ( int $index [, int $flags ] ).

The next demo application (statIndex.php) iterates over the file list in archive2.zip and prints all the details for each one of the entries. Here is the code for statIndex.php:

<?php

//Create object
$zip = new ZipArchive();   

//Open the archive2.zip 
if ($zip->open('archive2.zip') !== TRUE) {
    die ("Could not open archive");
}

//Get the number of files in archive2.zip
$numFiles = $zip->numFiles;

//Iterate over the file list
for ($i=0; $i<$numFiles; $i++) {

    //Get the details of an entry defined by its index
    print_r($zip->statIndex($i));
    print "<br />";    
}

// close archive
$zip->close();
?> 

The output of the 5.php listing is:
Array ( [name] => test3.txt [index] => 0 [crc] => -1117485446 [size] => 45 [mtime] => 1269720238 [comp_size] => 46 [comp_method] => 8 )
Array ( [name] => test4.txt [index] => 1 [crc] => -1780851877 [size] => 77 [mtime] => 1269786772 [comp_size] => 62 [comp_method] => 8 )
The next demo application (locate.php) also gets the details of a file, if the archive1.zip contains that file. The locateName method returns the index of the file in the archive and uses the predefined constant ZIPARCHIVE::FL_NODIR, which ignores the directory component. Here is the ZIPARCHIVE::FL_NODIR constant’s prototype:

mixed ZipArchive::locateName ( string $name [, int $flags ] )

Here is the code for locate.php:

<?php
//Create object
$zip = new ZipArchive();   

//Open archive 
if ($zip->open('archive1.zip') === TRUE) {
    
//Find a specify file in archive and print its details
$locate_file = $zip->locateName('test2.txt', ZIPARCHIVE::FL_NODIR);
if ($locate_file == TRUE) {
    $file = $zip->statIndex($locate_file);
    print_r($file);
}

// close archive
$zip->close();
}
?> 

The output of the locate.php listing is:
Array ( [name] => test2.txt [index] => 2 [crc] => -513033757 [size] => 50 [mtime] => 1269715222 [comp_size] => 49 [comp_method] => 8 )

Delete and Rename Files from Zip Archives in PHP

To delete or rename Zip archives in PHP you can use the name or the index. The methods that take care of that are:
  • bool ZipArchive::deleteIndex ( int $index ) — Deletes an entry in the archive using its index
  • bool ZipArchive::deleteName ( string $name ) — Deletes an entry in the archive using its name
  • bool ZipArchive::renameIndex ( int $index , string $newname ) — Renames an entry defined by its index
  • bool ZipArchive::renameName ( string $name , string $newname ) — Renames an entry defined by its name
The next demo application (rename.php) uses all the above methods to rename the file with the “index=3” as “renameByIndex.txt” and the “test4.txt” as “renameByName.txt“. Here is the code for rename.php:

<?PHP
// create object
$zip = new ZipArchive();

// open archive 
if ($zip->open('archive2.zip') !== TRUE) {
    die ("Could not open archive");
}

// rename file in archive using index
$zip->renameIndex(0, 'renamedByIndex.txt') or die("ERROR: Could not rename file");

// rename file in archive using name
$zip->renameName("test3.txt", "renamedByName.txt") or die("ERROR: Could not rename file");

// close and save archive
$zip->close();
echo "The files were successfully renamed in the archive2.zip archive!";
?> 

Here is the output of the rename.php listing and you can also see it in Figure 7 and Figure 8:

The files were successfully renamed in the archive2.zip archive!



Zip Archives in PHP
Click here for larger image


Figure 7. The Initial Content of archive2.zip


Zip Archives in PHP
Click here for larger image


Figure 8. The Contents of archive2.zip after the Files Were Renamed
The next application (delete.php) deletes from archive2.zip the file with the “index=1” and the renameByIndex.txt text file using the deleteIndex() and deleteName() methods. Here is the code for delete.php:

<?PHP
// create object
$zip = new ZipArchive();

// open archive 
if ($zip->open('archive2.zip') !== TRUE) {
    die ("Could not open archive");
}

//Delete the file with the index=1 from the archive2.zip
$zip->deleteIndex(0) or die("ERROR: Could not delete file with the index=1 ");

//Delete the test3.txt file from archive2.zip
$zip->deleteName('renameByIndex.txt') or die("ERROR: Could not delete the test3.txt file ");

echo "The files were successfully deleted from the archive2.zip archive!";

// close and save archive
$zip->close();
?> 

Because the archive contained only two files and both were deleted, the archive2.zip was deleted too.
The next application (add_text_files.php) iterates over the archive files and adds all .txt files from the current directory to text_archive.zip. Here is the code for add_text_files.php:

<?php

//Create the object
$zip = new ZipArchive();

//Open the archive 
if ($zip->open('text_archive.zip', ZIPARCHIVE::CREATE) !== TRUE) {
    die ("Could not open archive");
}

//Adding all .txt files from the current directory to text_archive.zip
foreach (glob ('*.txt') as $textfile) {
    $zip->addFile($textfile);
}

//Close the archive
$zip->close();

echo "text_archive.zip was created successfully!";    
?> 

Here is the output of the add_text_files.php listing:

text_archive.zip was created successfully!



Zip Archives in PHP
Click here for larger image


Figure 9. Grouping All the Text Files from the Current Directory into a New Archive, text_archive.zip

Conclusion

Working with the PHP Zip archive extension is very simple and even fun, as you have seen in all the applications in this article. All you have to do is handle the extension methods and you will successfully manage all the possible Zip archive operations.
Code Download
About the Author
Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book “XML Technologies — XML in Java” (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she’s interested in software architecture, web services, UML, and high-performance unit tests. to e-mail her.