Hi ,
Ever thought of writing your own php code for compressing and extracting your files
Ever thought of writing your own php code for compressing and extracting your files
??
Then your in the right place , by following this post you can zip and unzip your files using a PHP code
. You can also perform other operations like renaming the files to be zipped , getting the index of the file to be zipped , extracting the zipped files to the folder you wish , getting the number of files zipped and the name of the files being zipped etc :-)
Excited ? well here it goes !!!!!!!!!!
Create and Extract zip files using your own PHP program
<?php
$zip = new ZipArchive(); /*creating a variable of the zipArchive class*/
$filename = "./wetechies.zip"; /*name of the zip folder you want to create*/
![]() |
Compress your files using PHP |
/***************Creates the zip folder if it doesn't exists , else it overwrites ***************/
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
/****************** Lets add 2 files into the zip for demonstration********************/
$zip->addFromString("wetechies1.txt" . time(), "#1 welcome to wetechies blog.\n");
$zip->addFromString("wetechies2.txt" . time(), "#2 enjoy reading the blog.\n");
/***********************Just to print some archive details*****************************/
$zip = new ZipArchive();$zip->open('wetechies.zip');
print_r($zip);
var_dump($zip);
echo "Number of files : " . $zip->numFiles . "\n";
echo "Execution status : " . $zip->status . "\n";
echo "statusSys : " . $zip->statusSys . "\n";
echo "filename : " . $zip->filename . "\n";
echo "comment : " . $zip->comment . "\n";
for ($i=0; $i<$zip->numFiles;$i++) {
echo "index : $i\n";
print_r($zip->statIndex($i));
}
/**************************To print some more additional information**************/
$zip = zip_open("wetechies.zip");
if ($zip) {
while ($zip_entry = zip_read($zip)) {
echo "Name of the zip file : " . zip_entry_name($zip_entry) . "\n";
echo "Actual Filesize : " . zip_entry_filesize($zip_entry) . "\n";
echo "Compressed Size : " . zip_entry_compressedsize($zip_entry) . "\n";
echo "Compression Method used : " . zip_entry_compressionmethod($zip_entry) . "\n";
if (zip_entry_open($zip, $zip_entry, "r")) {
echo "The File Contents are :\n";
$buffer = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
echo "$buffer\n";
zip_entry_close($zip_entry);
}
echo "\n";
}
zip_close($zip);
}
/***************Renaming and extracting the file to the folder you wish******************/
echo $zip->locateName('send.php') . "\n";
$zip->renameIndex(1,'newwetechies.txt');
$zip->extractTo("wetechiesextracted");
$zip->extractTo("wetechiesextracted");
$zip->close();
?>
/****************************************************************************************************/
You can download the source code from here
Any doubts? or suggestion please feel free to comment :-)
Good day. I was impressed with your article. Keep it up . You can also visit my site if you have time. Thank you and Bless you always.
ReplyDeletePHP Mysql Developer in India