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
https://forums.phpfreaks.com/topic/258333-php-unzip/
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
https://forums.phpfreaks.com/topic/258333-php-unzip/#findComment-1324341
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.