Jump to content

PHP Unzip


RobertP

Recommended Posts

I am create a function the will unzip a simple archive. My function works, but if the file is larger them 1KB, then it will 'cut' the file to 1KB.

 

Here is my function.

 

define('_DS',DIRECTORY_SEPARATOR);

function unzip($file,$dir = null){
if(!is_readable($file))
	return 'File \''.$file.'\' not found.';
if(!$dir)
	$dir = dirname($file);
if(!is_dir($dir))
	mkdir($dir,0755,true);
$resource = zip_open($file);
if(!is_resource($resource))
	return 'File \''.$file.'\' is corrupt.';
while(($entry = zip_read($resource))!==false){
	$entry_file = $dir._DS.zip_entry_name($entry);
	if($entry_size = zip_entry_filesize($entry)==0){
		mkdir($entry_file);
		continue;
	}
	elseif(!is_dir($entry_dir = dirname($file)))
		mkdir($entry_dir,0755,true);
	file_put_contents($entry_file,zip_entry_read($entry,$entry_size));
}
zip_close($resource);
return true;
}

Link to comment
Share on other sites

ok, i have it working correctly, here it is :D

 

function unzip($file,$dir = null){
if(!is_readable($file))
	return 'File \''.$file.'\' not found.';
if(!$dir)
	$dir = dirname($file);
if(!is_dir($dir))
	mkdir($dir,0755,true);
$resource = zip_open($file);
if(!is_resource($resource))
	return 'File \''.$file.'\' is corrupt.';
while($entry = zip_read($resource)){
	$size = zip_entry_filesize($entry);
	$name = zip_entry_name($entry);
	$unzipped = fopen($dir._DS.$name,'wb');
	while($size>0){
		$chunkSize = ($size>102400)?10240:$size;
		$size -= $chunkSize;
		$chunk = zip_entry_read($entry,$chunkSize);
		if($chunk!==false)
			fwrite($unzipped,$chunk);
	}
	fclose($unzipped);
}
return true;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.