PHP Compressed file Zip example
1. Please download my prepared zip.php tool class, download and unzip, put the files inside the corresponding directory, I was placed in the virtual directory in the Include folder.
2. Add the following code to your PHP file
Usage:
Require_once "./include/zip.php";
$zip = new Phpzip ();
$zip-Createzip ("folder directory address to compress", "compressed file name. zip"); Generate non-automatic downloads only
$zip-Downloadzip ("folder directory address to compress", "compressed file name. zip"); Automatic download
Example: You can refer to the following pseudo-code to see a specific usage scenario: code
Require_once "./include/zip.php";
if (! $download) {
Exit ();
}
Set_time_limit (60);
$tmpManager = new Tmpmanager (); Let's say we have a class to do the following
$tempfolder = Array ();
$tempfile = Array (); Let's say we download the selected folder or file by selecting the checkbox on the page and package it together
for ($i = 0; $i < $checkboxnum; $i) {
$value = ${"select". $i};
if ($value! =) {
$this _type = substr ($value, 0, 1);
$this _id = substr ($value, 1);
Handling folders and files separately
if ($this _type = = d) {
$tempfolder [] = $this _id;
}
ElseIf ($this _type = = f) {
$tempfile [] = $this _id;
}
}
}
@mkdir ($tempdir);
$curtempdir = "$tempdir/". $userid; Different users operate under different temporary folders
if (file_exists ($curtempdir)) {
$tmpManager->deletedir ($curtempdir); Delete the old folder
}
if (sizeof ($tempfolder) > 0 | | sizeof ($TEMPFILE) > 0) {
mkdir ($curtempdir, 0777); Re-create the folder if you have a folder to package the files
}
if (sizeof ($tempfile) > 0) {
$tmpManager->copyfile ($tempfile, $curtempdir); Copy the file you want to download to the folder you created
}
if (sizeof ($tempfolder) > 0) {
$tmpManager->copyfolder ($tempfolder, $curtempdir); Copy the folder you want to download to the folder you created
}
$zip = new Phpzip ();
$zip-Downloadzip ($curtempdir, "File_". Date (YMD). ". Zip ");//Package and download
Compression class:
- /*
- File Name:/include/zip.php
- Author:horace 2009/04/15
- */
- Class phpzip{
- var $dirInfo = Array ("0", "0");
- var $rootDir =;
- var $datasec = array ();
- var $ctrl _dir = Array ();
- var $eof _ctrl_dir = "x50x4bx05x06x00x00x00x00";
- var $old _offset = 0;
- function Downloadzip () {
- Createzip ($dir, $zipfilename, true);
- }
- function Createzip ($dir, $zipfilename, $autoDownload = False) {
- if (@function_exists (gzcompress)) {
- @set_time_limit ("0");
- if (Is_array ($dir)) {
- $FD = fopen ($dir, "R");
- $fileValue = Fread ($FD, FileSize ($filename));
- Fclose ($FD);
- if (Is_array ($dir)) $filename = basename ($dir);
- $this-AddFile ($fileValue, "$filename");
- }else{
- $this->dirtree ($dir, $dir);
- }
- $zipfilenametemp = Time (). $zipfilename;
- $out = $this-Filezip ();
- $fp = fopen ($zipfilenametemp, "w");
- Fwrite ($fp, $out, strlen ($out));
- Fclose ($FP);
- $filesize = FileSize ($zipfilenametemp);
- if ($filesize < 104857600) {
- if ($autoDownload) {
- Header ("Content-type:application/octet-stream");
- Header ("content-disposition:attachment; Filename= ". $zipfilename);
- }
- Echo $this-Filezip ();
- }else{
- echo "Create zip error!";
- }
- Unlink ($zipfilenametemp);
- }
- }
- Get dir tree.
- function DirTree ($directory, $rootDir) {
- Global $_server, $dirInfo, $rootDir;
- $fileDir = $rootDir;
- $myDir =dir ($directory);
- while ($file = $myDir->read ()) {
- if (Is_dir ("$directory/$file") and $file! = "." and $file! = "...") {
- $dirInfo [0];
- $rootDir = "$fileDir $file/";
- $this-AddFile (, "$rootDir");
- Go on NS folders
- $this->dirtree ("$directory/$file", $rootDir);
- }else{
- if ($file! = "." and $file! = "...") {
- $dirInfo [1];
- $FD = fopen ("$directory/$file", "R");
- $fileValue = file_get_contents ("$directory/$file");
- Fclose ($FD);
- $this-AddFile ($fileValue, "$fileDir $file");
- }
- }
- }
- $myDir->close ();
- }
- 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);
- }
- 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 = "x50x4bx03x04";
- $fr. = "x14x00"; Ver needed to extract
- $fr. = "x00x00"; Gen Purpose bit Flag
- $fr. = "x08x00"; 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 are 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 Central directory record
- $cdrec = "x50x4bx01x02";
- $cdrec. = "x00x00"; Version made by
- $cdrec. = "x14x00"; Version needed to extract
- $cdrec. = "x00x00"; Gen Purpose bit Flag
- $cdrec. = "x08x00"; 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
http://www.bkjia.com/PHPjc/486133.html www.bkjia.com true http://www.bkjia.com/PHPjc/486133.html techarticle PHP Compressed File Zip example 1. Please download my prepared zip.php tool class, download and unzip, put the files inside the corresponding directory, I was placed in the virtual directory of the include file ...
-