Jump to content

Zip File Manipulation


chronister

Recommended Posts

Hello,

 

I am playing with manipulating zip files server-side. I have a very simple function that takes 2 parameters, the path of the zip file and the destination.

 

It works great, but I am having trouble figuring out how to the the names / paths of all the files in the zip file. I think I am going to create a photo gallery that is mysql based and I want the ability to upload zip files, grab the names of the files and insert them into the db.

 

The script below is simply a open zip file and extract them to a dir. There is almost no validation in there (coming soon), and there is no error handling.

 

<?php

function unzip($file, $dest)
{
$zip = new ZipArchive;
if ($zip->open($file) === TRUE)
{
	if(!is_dir($dest))
	{
		mkdir($dest);
	}
	$zip->extractTo($dest);
	$zip->close();
}
else 
        {
	echo 'Could not open zip file';
}
}



$file = $_SERVER['DOCUMENT_ROOT'].'/etc/temp/test.zip';
$dest = $_SERVER['DOCUMENT_ROOT'].'/test_dir';

$inflateit =  unzip($file, $dest);

?>

 

Any advice on how to get the filenames, sub-dir names and such?

 

Thanks,

 

nate

 

Link to comment
Share on other sites

The actual zip functions allow you to read the contents of the zip file.

<?php
$zip = zip_open($zipfile);
while ($zip_entry = zip_read($zip))
{
zip_entry_open($zip, $zip_entry);

$name = zip_entry_name($zip_entry);

$handle = fopen($name, "w");
fwrite($handle, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)), zip_entry_filesize($zip_entry));
fclose($handle);

// use $name to insert in to database.

zip_entry_close($zip_entry);
}
zip_close($zip);
?>

 

Presuming you aren't after uploading a zip file with directories and you can add a directory before the fopen directory.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.