Welcome Guest, Not a member yet? Register   Sign In
Performance issue of Zip Class
#1

[eluser]stanleyxu[/eluser]
I use the following code to compress a folder.
- When folder contains 1000 items, it takes 65secs
- When folder contains 500 items, it takes 8secs.
Code:
$this->load->library('zip'); // zip object
$this->zip->read_dir(($path_to_dir); // contains 1000+ files
$this->zip->archive($zip_filename);

I think there is a bottleneck in Zip.php.
I modified the source, after that:
- When folder contains 1000 items, it takes 2secs
- When folder contains 500 items, it takes 1secs.

This issue has been reported in bug tracker as well.
Full Source: Click Here
#2

[eluser]stanleyxu[/eluser]
I have found another issue related to Zip class: When creating a Zip file, this class will cache the zip content in memory, which means double memory usage.
Code:
$this->zipfile = $data.$dir."\x50\x4b\x05\x06\x00\x00\x00\x00"
When a script tries to make a big zipfile, it might be failed. Because usually PHP server allows 16M memory usage for each script execution. I have modified the source again (download) and implement it in another way. I think it really make sense. ^^)

Furthermore, Zip->archive($filepath) can be extended to Zip->archive($filepath, $volume=16384)....
#3

[eluser]tonanbarbarian[/eluser]
default memory usage in PHP 4 is only 8M by default - 16+ is PHP5+
#4

[eluser]stanleyxu[/eluser]
[quote author="tonanbarbarian" date="1202545911"]default memory usage in PHP 4 is only 8M by default - 16+ is PHP5+[/quote]
Thanks for the info. 8M is even worse for compressing file, isn't it? That is why, i request to use my modification. Smile
#5

[eluser]Chris Newton[/eluser]
Your URL seems to be down. Bummer.
#6

[eluser]Wilker[/eluser]
I'm sorry if I wrong... but... you leave this line commented:

$gzdata = gzcompress($data);

and this line is responsible to compress data... this way the data is zipping without compression... or no?
#7

[eluser]Chris Newton[/eluser]
The next line encompasses that line

$gzdata = substr(gzcompress($data), 2, -4);

So it's being compressed.
#8

[eluser]stanleyxu[/eluser]
[quote author="mahuti" date="1202595836"]Your URL seems to be down. Bummer.[/quote]
Sorry, the server is unstable.


[quote author="Wilker" date="1202602119"]I'm sorry if I wrong... but... you leave this line commented:

$gzdata = gzcompress($data);

and this line is responsible to compress data... this way the data is zipping without compression... or no?[/quote]

It works, I just combined two lines as one
Code:
// ORIGINAL
$gzdata = gzcompress($data);
$gzdata = substr($gzdata), 2, -4);
// NEW
$gzdata = substr(gzcompress($data), 2, -4);
#9

[eluser]ocergyNohtna[/eluser]
thanks so much for this. my script went from 16 seconds to hardly 1. Smile question... do you have any idea how to build the archive without the full path being archived when using read_dir? for example, assuming the following ./path/to/folder/ has several sub-folders and files
Code:
$dir = 'foo';
$this->zip->read_dir('./path/to/'.$dir.'/');
$this->zip->archive('./path/to/archive_dir/'.$dir.'.zip');
builds foo.zip with the following content structure:
Code:
.\path
  |
  |-to
    |
    |-foo
      |
      |-folder1
      |-folder2
      |-folder3
      |-folder4
      |-file1.xxx
      |-file2.xxx
      |-file3.xxx
as opposed to the desired content structure:
Code:
folder1
folder2
folder3
folder4
file1.xxx
file2.xxx
file3.xxx
#10

[eluser]stanleyxu[/eluser]
[quote author="ocergyNohtna" date="1202674559"]thanks so much for this. my script went from 16 seconds to hardly 1. Smile question... do you have any idea how to build the archive without the full path being archived when using read_dir? for example, assuming the following ./path/to/folder/ has several sub-folders and files
Code:
$dir = 'foo';
$this->zip->read_dir('./path/to/'.$dir.'/');
$this->zip->archive('./path/to/archive_dir/'.$dir.'.zip');
builds foo.zip with the following content structure:
Code:
.\path
  |
  |-to
    |
    |-foo
      |
      |-folder1
      |-folder2
      |-folder3
      |-folder4
      |-file1.xxx
      |-file2.xxx
      |-file3.xxx
as opposed to the desired content structure:
Code:
folder1
folder2
folder3
folder4
file1.xxx
file2.xxx
file3.xxx
[/quote]

Fortunately, yes ^^)
I overrided Zip class

Code:
/**
     * Add a complete directory snapshot to Zip
     *
     * @access  public
     * @param   string  physical path to the root
     * @param   string  used internally
     * @return  void
     */
    function add_all_($root, $zip_prefix = '')
    {
        $CI =& get_instance();
        $CI->load->helper('directory'); // directory_map

        foreach (directory_map($root, TRUE) as $item)
        {
            $filename = $root.'/'.$item;
            
            if (is_file($filename))
            {
                $this->add_data(
                    $zip_prefix.$item, file_get_contents($filename));
            }
            else
            {
                $this->add_dir($zip_prefix.$item);
                $this->add_all_(// recursively
                    $filename, $zip_prefix.$item.'/');
            }
        }
    }

I am still tuning Unzip module. Stay tunedWink




Theme © iAndrew 2016 - Forum software by © MyBB