Jump to content

PHP compression nightmare!


Jon N

Recommended Posts

Greetings,

 

I'm prepared to feel like an idiot for asking such a noobish question, but here goes.

 

The www user on my server is unable to exec("zip...") on our Unix server, so I've been faced with utilizing PHP to zip files (it needs to be Zip/PKzip compatible, NOT gzip or bzip2).

 

Server Details

PHP 4.4.2

Registered PHP Streams: php, http, ftp, https, ftps, compress.zlib, compress.bzip2 

 

zip

Zip support  enabled 

 

 

zlib

ZLib Support  enabled 

Compiled Version  1.2.2 

Linked Version  1.2.2 

 

 

Now, when I try to run the following code:

 

$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFile('data.kml');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}

 

I get an error (the second line above is 232):

Parse error: syntax error, unexpected ')', expecting '(' in _______ on line 232

 

Alas, I have tried over and over again to get some sort of zip going on, but to no avail.

 

Is the PHP version being too old causing the syntax error? Do I not have ZIP support like above in this version?

 

Is there a way to write a zip file in 4.4.2? Please help!

 

 

Link to comment
https://forums.phpfreaks.com/topic/114150-php-compression-nightmare/
Share on other sites

It's actually in a file with a bunch of if statements, and I've only called this one specific if statement, alas nothing before or after it is called.

if($test=="y") {
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFile('data.kml');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
}

 

Damn, it must be a problem with the version I have then, right?

 

I can't update php as it's not my server. It's a development server with a lot of important code. Revising such may take ages if we update.

 

I guess I'm most curious as to whether or not there is a workaround for compressing files in 4.4.2?

Should I use a class like this instead?

 

$ARCHIVE = new zip;

$ARCHIVE->makeZip('./','./toto.zip'); // make an ZIP archive
var_export($ARCHIVE->infosZip('./toto.zip'), false); // get infos of this ZIP archive (without files content)
var_export($ARCHIVE->infosZip('./toto.zip')); // get infos of this ZIP archive (with files content)
$ARCHIVE->extractZip('./toto.zip', './1/'); //

class zip
{
    public function infosZip ($src, $data=true)
    {
        if (($zip = zip_open(realpath($src))))
        {
            while (($zip_entry = zip_read($zip)))
            {
                $path = zip_entry_name($zip_entry);
                if (zip_entry_open($zip, $zip_entry, "r"))
                {
                    $content[$path] = array (
                        'Ratio' => zip_entry_filesize($zip_entry) ? round(100-zip_entry_compressedsize($zip_entry) / zip_entry_filesize($zip_entry)*100, 1) : false,
                        'Size' => zip_entry_compressedsize($zip_entry),
                        'NormalSize' => zip_entry_filesize($zip_entry));
                    if ($data)
                        $content[$path]['Data'] = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                    zip_entry_close($zip_entry);
                }
                else
                    $content[$path] = false;
            }
            zip_close($zip);
            return $content;
        }
        return false;
    }
    public function extractZip ($src, $dest)
    {
        $zip = new ZipArchive;
        if ($zip->open($src)===true)
        {
            $zip->extractTo($dest);
            $zip->close();
            return true;
        }
        return false;
    }
    public function makeZip ($src, $dest)
    {
        $zip = new ZipArchive;
        $src = is_array($src) ? $src : array($src);
        if ($zip->open($dest, ZipArchive::CREATE) === true)
        {
            foreach ($src as $item)
                if (file_exists($item))
                    $this->addZipItem($zip, realpath(dirname($item)).'/', realpath($item).'/');
            $zip->close();
            return true;
        }
        return false;
    }
    private function addZipItem ($zip, $racine, $dir)
    {
        if (is_dir($dir))
        {
            $zip->addEmptyDir(str_replace($racine, '', $dir));
            $lst = scandir($dir);
                array_shift($lst);
                array_shift($lst);
            foreach ($lst as $item)
                $this->addZipItem($zip, $racine, $dir.$item.(is_dir($dir.$item)?'/':''));
        }
        elseif (is_file($dir))
            $zip->addFile($dir, str_replace($racine, '', $dir));
    }
}

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.