Automatic Zip Extraction

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
WanamakerStudios
Forum Commoner
Posts: 65
Joined: Thu Nov 30, 2006 7:35 am

Automatic Zip Extraction

Post by WanamakerStudios »

Does any have or has anyone ever seen a program for automatic zip extraction? We get a feed as a .Zip file every night and I am looking for a way to unzip it on the server, process the text files into the DB and update our sites ... but I haven't been able to find a script that does Zip extraction ...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Look in Code Snippets.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Here is something that will unzip a zip file. It will create directories and everything. I create this as a package installer for a future release of AATraders.

Just change $package_name to your zip file name IE: $package_name = "incoming.zip"

And change the $package_title to anything you want or leave it blank.

Code: Select all

<?php
$debug = false;
$package_name = "aatrade_package";
$package_title = "Alien Assault Traders";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><?php echo $package_title; ?> Package Installer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
var oldheight = 0;
var intStop = .1*60*1000;
var timeStart = new Date();

function AddRow()
{
	var objDiv = document.getElementById("installprogress");
	var d = new Date();

	objDiv.scrollTop = objDiv.scrollHeight;

	if(oldheight == objDiv.scrollHeight)
	{
		if(d - timeStart >= intStop)
		{
			window.clearInterval(timer);
		}
	}
	else
	{
		timeStart = new Date();
		oldheight = objDiv.scrollHeight;
	}
}

var timer = window.setInterval("AddRow()", 1);
</script>
</head>

<body bgcolor="#000000" text="#ffffff">

<table width="100%" border=0 cellspacing=0 cellpadding=0>
	<tr>		  
		<td>
			<table width="772" border="0" cellpadding="0" cellspacing="0" align="center">
				<tr>
					<td width="772" height="30" bgcolor="blue">
						<div align="center" style="font-weight:bold; font-size:18px; font-family:Verdana,Geneva,sans-serif;"><?php echo $package_title; ?> Package Installer</div>
					</td>
				</tr>
			</table>
			<table width="772" border="0" align="center" cellpadding="0" cellspacing="0">
				<tr>
					<td bgcolor="blue">
						<table cellspacing ="0" cellpadding ="0" border ="1" width ="667" align="center" bgcolor="#000000">
							<tr align="center">
								<td align="center">
									<table width="100%" border="0" cellspacing="0" cellpadding="3" align="center">
										<tr>
											<td>
												<div id="installprogress" align="center" style="width: 600px; height:300px; overflow:auto; background:#000000; color:white; font-weight:bold; font-size:10px; font-family:Verdana,Geneva,sans-serif;">
<?
flush();
if (function_exists('zip_open'))
{
	if(@mkdir("test"))
	{
		rmdir("test");
		$zip = @zip_open($package_name);

		if ($zip)
		{
			while ($zip_entry = zip_read($zip)) {
				$filename = zip_entry_name($zip_entry);
				$entry = zip_entry_open($zip, $zip_entry, "r");
				$filesize = zip_entry_filesize($zip_entry);

				if($debug)
				{
					echo "Filename:           " . $filename . "<br>\n";
				}
				$target_dir = substr($filename,0,strrpos($filename,'/'));
				if(!empty($target_dir) && $debug)
				{
					echo "Target Dir:              " . $target_dir . "<br>\n";
				}

				$dir_name = dirname(zip_entry_name($zip_entry));
				if ($dir_name != ".")
				{
					$dir_op = "";
					$tabs = "";
					foreach ( explode("/",$dir_name) as $k)
					{
						$dir_op = $dir_op . $k;
						if (is_file($dir_op))
						{
							unlink($dir_op);
						}
						if (!is_dir($dir_op))
						{
							$tabs .= "&nbsp;&nbsp;&nbsp;&nbsp;";
							echo $tabs . "Creating Directory: $dir_op <br>\n";
							mkdir($dir_op);
						}
						$dir_op = $dir_op . "/" ;
					}
				}

				if (is_dir($target_dir) || empty($target_dir))
				{

					if($debug)
					{
						echo "Actual Filesize:    " . $filesize . "<br>\n";
						echo "Compressed Size:    " . zip_entry_compressedsize($zip_entry) . "<br>\n";
						echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "<br>\n";
					}
					if ($filesize > 0)
					{
						$contents = zip_entry_read($zip_entry, $filesize);
						echo "Saving:             " . $filename . "<br>\n";
						$fp=fopen($filename,"w");
						fwrite($fp,$contents);
						fclose($fp);
						echo "<hr>\n";
					}
					else
					{
						if(!is_dir($filename))
						{
							echo "Saving:             " . $filename . "<br>\n";
							$fp=fopen($filename,"w");
							fclose($fp);
							echo "<hr>\n";
						}
					}
				}
				flush();
			}
			zip_close($zip);
		}
		else
		{
			echo "Install Failed: couldn't find/open aatrade_package<br>\n";
		}
	}
	else
	{
		echo "Install Failed: change directory permissions or ownership to allow creation of directories and saving files.<br>\n";
	}
}
else
{
	echo "Install Failed: PHP has not been compiled to enable ZIP functions<br>\n";
}
?>
												</div>
											</td>
										</tr>
									</table>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
			<table width="772" border="0" align="center" cellpadding="0" cellspacing="0">
				<tr>
					<td bgcolor="blue" height="30">
						<div align="center" style="font-weight:bold; font-size:18px; font-family:Verdana,Geneva,sans-serif;">Package Installation Complete</div>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>

</body>
</html>
You can remove all of the html if you just need the php code. :) The javascript will cause the window to scroll the data as it comes from the server if you are using Opera or Firefox.
WanamakerStudios
Forum Commoner
Posts: 65
Joined: Thu Nov 30, 2006 7:35 am

Post by WanamakerStudios »

I'll give this a shot ... thanks!
WanamakerStudios
Forum Commoner
Posts: 65
Joined: Thu Nov 30, 2006 7:35 am

Post by WanamakerStudios »

I dumped your code into a file called zip.php and changed the zip name variable to my zips name. The zip.php and zip file were in the same folder. When I went to run it ... the program said the zip file couldnt be found. Any ideas?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Going off away from what has been posted so far, PECL has a zip extension for PHP Zip File Functions
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Remove the @ symbol from the front of the zip_open to see if another error is occuring.
WanamakerStudios
Forum Commoner
Posts: 65
Joined: Thu Nov 30, 2006 7:35 am

Post by WanamakerStudios »

I did ... no luck.
the DtTvB
Forum Newbie
Posts: 11
Joined: Sun Feb 11, 2007 6:10 am

Post by the DtTvB »

I would use:

Code: Select all

exec('unzip ' . escapeshellcmd('filename.zip'));
Should work on most Ubuntu servers, and some other servers.
WanamakerStudios
Forum Commoner
Posts: 65
Joined: Thu Nov 30, 2006 7:35 am

Post by WanamakerStudios »

the DtTvB wrote:I would use:

Code: Select all

exec('unzip ' . escapeshellcmd('filename.zip'));
Should work on most Ubuntu servers, and some other servers.
This worked perfectly, but it generated an error_log file that contained the following:
[16-Mar-2007 07:52:05] PHP Warning: Zend Optimizer does not support this version of PHP - please upgrade to the latest version of Zend Optimizer in Unknown on line 0

Any ideas on how to fix this?
Post Reply