Page 1 of 1

Automatic Zip Extraction

Posted: Tue Feb 13, 2007 5:18 pm
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 ...

Posted: Tue Feb 13, 2007 5:40 pm
by feyd
Look in Code Snippets.

Posted: Tue Feb 13, 2007 10:10 pm
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.

Posted: Tue Feb 13, 2007 10:16 pm
by WanamakerStudios
I'll give this a shot ... thanks!

Posted: Wed Feb 21, 2007 6:08 am
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?

Posted: Wed Feb 21, 2007 7:27 am
by CoderGoblin
Going off away from what has been posted so far, PECL has a zip extension for PHP Zip File Functions

Posted: Wed Feb 21, 2007 1:04 pm
by AKA Panama Jack
Remove the @ symbol from the front of the zip_open to see if another error is occuring.

Posted: Wed Feb 21, 2007 3:09 pm
by WanamakerStudios
I did ... no luck.

Posted: Wed Feb 21, 2007 5:33 pm
by the DtTvB
I would use:

Code: Select all

exec('unzip ' . escapeshellcmd('filename.zip'));
Should work on most Ubuntu servers, and some other servers.

Posted: Fri Mar 16, 2007 7:17 am
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?