A PHP Zip compression class sharing _php instance

Source: Internet
Author: User
Tags bit set crc32 getdate pack time and date

Function: Compress the file into zip, or rar compressed package. Suffix names can be customized.

How to: Instantiate first, then pass the argument. Two parameters. First an array of your file addresses. The second is the absolute address of the compressed package file you want to save.

Use examples:

Copy Code code as follows:

$zipfiles =array ("/root/pooy/test1.txt", "/root/pooy/test2.txt");
$z = new Phpzip ();
$RANDOMSTR = random (8);
$zipfile = TEMP. " /photocome_ ". $groupid.". Zip ";
$z->zip ($zipfiles, $zipfile); Add File List


The zip compression class for PHP is as follows:

Copy Code code as follows:

<?php
#
# Phpzip v1.2 by Sext (sext@neud.net) 2002-11-18
# (CHANGED:2003-03-01)
#
# makes ZIP archive
#
# Based on ' Zip file Creation class ', uses ZLib
#
#
Class Phpzip
{
function Zip ($dir, $zipfilename)
{
if (@function_exists (' gzcompress '))
{
$curdir = GETCWD ();
if (Is_array ($dir))
{
$filelist = $dir;
}
Else
{
$filelist = $this-> getfilelist ($dir);
}

if (!empty ($dir)) && (!is_array ($dir)) && (File_exists ($dir)) chdir ($dir);
else ChDir ($curdir);

if (count ($filelist) >0)
{
foreach ($filelist as $filename)
{
if (Is_file ($filename))
{
$FD = fopen ($filename, "R");
$content = Fread ($FD, FileSize ($filename));
Fclose ($FD);

if (Is_array ($dir)) $filename = basename ($filename);
$this-> addfile ($content, $filename);
}
}
$out = $this-> file ();

ChDir ($curdir);
$fp = fopen ($zipfilename, "w");
Fwrite ($fp, $out, strlen ($out));
Fclose ($FP);
}
return 1;
}
else return 0;
}

function Getfilelist ($dir)
{
if (file_exists ($dir))
{
$args = Func_get_args ();
$pref = $args [1];

$DH = Opendir ($dir);
while ($files = Readdir ($DH))
{
if ($files!= ".") && ($files!= "..."))
{
if (Is_dir ($dir. $files))
{
$curdir = GETCWD ();
ChDir ($dir. $files);
$file = Array_merge ($file, $this-> getfilelist ("", "$pref $files/"));
ChDir ($curdir);
}
else $file []= $pref. $files;
}
}
Closedir ($DH);
}
return $file;
}

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

/**
* Converts a Unix timestamp to a four byte-DOS date and time format (date
* In the high two bytes, the time in the low two bytes allowing magnitude).
*
* @param integer The current Unix timestamp
*
* @return Integer The current date in a four byte DOS format
*
* @access 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;
}//End If

Return (($timearray [' Year ']-1980) << 25) | ($timearray [' mon '] << 21) | ($timearray [' Mday '] << 16) |
($timearray [' hours '] << 11) | ($timearray [' minutes '] << 5) | ($timearray [' seconds '] >> 1);
}//End of the ' unix2dostime () ' method

/**
* Adds "file" to archive
*
* @param string file contents
* @param string name of the file in the archive (may contains the path)
* @param integer The current timestamp
*
* @access Public
*/
function AddFile ($data, $name, $time = 0)
{
$name = str_replace (' \ \ ', '/', $name);

$dtime = Dechex ($this->unix2dostime ($time));
$hexdtime = ' \x '. $dtime [6]. $dtime [7]
<a href= "http://wutransfer.com/western-union-locations-in-russia-taiynsha/" >western Union point</a>. ' \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"; Ver needed to extract
$fr. = "\x00\x00"; Gen Purpose bit Flag
$fr. = "\x08\x00"; Compression method
$fr. = $hexdtime; Last mod time and date

"Local File Header" segment
$unc _len = strlen ($data);
$CRC = CRC32 ($data);
$zdata = gzcompress ($data);
$c _len = strlen ($zdata);
$zdata = substr (substr ($zdata, 0, strlen ($zdata)-4), 2); Fix CRC Bug
$fr. = Pack (' V ', $CRC); Crc32
$fr. = Pack (' V ', $c _len); Compressed FileSize
$fr. = Pack (' V ', $unc _len); Uncompressed FileSize
$fr. = Pack (' V ', strlen ($name)); Length of filename
$fr. = Pack (' V ', 0); Extra field length
$fr. = $name;

"File Data" segment
$fr. = $zdata;

"Data Descriptor" segment (optional but necessary if archive is not
served as file)
$fr. = Pack (' V ', $CRC); Crc32
$fr. = Pack (' V ', $c _len); Compressed FileSize
$fr. = Pack (' V ', $unc _len); Uncompressed FileSize

Add this entry to array
$this-> datasec[] = $FR;
$new _offset = strlen (Implode (', $this->datasec));

Now add to the "Now" directory record
$cdrec = "\x50\x4b\x01\x02";
$cdrec. = "\x00\x00"; Version made by
$cdrec. = "\x14\x00"; Version needed to extract
$cdrec. = "\x00\x00"; Gen Purpose bit Flag
$cdrec. = "\x08\x00"; Compression method
$cdrec. = $hexdtime; Last MoD time & date
$cdrec. = Pack (' V ', $CRC); Crc32
$cdrec. = Pack (' V ', $c _len); Compressed FileSize
$cdrec. = Pack (' V ', $unc _len); Uncompressed FileSize
$cdrec. = Pack (' V ', strlen ($name)); Length of filename
$cdrec. = Pack (' V ', 0); Extra field length
$cdrec. = Pack (' V ', 0); File Comment length
$cdrec. = Pack (' V ', 0); Disk number Start
$cdrec. = Pack (' V ', 0); Internal file attributes
$cdrec. = Pack (' V ', 32); External file attributes-' archive ' bit set

$cdrec. = Pack (' V ', $this-> old_offset); Relative offset of local header
$this-> old_offset = $new _offset;

$cdrec. = $name;

Optional extra field, file comment goes here
Save to the \ Directory
$this-> ctrl_dir[] = $cdrec;
}//End of the ' AddFile () ' method

/**
* Dumps out file
*
* @return String The zipped file
*
* @access Public
*/
function file ()
{
$data = Implode (", $this-> datasec);
$ctrldir = Implode (", $this-> Ctrl_dir);

Return
$data.
$ctrldir.
$this-> Eof_ctrl_dir.
Pack (' V ', sizeof ($this-> ctrl_dir)). Total # of entries ' on this disk '
Pack (' V ', sizeof ($this-> ctrl_dir)). Total # of entries overall
Pack (' V ', strlen ($ctrldir)). Size of
Pack (' V ', strlen ($data)). Offset to start of the
"\x00\x00"; . zip file Comment length
}//end of the ' file () ' method

}//End of the ' Phpzip ' class
?>

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.