PHP Operations zip_php Tutorial

Source: Internet
Author: User
Tags fread php class
Class Phpzip

{

Private $ctrl _dir = Array ();

Private $datasec = Array ();




/**********************************************************



**********************************************************/

// ------------------------------------------------------ //

#ָļ

//

$archive = new Phpzip ();

$filelist = $archive->visitfile (ļ);

Print "ǰļеļ:

\ r \ n ";

foreach ($filelist as $file)

printf ("%s
\ r \ n ", $file);

// ------------------------------------------------------ //

var $fileList = array ();

Public Function Visitfile ($path)

{

Global $fileList;

$path = str_replace ("\ \", "/", $path);

$fdir = Dir ($path);


while (($file = $fdir->read ())!== false)
{

if ($file = = '. ' | | $file = = ' ... ') {continue;}



$pathSub = Preg_replace ("*/{2,}*", "/", $path. " /". $file); 滻 Bldg

$fileList [] = Is_dir ($pathSub)? $pathSub. " /": $pathSub;

if (Is_dir ($pathSub)) {$this->visitfile ($pathSub);}
}
$fdir->close ();

return $fileList;

}




Private 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);

}





var $old _offset = 0;

Private Function AddFile ($data, $filename, $time = 0)
{

$filename = str_replace (' \ \ ', '/', $filename);



$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 ($filename));

$fr. = Pack (' V ', 0);

$fr. = $filename;



$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 ($filename));

$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. = $filename;

$this->ctrl_dir[] = $cdrec;

}





var $eof _ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";

Private Function file ()

{

$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";

}





// ------------------------------------------------------ //



//

$archive = new Phpzip ();

$archive->zip ("Ѹļŀ¼", "Zipѹļ");

// ------------------------------------------------------ //

Public Function Zip ($dir, $saveName)

{

if (@!function_exists (' gzcompress ')) {return;}



Ob_end_clean ();

$filelist = $this->visitfile ($dir);

if (count ($filelist) = = 0) {return;}



foreach ($filelist as $file)

{

if (!file_exists ($file) | |!is_file ($file)) {continue;}



$FD = fopen ($file, "RB");

$content = @fread ($FD, FileSize ($file));

Fclose ($FD);



1.ɾ $dir ַ (./folder/file.txtɾ./folder/)

2./ɾ (/file.txtɾ/)

$file = substr ($file, strlen ($dir));

if (substr ($file, 0, 1) = = "\" | | substr ($file, 0, 1) = = "/") {$file = substr ($file, 1);}



$this->addfile ($content, $file);

}

$out = $this->file ();



$fp = fopen ($saveName, "WB");

Fwrite ($fp, $out, strlen ($out));

Fclose ($FP);

}





// ------------------------------------------------------ //

#ѹ ֱ

//

$archive = new Phpzip ();

$archive->zipanddownload ("Ѹļŀ¼");

// ------------------------------------------------------ //

Public Function Zipanddownload ($dir)

{

if (@!function_exists (' gzcompress ')) {return;}



Ob_end_clean ();

$filelist = $this->visitfile ($dir);

if (count ($filelist) = = 0) {return;}



foreach ($filelist as $file)

{

if (!file_exists ($file) | |!is_file ($file)) {continue;}



$FD = fopen ($file, "RB");

$content = @fread ($FD, FileSize ($file));

Fclose ($FD);



1.ɾ $dir ַ (./folder/file.txtɾ./folder/)

2./ɾ (/file.txtɾ/)

$file = substr ($file, strlen ($dir));

if (substr ($file, 0, 1) = = "\" | | substr ($file, 0, 1) = = "/") {$file = substr ($file, 1);}



$this->addfile ($content, $file);

}

$out = $this->file ();



@header (' Content-encoding:none ');

@header (' Content-type:application/zip ');

@header (' content-disposition:attachment; filename=farticle '. Date ("Ymdhis", Time ()). Zip ');

@header (' Pragma:no-cache ');

@header (' expires:0 ');

Print ($out);

}



/**********************************************************



**********************************************************/

// ------------------------------------------------------ //

Readcentraldir ($zip, $zipfile)

$zip ǿ @fopen ($zipfile, ' RB ')





Excerpt from Lpdx111 's column

http://www.bkjia.com/PHPjc/478171.html www.bkjia.com true http://www.bkjia.com/PHPjc/478171.html techarticle PHP class Phpzip {private $ctrl _dir = Array (); Private $datasec = Array ();/**************************************** ****************** * *****************************************...

  • 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.