User Contributed Notes 20 notes

This is the function I use to unzip a file. It includes the following options:* Unzip in any directory you like* Unzip in the directory of the zip file* Unzip in a directory with the zipfile's name in the directory of the zip file. (i.e.: C:\test.zip will be unzipped in C:\test\)* Overwrite existing files or not* It creates non existing directories with the function Create_dirs($path)

You should use absolute paths with slashes (/) instead of backslashes (\).I tested it with PHP 5.2.0 with php_zip.dll extension loaded

<?php/** * Unzip the source_file in the destination dir * * @param string The path to the ZIP-file. * @param string The path where the zipfile should be unpacked, if false the directory of the zip-file is used * @param boolean Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!) * @param boolean Overwrite existing files (true) or not (false) * * @return boolean Succesful or not */function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true) { if ($zip = zip_open($src_file)) { if ($zip) {$splitter = ($create_zip_name_dir === true) ? "." : "/"; if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";

// Create the directories to the destination dir if they don't already existcreate_dirs($dest_dir);

// For every file in the zip-packetwhile ($zip_entry = zip_read($zip)) {// Now we're going to create the directories in the destination directories

// If the file is not in the root dir$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/"); if ($pos_last_slash !== false) {// Create the directory where the zip-entry should be saved (with a "/" at the end)create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1)); }

// Open the entryif (zip_entry_open($zip,$zip_entry,"r")) {

// The name of the file to save on the disk$file_name = $dest_dir.zip_entry_name($zip_entry);

// Check if the files should be overwritten or notif ($overwrite === true || $overwrite === false && !is_file($file_name)) {// Get the content of the zip entry$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

If (as me) all you wanted to do is store a big string (for example, a serialized array or the like) in a mysql BLOB field, remember that mysql has a COMPRESS() and UNCOMPRESS() pair of functions that do exactly that. Compression/decompression is therefore available also when accessing the DB from other languages like java, etc.

I made a zip stream handler in case your distribution does not have the built in one using the new ZipArchive system. This one also features the ability to grab entries by index as well as by name. It is similar in capabilities to the builtin gzip/bzip2 compression stream handlers (http://us2.php.net/manual/en/wrappers.compression.php) except it does not support writing.

Watch out with archives that contain subfolders if you're using the zip functions to extract archives to actual files. Let's say you're trying to extract foldername/filename.txt from the archive. You can't fopen a directory that doesn't exist, so you'll have to check for the existance of directory foldername and create it if it isn't found, then fopen foldername/filename.txt and begin writing to that.

This is the simplest method to unzip a file. Pass the function your filename and it will unzip it to the current directory of the script with permissions done properly on unix type operating systems. Far simpler to understand and read.

Hi,
if you have the RAW CONTENT OF THE ZIP FILE IN A STRING ONLY and you can't create files on your server (because of the SAFE MODE) to be able to create a file which you can then pass to zip_open(), you'll be having hard times getting the uncompressed content of your ZIP data.
This may help:
I've written simple ZIP decompression function for decompressing the first file from the archive stored in a string (no matter what file it is). It's just about parsing a local file header of the first file, getting raw compressed data of that file and decompression of that data (usually, data in ZIP files are compressed by 'DEFLATE' method, so we'll just decompress it by gzinflate() function then).

function binstrtonum($Str){
//Returns a number represented in a raw binary data passed as string.
//This is useful for example when reading integers from a file,
// when we have the content of the file in a string only.
//Examples:
// chr(0xFF) will result as 255
// chr(0xFF).chr(0xFF).chr(0x00).chr(0x00) will result as 65535
// chr(0xFF).chr(0xFF).chr(0xFF).chr(0x00) will result as 16777215
$Num=0;
for($TC1=strlen($Str)-1;$TC1>=0;$TC1--){ //go from most significant byte
$Num<<=8; //shift to left by one byte (8 bits)
$Num|=ord($Str[$TC1]); //add new byte
}
return $Num;
}
?>

[EDIT BY danbrown AT php DOT net: Contains a bugfix suggested by (bart AT blueberry DOT nl) on 29-JUN-2011 with the following message. "Fix for the endless Iteratore add the flag FilesystemIterator::SKIP_DOTS"]

There are lot of functions given below which etracts files, but what they lack is setting file permissions. On some servers file permissions are very important and the script cease to work after creating first directory, So I have added chmod to the code. There is only one limitation to the code, files without file extension are neither treated as files or directories so they are not chmoded, anyway this does not affect the code. Hope this helps.

If you want to unzip an password protected file with php..try the following command....it works in Unix/Apache environment...I haven't tested in any other environment...

system("`which unzip` -P Password $zipfile -d $des",$ret_val)

Where $zipfile is the path to the .zip to be unzipped and $des is path to the destination directory.....here both absolute and relative path to the script (which contains this system command) will work...

if everything runs well...file should be unzipped at the $des directory..and you will get 0 value for $ret_val , which means success(info-zip.org)