Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ZipArchive::close is terribly slow when executed by Apache2

Status
Not open for further replies.

cmayo

MIS
Apr 23, 2001
159
US
I'm creating a zip archive using PHP's ZipArchive functions. My code performs well enough on small archives but on larger jobs, the time difference between running the PHP script from the shell and running it from a browser is just ridiculous.

The script below, for instance, executes in ~37 seconds from the shell but takes a whopping ~18 minutes when called with a browser. (It may seem nonsensical to be zipping a zip file, but I'll actually be zipping a collection of different file types (which often include zip files) and in this case, the 353MB zip file is the bottleneck so that's what I'm testing with.)

I've tried using PHP's exec() to zip the file using the OS's zip function with similar results. The code was fast in the shell but slowed to a crawl when executed by a browser.

I've found a few references to similar issues online, but none with a resolution. Does anyone have an idea why running this code with a browser results in a 3,000% performance hit?

I'm only using zip to package a collection of files for download and don't really care if the archive is compressed. Tar might be a viable choice, but end users won't know what to do with a tar file, and tgz is also off the table for the same reason. I'm open to another packaging format but off the top of my head, I can't think of another packaging method as ubiquitous as Zip.

Any thoughts?

Code:
<?php
$zip = new ZipArchive;
$f = array('attach/service/2016/02/12200/353MB.zip');

if($zip->open('test.zip',false ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) === true) {

    foreach($f as $file)
    {
        $zip->addFile($file);
    }

    $zip->close();

    echo "ok\n";
} else {
    echo "failed\n";
}
 
Hi

cmayo said:
I'm only using zip to package a collection of files for download and don't really care if the archive is compressed.
Then try [tt]ZipArchive::CM_STORE[/tt] :
PHP:
[teal]<?php[/teal]
[navy]$zip[/navy] [teal]=[/teal] [b]new[/b] ZipArchive[teal];[/teal]
[navy]$f[/navy] [teal]=[/teal] [b]array[/b][teal]([/teal][i][green]'attach/service/2016/02/12200/353MB.zip'[/green][/i][teal]);[/teal]

[b]if[/b][teal]([/teal][navy]$zip[/navy][teal]->[/teal][COLOR=orange]open[/color][teal]([/teal][i][green]'test.zip'[/green][/i][teal],[/teal][b]false[/b] [teal]?[/teal] ZIPARCHIVE[teal]::[/teal]OVERWRITE [teal]:[/teal] ZIPARCHIVE[teal]::[/teal]CREATE[teal]) ===[/teal] [b]true[/b][teal]) {[/teal]

    [b]foreach[/b][teal]([/teal][navy]$f[/navy] [b]as[/b] [navy]$file[/navy][teal])[/teal]
    [teal]{[/teal]
        [navy]$zip[/navy][teal]->[/teal][COLOR=orange]addFile[/color][teal]([/teal][navy]$file[/navy][teal]);[/teal]
        [highlight][navy]$zip[/navy][teal]->[/teal][COLOR=orange]setCompressionName[/color][teal]([/teal][navy]$file[/navy][teal],[/teal] ZipArchive[teal]::[/teal]CM_STORE[teal]);[/teal][/highlight]
    [teal]}[/teal]

    [navy]$zip[/navy][teal]->[/teal][COLOR=orange]close[/color][teal]();[/teal]

    [b]echo[/b] [i][green]"ok\n"[/green][/i][teal];[/teal]
[teal]}[/teal] [b]else[/b] [teal]{[/teal]
    [b]echo[/b] [i][green]"failed\n"[/green][/i][teal];[/teal]
[teal]}[/teal]


Feherke.
feherke.github.io
 
Thanks for the reply! I got my hopes up for a minute, but it looks like setCompressionName was introduced as of PHP 7 and we're still on 5.6. I probably should have mentioned that in my post.
 
I think I've determined that the bottleneck lies with my shared hosting provider. The code runs in 20 seconds on my home Ubuntu box. I've opened a ticket with them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top