PHP implementation file Online decompression _php tutorial

Source: Internet
Author: User
Tags fread unpack
Verify Password
$password = "123456";
?>

<title>Faisun_unzip-Pure Space-www.softpure.com</title>





if (!$_request["Myaction"]):
?>



























Faisun_unzip-Online Unzip zip file program V1.0


Reprint Please specify: This program is provided by pure space (http://www.softpure.com), Author: Faisun (faisun@sina.com).


How to use: Upload the zip file via FTP to the same directory as this file, select the zip file, or simply click "Browse ..." to upload the zip file.


The extracted results retain the original directory structure.


Select Zip file: -Please select- $fdir = Opendir ('./'), while ($file =readdir ($fdir)) {if (!is_file ($file)) continue; if (Preg_match ('/.zip$/ Mis ', $file)) {echo "$filern "; }}?>
or upload a file:
Extract to Directory:
(Leave blank for this directory, must have write permission)
Verify Password:
(password set in the source file)



ElseIf ($_request["myaction"]== "Dounzip"):


Class zip
{

var $total _files = 0;
var $total _folders = 0;

function Extract ($ZN, $to, $index = Array (-1))
{
$ok = 0; $zip = @fopen ($ZN, ' RB ');
if (! $zip) return (-1);
$cdir = $this->readcentraldir ($zip, $ZN);
$pos _entry = $cdir [' offset '];

if (!is_array ($index)) {$index = array ($index);}
for ($i =0; $index [$i]; $i + +) {
if (Intval ($index [$i])! = $index [$i]| | $index [$i]> $cdir [' Entries '])
Return (-1);
}
for ($i =0; $i < $cdir [' entries ']; $i + +)
{
@fseek ($zip, $pos _entry);
$header = $this->readcentralfileheaders ($zip);
$header [' index '] = $i; $pos _entry = Ftell ($zip);
@rewind ($zip); Fseek ($zip, $header [' offset ']);
if (In_array ("1", $index) | | In_array ($i, $index))
$stat [$header [' filename ']]= $this->extractfile ($header, $to, $zip);
}
Fclose ($zip);
return $stat;
}

function Readfileheader ($zip)
{
$binary _data = fread ($zip, 30);
$data = Unpack (' vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/vcrc/vcompressed_size/vsize/vfilename_len/ Vextra_len ', $binary _data);

$header [' filename '] = fread ($zip, $data [' Filename_len ']);
if ($data [' Extra_len ']! = 0) {
$header [' extra '] = Fread ($zip, $data [' Extra_len ']);
} else {$header [' extra '] = ';}

$header [' compression '] = $data [' compression ']; $header [' size '] = $data [' Size '];
$header [' compressed_size '] = $data [' compressed_size '];
$header [' CRC '] = $data [' CRC ']; $header [' flag '] = $data [' flag '];
$header [' mdate '] = $data [' mdate ']; $header [' mtime '] = $data [' Mtime '];

if ($header [' mdate '] && $header [' mtime ']) {
$hour = ($header [' Mtime ']&0xf800) >>11; $minute = ($header [' Mtime ']&0x07e0) >>5;
$seconde = ($header [' mtime ']&0x001f) * *, $year = (($header [' mdate ']&0xfe00) >>9) +1980;
$month = ($header [' mdate ']&0x01e0) >>5; $day = $header [' Mdate ']&0x001f;
$header [' mtime '] = Mktime ($hour, $minute, $seconde, $month, $day, $year);
}else{$header [' mtime '] = time ();}

$header [' stored_filename '] = $header [' filename '];
$header [' status '] = "OK";
return $header;
}

function Readcentralfileheaders ($zip) {
$binary _data = fread ($zip, 46);
$header = Unpack (' vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/vcrc/vcompressed_size/ Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/vexternal/voffset ', $binary _data);

if ($header [' Filename_len ']! = 0)
$header [' filename '] = fread ($zip, $header [' Filename_len ']);
else $header [' filename '] = ';

if ($header [' Extra_len ']! = 0)
$header [' extra '] = Fread ($zip, $header [' Extra_len ']);
else $header [' extra '] = ';

if ($header [' Comment_len ']! = 0)
$header [' comment '] = fread ($zip, $header [' Comment_len ']);
else $header [' comment '] = ';

if ($header [' mdate '] && $header [' mtime '])
{
$hour = ($header [' Mtime '] & 0xf800) >> 11;
$minute = ($header [' Mtime '] & 0x07e0) >> 5;
$seconde = ($header [' Mtime '] & 0x001f) * *;
$year = (($header [' Mdate '] & 0xfe00) >> 9) + 1980;
$month = ($header [' Mdate '] & 0x01e0) >> 5;
$day = $header [' mdate '] & 0x001f;
$header [' mtime '] = Mktime ($hour, $minute, $seconde, $month, $day, $year);
} else {
$header [' mtime '] = time ();
}
$header [' stored_filename '] = $header [' filename '];
$header [' status '] = ' OK ';
if (substr ($header [' filename '],-1) = = '/')
$header [' external '] = 0x41ff0010;
return $header;
}

function Readcentraldir ($zip, $zip _name) {
$size = filesize ($zip _name);

if ($size < 277) $maximum _size = $size;
else $maximum _size=277;

@fseek ($zip, $size-$maximum _size);
$pos = Ftell ($zip); $bytes = 0x00000000;

while ($pos < $size) {
$byte = @fread ($zip, 1); $bytes = ($bytes << 8) | Ord ($byte);
if ($bytes = = 0x504b0506 or $bytes = = 0x2e706870504b0506) {$pos ++;break;} $pos + +;
}

$fdata =fread ($zip, 18);

$data = @unpack (' vdisk/vdisk_start/vdisk_entries/ventries/vsize/voffset/vcomment_size ', $fdata);

if ($data [' comment_size ']! = 0) $centd [' comment '] = fread ($zip, $data [' comment_size ']);
else $centd [' comment '] = '; $centd [' entries '] = $data [' Entries '];
$centd [' disk_entries '] = $data [' disk_entries '];
$centd [' offset '] = $data [' offset ']; $centd [' disk_start '] = $data [' Disk_start '];
$centd [' size '] = $data [' Size ']; $CENTD [' disk '] = $data [' Disk '];
return $CENTD;
}

function Extractfile ($header, $to, $zip) {
$header = $this->readfileheader ($zip);

if (substr ($to, -1)! = "/") $to. = "/";
if ($to = = './') $to = ';
$pth = Explode ("/", $to. $header [' filename ']);
$mydir = ";
for ($i =0; $i <>
if (! $pth [$i]) continue;
$mydir. = $pth [$i]. " /";
if (!is_dir ($mydir) && @mkdir ($mydir, 0777)) | | ($mydir = = $to. $header [' filename '] | | ($mydir = = $to && $this->total_folders==0)) && Is_dir ($mydir))) {
@chmod ($mydir, 0777);
$this->total_folders + +;
echo " Catalog: $mydir
";
}
}

if (STRRCHR ($header [' filename '], '/') = = '/') return;

if (! ( $header [' External ']==0x41ff0010) &&! ($header [' External ']==16)] {
if ($header [' compression ']==0) {
$fp = @fopen ($to. $header [' filename '], ' WB ');
if (! $fp) return (-1);
$size = $header [' compressed_size '];

while ($size! = 0) {
$read _size = ($size < 2048? $size: 2048);
$buffer = Fread ($zip, $read _size);
$binary _data = Pack (' a '. $read _size, $buffer);
@fwrite ($fp, $binary _data, $read _size);
$size-= $read _size;
}
Fclose ($FP);
Touch ($to. $header [' filename '], $header [' mtime '];
}else{
$fp = @fopen ($to. $header [' filename ']. GZ ', ' WB ');
if (! $fp) return (-1);
$binary _data = Pack (' va1a1va1a1 ', 0x8b1f, Chr ($header [' compression ']),
Chr (0x00), Time (), Chr (0x00), Chr (3));

Fwrite ($fp, $binary _data, 10);
$size = $header [' compressed_size '];

while ($size! = 0) {
$read _size = ($size < 1024x768 $size: 1024);
$buffer = Fread ($zip, $read _size);
$binary _data = Pack (' a '. $read _size, $buffer);
@fwrite ($fp, $binary _data, $read _size);
$size-= $read _size;
}

$binary _data = Pack (' VV ', $header [' CRC '], $header [' size '];
Fwrite ($fp, $binary _data,8); Fclose ($FP);

$GZP = @gzopen ($to. $header [' filename ']. GZ ', ' RB ') or die ("Cette archive est compress

http://www.bkjia.com/PHPjc/445079.html www.bkjia.com true http://www.bkjia.com/PHPjc/445079.html techarticle ? PHP//Authentication password $password = 123456;? htmlheadmeta http-equiv=content-type content=text/html; charset=gb2312 Titlefaisun _unzip-Pure space-www.softpure.com/title style Typ ...

  • Related Article

    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.