Jump to content

Zip Extraction


Stewart

Recommended Posts

I'm trying to get hold of some script to extract .zip files and possibly one or 2 other compressed filetypes like Gzip. The problem is it needs to be able to run in environments where the Zip library isn't available. I've been having a look around, but all i could find were scripts using the library.Can anyone help me out here please?

Link to comment
Share on other sites

I use this:http://www.phpconcept.net/pclzip/index.en.phpDownload the class definition file (pclzip.lib.php), include that file in your PHP script, and you can work with zip files. Here is a little chunk of code that I use for a gallery when someone uploads a zip file full of images. This will extract the zip file to a temporary directory (zip_temp), automatically chmod each file that gets extracted, and extract all files that are nested in folders or subfolders of the zip archive. And then it adds each image to the database.

if ($errStr == "" && isset($filename))  {	  $zipfile = new PclZip($filename);	  $zipfile->extract(PCLZIP_OPT_PATH, "zip_temp/", PCLZIP_OPT_SET_CHMOD, 0777, PCLZIP_OPT_REMOVE_ALL_PATH);	$path = $temp_save . "zip_temp";	  $dir_handle = opendir($path);	  $path .= "/";	$galID[0] = 0;	if (mysql_query("INSERT INTO `galleries` (`name`, `desc`) VALUES ('" . mysql_escape_string($name) . "', '" . mysql_escape_string($desc) . "');"))	  $galID[0] = mysql_insert_id();	else	  echo mysql_error();	while ($galID[0] != 0 && (false !== ($file = readdir($dir_handle))))	{		$valid = false;	  if ($file != "." && $file != ".." && !is_dir($path . $file))	  {		$components = explode(".", $file);		switch (strtolower($components[count($components) - 1]))		{		  case 'gif':		  case 'jpg':		  case 'jpeg':		  case 'wbmp':		  case 'png':			$valid = true;			break;		}	  }	  if ($valid)	  {		$data = fread(fopen($path . $file, "rb"), filesize($path . $file));		add_image($path . $file, $data, $galID, $file);		$nr++;	  }	  if ($file != "." && $file != "..")	  {		  chmod($path . $file, 0777);		  if (is_dir($path . $file))		  rmdir($path . $file);		else		  unlink($path . $file);	  }	}	closedir($dir_handle);	$i = 1;	$result = mysql_query("SELECT `imgID` FROM `in_gallery` WHERE `galID`={$galID[0]};");	while ($row = mysql_fetch_assoc($result))	{		mysql_query("UPDATE `gallery_images` SET `desc`='$i of $nr' where `imgID`={$row['imgID']};");	  $i++;	}  }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...