phpZIP PRO Manual
This test file will show you how to use SS_ZIP class to work with ZIP archives.
Table of contents:
Requirements
Actually the only thing you need is zlib, which comes with modern PHP versions by default.
So lets begin:
test.php:
<?PHP
set_time_limit(3000); //for big archives
require("../ss_zip.class.php");
Creating new archive:
// new empty archive with compression level 6
$zip= new ss_zip('',6);
Adding files
Now lets add some file from disc and store into archive preserving its original name and path:
//add file from disc and store to the archive under its own name and path
$zip->add_file('testdata/folder1/file1.html');
Lets add some file and store it into the archive under other name and path:
//add file from disc and store to the archive under our/path/ourfile2.html
$zip->add_file('testdata/folder1/file1.html','our/path/ourfile2.html');
Adding data from variables
Now lets take some data generated by PHP (from a variable) and store it into the archive as a file, with name and path:
//add file generated by the script and store it as testphp/thefile.html
$myfiledata="This content was generated by PHP script and stored to ZIP as a file";
$zip->add_data('testphp/thefile.html', $myfiledata);
Saving the archive
Lets save the archive to the disk:
//Saving the archive to server under name 'myzip1.zip'
$zip->save('myzip1.zip');
Here it is: myzip1.zip
Opening existing ZIP archive
Lets open the existing archive (for example this one just saved):
//open an existing archive
$zip->open('myzip1.zip');
Appending another ZIP archive
Append another archive (append.zip):
//append another archive
$zip->open('append.zip', true);
Reading the entries
Read what is in the combined archive:
//read what is in the zip
while($entry=$zip->read()){
echo "Read Entry: <br>";
echo 'entry index $entry["idx"]=>'.$entry['idx']."<br>";
echo 'entry path $entry["name"]=>'.$entry['name']."<br>";
echo 'entry integer file attribute $entry["attr"]=>'.$entry['attr']."<br>";
echo 'entry string file attribute $entry["attrstr"]=>'.$entry['attrstr']."<br><br>";
}
Results:
Read Entry:
entry index $entry["idx"]=>0
entry path $entry["name"]=>testdata/folder1/file1.html
entry integer file attribute $entry["attr"]=>32
entry string file attribute $entry["attrstr"]=>file
Read Entry:
entry index $entry["idx"]=>1
entry path $entry["name"]=>our/path/ourfile2.html
entry integer file attribute $entry["attr"]=>32
entry string file attribute $entry["attrstr"]=>file
Read Entry:
entry index $entry["idx"]=>2
entry path $entry["name"]=>testphp/thefile.html
entry integer file attribute $entry["attr"]=>32
entry string file attribute $entry["attrstr"]=>file
Read Entry:
entry index $entry["idx"]=>3
entry path $entry["name"]=>append/
entry integer file attribute $entry["attr"]=>48
entry string file attribute $entry["attrstr"]=>dir
Read Entry:
entry index $entry["idx"]=>4
entry path $entry["name"]=>append/appfolder1/
entry integer file attribute $entry["attr"]=>48
entry string file attribute $entry["attrstr"]=>dir
Read Entry:
entry index $entry["idx"]=>5
entry path $entry["name"]=>append/appfolder1/appendedfile.html
entry integer file attribute $entry["attr"]=>32
entry string file attribute $entry["attrstr"]=>file
And save the combined archive as "joined.zip":
//save new archive as joined.zip
$zip->save('joined.zip');
Here it is: joined.zip
Extracting data to the variable
//extracting data to variable
$mydata = $zip->extract_data(1);
echo "This is data extracted from archive, from entry with index 1:<xmp>";
print_r($mydata);
echo "</xmp>";
Results:
This is data extracted from archive, from entry with index 1:Array
(
[file] => our/path/ourfile2.html
[data] => This is test file1
[size] => 18
[error] => 0
)
Extracting data to disc
Lets extract some files into 'unpackfolder':
//extracting data to disc
$mydata=$zip->extract_file(1,'unpackfolder');
echo "This is data extracted from archive, from entry with index 1:<xmp>";
print_r($mydata);
echo "</xmp> and saved here:<br>";
$path='unpackfolder/'.$mydata['file'];
echo " <a href='$path'target='_blank'>$path</a> <br>";
Results:
This is data extracted from archive, from entry with index 1:Array
(
[file] => our/path/ourfile2.html
[data] => This is test file1
[size] => 18
[error] => 0
)
and saved here:
unpackfolder/our/path/ourfile2.html
Removing entries
Now knowing that entry with ID:1 is file "our/path/ourfile2.html" lets remove it and save the archive as 'rem.zip':
//removing entries
$zip->remove(1);
$zip->save('rem.zip');
Here it is: rem.zip