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
https://forums.phpfreaks.com/topic/106201-zip-file-manipulation/
Share on other sites

The Ziparchive class is a built in class of PHP. http://us3.php.net/zip

 

So I cannot edit the class. What you see in my code is the complete code. There is no other code to it. :)

 

So that being said, any other ideas, or modifications EGOnick?

 

Thanks for the help,

 

Nate

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.