file zip problem
Posted: Tue Oct 06, 2009 2:05 am
I found this code on the net and I use it to zip a text file. The problem is when I try to extract the file is says an error "unexpected end of file". I tried to zip the text file manually and extract it afterwards and it works just fine. Any idea what seems to be the problem? Thanks
Code: Select all
echo"<p>Zip my Files</p>";
$files=array('fileA.txt','fileB.txt','fileC.txt');
if(create_zip($files, 'MYZIP.zip'))
echo "Files Zipped Successfully";
function create_zip($files = array(),$destination = '',$overwrite = false)
{
if(file_exists($destination) && !$overwrite)
{
echo "Destination already exists";
return false;
}
$valid_files = array();
if(is_array($files))
{
foreach($files as $file)
{
if(file_exists($file))
{
$valid_files[] = $file;
}
}
}
if(count($valid_files))
{
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true)
{
echo "Can't zip files";
return false;
}
foreach($valid_files as $file)
{
$zip->addFile($file,$file);
}
echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status ,'<br>';
$zip->close();
return file_exists($destination);
}
else
{
echo "No file found to zip";
return false;
}
}