Creating/Reading/Extracting Archives

Comments and questions that doest not fit anywhere else.
Locked
Vince

Creating/Reading/Extracting Archives

Post by Vince » Wed Jul 14, 2004 3:24 am

Hey,

Does anyone know where I can find references on how to create/extract/read RAR, ZIP and ACE files (so I can make a program in PHP to perform these actions)?

I'm in the middle of making a archive Extractor/Creator/Reader program in PHP-GTK and at the moment i'm using the command line to perform these actions but i'd like to eventually write some internal PHP functions to do it.

Also if anyone has any tips, hints or tricks on how this could be done that would be great aswell.

-Vince

P.S. the new getID3 seems to work perfectly so far.

James Heinrich
getID3() v1 developer
Posts: 1476
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Post by James Heinrich » Wed Jul 14, 2004 4:36 pm

getID3() can be used as the basis for reading ZIP files. I don't have support for RAR/ACE yet.

This code will get you the decoded contents of a ZIP file:

Code: Select all

$filename = 'file.zip';

if (@include_once('getid3.lib.php')) {
	if (@include_once('module.archive.zip.php')) {
		if ($fp_zipfile = @fopen($filename, 'rb')) {

			$ThisFileInfo = array('fileformat'=>'', 'error'=>array(), 'filesize'=>filesize($filename));
			$getid3_zip = new getid3_zip($fp_zipfile, $ThisFileInfo);

			if (($ThisFileInfo['fileformat'] == 'zip') && !empty($ThisFileInfo['zip']['files'])) {
				if (!empty($ThisFileInfo['zip']['central_directory'])) {
					$ZipDirectoryToWalk = $ThisFileInfo['zip']['central_directory'];
				} elseif (!empty($ThisFileInfo['zip']['entries'])) {
					$ZipDirectoryToWalk = $ThisFileInfo['zip']['entries'];
				} else {
					fclose($fp_zipfile);
					echo 'failed to parse ZIP file';
				}
				if (!empty($ZipDirectoryToWalk)) {
					echo 'zip file "'.$filename.'" contains:'."\n";
					foreach ($ZipDirectoryToWalk as $key => $valuearray) {
						fseek($fp_zipfile, $valuearray['entry_offset'], SEEK_SET);
						$LocalFileHeader = $getid3_zip->ZIPparseLocalFileHeader($fp_zipfile);

						if ($LocalFileHeader['flags']['encrypted']) {
							echo '* [password-protected] <b>'.$valuearray['filename'].'</b>'."\n";
						} else {
							fseek($fp_zipfile, $LocalFileHeader['data_offset'], SEEK_SET);
							$compressedFileData = fread($fp_zipfile, $LocalFileHeader['compressed_size']);
							switch ($LocalFileHeader['raw']['compression_method']) {
								case 0:
									// store - great, do nothing at all
									$uncompressedFileData = $compressedFileData;
									break;

								case 8:
									$uncompressedFileData = gzinflate($compressedFileData);
									break;

								default:
									echo 'unknown ZIP compression method ('.$LocalFileHeader['raw']['compression_method'].') on compressed file "'.$valuearray['filename'].'"';
									continue 2;
							}
							unset($compressedFileData);

							// do something with decompressed data

							unset($uncompressedFileData);
						}
					}
				}
			} else {
				echo 'failed to parse ZIP file';
			}
			fclose($fp_zipfile);

		} else {
			echo 'failed to make tmpfile()';
		}
	} else {
		echo 'failed to include module.archive.zip.php';
	}
} else {
	echo 'failed to include getid3.lib.php';
}
Last edited by James Heinrich on Mon Dec 18, 2006 12:41 pm, edited 2 times in total.

Vince

Post by Vince » Thu Jul 15, 2004 10:43 am

thanks James. If I find out how to read/write RAR and ACE files i'll pass it over to you.

-Vince

mjs
User
Posts: 9
Joined: Thu Nov 30, 2006 8:19 am

Post by mjs » Mon Dec 18, 2006 12:20 pm

I'm trying to use getID3 to read the contents of a zip file, but I can't seem to get it to work. My server is running PHP 4. Is this the problem? I know PHP 5 can read and write zip files on its own and PHP 4 cannot. From what I understand, getID3 simply reads the metadata of the file, so it shouldn't have to open the zip file (like PHP 5) to read its contents. Correct?

I can make getID3 return the amount of files that are in the zip file, but again, I cannot get it to display the contents there in. The most I can do is have it give me an empty array() of the content list, but if I try to pull the values out of the array it still gives me nothing.

Also, in the above script, the line towards the bottom that says...

Code: Select all

 echo 'unknown ZIP compression method ('.$LocalFileHeader['raw']['compression_method'].') on compressed file "'.$valuearray['filename'].'"');
...I get a parsing error. Taking out the last ) solves it.

However, the only output that displays is: zip file "test.zip" contains:

Nothing returns after that.

???

mjs
User
Posts: 9
Joined: Thu Nov 30, 2006 8:19 am

Post by mjs » Tue Dec 19, 2006 9:24 am

As it turns out, I guess PHP 4 (well, 4.1.0) can in fact read zip contents. I guess writing zip files is a PHP 5 thing. Anyway, it looks like this code will output the zip file's contents.

Code: Select all

<ol>
<?php

$zip = zip_open('test.zip');

if ($zip) {
	while ($zip_entry = zip_read($zip)) {
		echo '<li>' . zip_entry_name($zip_entry) . '</li>';
	}
	zip_close($zip);
}

?>
</ol>
Simple enough. Thanks.

arowana
User
Posts: 1
Joined: Tue May 29, 2012 2:43 am
Are you a spambot?: no

Re: Creating/Reading/Extracting Archives

Post by arowana » Tue May 29, 2012 2:48 am

Hi there, newbie here and just saw this post which is quite helpful~Thanks!

Locked