Tag Archive for php

How to read the rar content in php?

To read the rar content in php, you should have your rar extension enabled first. The extension is located in /php/ext directory. To enable it add this line to php.ini file.
extension = php_rar.dll

If you don’t have a .dll file then it could be downloaded from here php_rar.dll

function read_rar_file($rarfile) {
$rar_file = rar_open($rarfile) or die("Can't open Rar archive");
$entries = rar_list($rar_file); // each entry of a rar

foreach ($entries as $entry) {
	$stream = $entry->getStream(); 
	
	$file = '';
	while (!feof($stream)) {
		$buff = fread($stream, 8192);
		if ($buff !== false) {
			$file .= $buff; // file content for a rar file
		}else {
			break;
		}
	}
        $each_file_content[$entry->getName()] = $file;	
}
fclose($stream);
rar_close($rar_file);
}

How to read the zip content in php?

To read the zip content in php, you should have your zip extension enabled first. The extension is located in /php/ext directory. To enable it add this line to php.ini file.
extension = php_zip.dll

Once you have your extension enabled, you could use the php built in function to open and read the zip content. Here is the small example to read multiple files contained in the zip.

 function read_zip_file($zipfile) {
 $zip = zip_open($zipfile);

 if(is_resource($zip)) {
	while(($zip_entry = zip_read($zip))) {
		$filename = basename(zip_entry_name($zip_entry));
		$entry_content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

		$each_file_content[$filename] = $entry_content;
	}
 }else {
	echo 'Invalid Zip Format';
 }
}
www.000webhost.com