Little brother I have such a compressed folder class, to find a faster compression method

Source: Internet
Author: User
Tags zip extension
I have such a compressed folder class, to find a faster compression method


Class Phpzip
{

var $datasec = array ();
var $ctrl _dir = Array ();
var $eof _ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
var $old _offset = 0;

function Zip ($dir, $zipfilename)
{
if (@function_exists (' gzcompress '))
{
@set_time_limit ("0");
$this->openfile ($dir, $dir);
$out = $this->filezip ();
$fp = fopen ($zipfilename, "w");
$fwrite = fwrite ($fp, $out, strlen ($out));
Fclose ($FP);
if ($fwrite = = True)
{
return true;
}
}
return false;
}

function OpenFile ($path, $zipName)
{
$temp _path = $path;
$temp _zip_path = $zipName;
$zipDir = $zipName;

if ($handle = @opendir ($path))
{
while (($file = Readdir ($handle))!==false)
{
$sub _dir = $path. Directory_separator. $file; To build a subdirectory path
if ($file = = '. ' | | $file = = ' ... ') {
Continue
}else if (Is_dir ($sub _dir)) {//If it is a directory, recursively
$this->openfile ($path. '/' . $file, $zipName. '/' . $file);
}
else{
$FD = fopen ($path. '/' . $file, "R");
$fileValue = @fread ($FD, 1024000);
if ($FD)
Fclose ($FD);
$this->addfile ($fileValue, $zipName. '/' . $file);
}
}
$zipName = $temp _zip_path;
$path = $temp _path;
Closedir ($handle);
}
}

function Unix2dostime ($unixtime = 0)
{
$timearray = ($unixtime ==0)? GETDATE (): getdate ($unixtime);
if ($timearray [' Year ']<1980)
{
$timearray [' year '] = 1980;
$timearray [' mon '] = 1;
$timearray [' mday '] = 1;
$timearray [' hours '] = 0;
$timearray [' minutes '] = 0;
$timearray [' seconds '] = 0;
}
Return (($timearray [' Year ']-1980) <<25) | ($timearray [' mon ']<<21) | ($timearray [' Mday ']<<16) |
($timearray [' hours ']<<11) | ($timearray [' minutes ']<<5) | ($timearray [' seconds ']>>1);
}

function AddFile ($data, $name, $time = 0)
{
$name = str_replace (' \ \ ', '/', $name);
$dtime = Dechex ($this->unix2dostime ($time));
$hexdtime = ' \x '. $dtime [6]. $dtime [7]
. ' \x '. $dtime [4]. $dtime [5]
. ' \x '. $dtime [2]. $dtime [3]
. ' \x '. $dtime [0]. $dtime [1];
Eval (' $hexdtime = '. $hexdtime. '";');
$FR = "\x50\x4b\x03\x04";
$fr. = "\x14\x00";
$fr. = "\x00\x00";
$fr. = "\x08\x00";
$fr. = $hexdtime;
$unc _len = strlen ($data);
$CRC = CRC32 ($data);
$zdata = gzcompress ($data);
$c _len = strlen ($zdata);
$zdata = substr (substr ($zdata, 0, strlen ($zdata)-4), 2);
$fr. = Pack (' V ', $CRC);
$fr. = Pack (' V ', $c _len);
$fr. = Pack (' V ', $unc _len);
$fr. = Pack (' V ', strlen ($name));
$fr. = Pack (' V ', 0);
$fr. = $name;
$fr. = $zdata;
$fr. = Pack (' V ', $CRC);
$fr. = Pack (' V ', $c _len);
$fr. = Pack (' V ', $unc _len);
$this->datasec[] = $FR;
$new _offset = strlen (Implode (", $this->datasec));
$cdrec = "\x50\x4b\x01\x02";
$cdrec. = "\x00\x00";
$cdrec. = "\x14\x00";
$cdrec. = "\x00\x00";
$cdrec. = "\x08\x00";
$cdrec. = $hexdtime;
$cdrec. = Pack (' V ', $CRC);
$cdrec. = Pack (' V ', $c _len);
$cdrec. = Pack (' V ', $unc _len);
$cdrec. = Pack (' V ', strlen ($name));
$cdrec. = Pack (' V ', 0);
$cdrec. = Pack (' V ', 0);
$cdrec. = Pack (' V ', 0);
$cdrec. = Pack (' V ', 0);
$cdrec. = Pack (' V ', 32);
$cdrec. = Pack (' V ', $this->old_offset);
$this->old_offset = $new _offset;
$cdrec. = $name;
$this->ctrl_dir[] = $cdrec;
}

function Filezip ()
{
$data = Implode (' ', $this->datasec);
$ctrldir = Implode (' ', $this->ctrl_dir);
Return
$data.
$ctrldir.
$this->eof_ctrl_dir.
Pack (' V ', sizeof ($this->ctrl_dir)).
Pack (' V ', sizeof ($this->ctrl_dir)).
Pack (' V ', strlen ($ctrldir)).
Pack (' V ', strlen ($data)).
"\x00\x00";
}

}

I have a folder, probably 30m,600 a file, packaging time more than half a child, who has a better and faster packaging method Ah, thank you.


------Solution--------------------
PHP provides a zip extension, why not?
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.