PHP online unzipping source code

Source: Internet
Author: User
Tags fread php online unpack

<? PHP

// Verify the password
$ Password = "123456 ";

?>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> faisun_unzip </title>
<Style type = "text/CSS">
<! --
Body, TD {
Font-size: 14px;
Color: #000000;
}
A {
Color: #000066;
Text-Decoration: none;
}
A: hover {
Color: # ff6600;
Text-Decoration: underline;
}
-->
</Style>
</Head>

<Body>
<Form name = "myform" method = "Post" Action = "<? =$ _ Server [php_self];?> "Enctype =" multipart/form-Data "onsubmit =" Return check_uploadobject (this); ">
<?
If (! $ _ Request ["myaction"]):
?>

<Script language = "JavaScript">
Function check_uploadobject (form ){
If (Form. Password. value = ''){
Alert ('enter your password .');
Return false;
}
Return true;
}
</SCRIPT>

<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "4">
<Tr>
<TD Height = "40" colspan = "2" style = "color: # ff9900"> <p> <font color = "# ff0000"> faisun_unzip-decompress the ZIP file online Program V1.0 </font> </P>
<P> reprinted, please note: by <a href = "http://www.softpure.com/" target = "_ blank"> pure space </a> (<a href = "http://www.softpure.com/" target = "_ blank"> http://www.softpure.com </a>) supplied by: faisun (<a href = "mailto: faisun@sina.com"> faisun@sina.com </a> ). </P>
<P> usage: Upload the ZIP file to the same directory of the file through FTP, select the ZIP file, or click browse... to upload the ZIP file. </P>
<P> the extracted results retain the original directory structure. </P>
<P> & nbsp; </P> </TD>
</Tr>
<Tr>
<TD width = "11%"> select ZIP file: </TD>
<TD width = "89%"> <select name = "zipfile">
<Option value = "" selected>-select-</option>
<?
$ Fdir = opendir ('./');
While ($ file = readdir ($ fdir )){
If (! Is_file ($ file) continue;
If (preg_match ('/\. Zip $/MIS', $ file )){
Echo "<option value = '$ file'> $ file </option> \ r \ n ";
}
}
?>
</SELECT> </TD>
</Tr>
<Tr>
<TD width = "11%" nowrap> or upload a file: </TD>
<TD width = "89%"> <input name = "upfile" type = "file" id = "upfile" size = "20"> </TD>
</Tr>
<Tr>
<TD> decompress the package to the directory: </TD>
<TD> <input name = "todir" type = "text" id = "todir" value = "_ unzipfiles _" size = "15">
(Leave this directory empty and you must have the write permission) </TD>
</Tr>
<Tr>
<TD> Verification password: </TD>
<TD> <input name = "password" type = "password" id = "password" size = "15">
(Password set in the source file) </TD>
</Tr>
<Tr>
<TD> <input name = "myaction" type = "hidden" id = "myaction" value = "dounzip"> </TD>
<TD> <input type = "Submit" name = "Submit" value = "decompress"> </TD>
</Tr>
</Table>

<?

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) * 2; $ 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/kernel/vflag/vcompression/vmtime/vmdate/vcrc/kernel/vsize/kernel/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) * 2;
$ 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 <count ($ PTH)-1; $ I ++ ){
If (! $ PTH [$ I]) continue;
$ Mydir. = $ PTH [$ I]. "/";
If ((! Is_dir ($ mydir) & @ mkdir ($ mydir, 0777) | ($ mydir = $. $ header ['filename'] | ($ mydir == to & $ this-> total_folders = 0) & is_dir ($ mydir ))){
@ Chmod ($ mydir, 0777 );
$ This-> total_folders ++;
Echo "<input name = 'dfile [] 'Type = 'checkbox' value = '$ mydir' checked> <a href =' $ mydir' target = '_ blank'> directory: $ mydir </a> <br> ";
}
}

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.w.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 <1024? $ 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.w.header='filename'{.'.gz ', 'rb') or die ("Cette archive est compress compression ");
If (! $ Gzp) Return (-2 );
$ Fp = @ fopen ($ to. $ header ['filename'], 'wb ');
If (! $ FP) Return (-1 );
$ Size = $ header ['SIZE'];

While ($ size! = 0 ){
$ Read_size = ($ size <2048? $ Size: 2048 );
$ Buffer = gzread ($ gzp, $ read_size );
$ Binary_data = pack ('A'. $ read_size, $ buffer );
@ Fwrite ($ FP, $ binary_data, $ read_size );
$ Size-= $ read_size;
}
Fclose ($ FP); gzclose ($ gzp );

Touch ($ to. $ header ['filename'], $ header ['mtime']);
@ Unlink(%to.%header%'filename'%.'.gz ');

}
}

$ This-> total_files ++;
Echo "<input name = 'dfile [] 'Type = 'checkbox' value = '$ to $ header [filename] 'checked> <a href =' $ to $ header [filename] 'target = '_ blank'> file: $ to $ header [filename] </a> <br> ";

Return true;
}

// End class
}

Set_time_limit (0 );

If ($ _ post ['Password']! = $ Password) Die ("the entered password is incorrect. Please enter it again. ");
If (! $ _ Post ["todir"]) $ _ post ["todir"] = ".";
$ Z = new zip;
$ Have_zip_file = 0;
Function start_unzip ($ tmp_name, $ new_name, $ checked ){
Global $ _ post, $ Z, $ have_zip_file;
$ Upfile = array ("tmp_name" => $ tmp_name, "name" => $ new_name );
If (is_file ($ upfile [tmp_name]) {
$ Have_zip_file = 1;
Echo "<br> decompression: <input name = 'dfile [] 'Type = 'checkbox' value = '$ upfile [name]'". ($ checked? "Checked": ""). "> $ upfile [name] <br> ";
If (preg_match ('/\. Zip $/MIS', $ upfile [name]) {
$ Result = $ Z-> extract ($ upfile [tmp_name], $ _ post ["todir"]);
If ($ result =-1 ){
Echo "<br> File $ upfile [name] error. <br> ";
}
Echo "<br> complete. A total of $ Z-> total_folders directories and $ Z-> total_files files are created. <br> ";
} Else {
Echo "<br> $ upfile [name] is not a zip file. <br> ";
}
If (realpath ($ upfile [name])! = Realpath ($ upfile [tmp_name]) {
@ Unlink ($ upfile [name]);
Rename ($ upfile [tmp_name], $ upfile [name]);
}
}
}
Clearstatcache ();

Start_unzip ($ _ post ["zipfile"], $ _ post ["zipfile"], 0 );
Start_unzip ($ _ FILES ["upfile"] [tmp_name], $ _ FILES ["upfile"] [name], 1 );

If (! $ Have_zip_file ){
Echo "<br> select or upload a file. <br> ";
}
?>
<Input name = "password" type = "hidden" id = "password" value = "<? =_ _ Post ['Password'];?> ">
<Input name = "myaction" type = "hidden" id = "myaction" value = "dodelete">
<Input name = "button" type = "button" value = "return" onclick = "window. Location = '<? =$ _ Server [php_self];?> '; ">

<Input type = 'button 'value = 'select 'onclick = 'selrev ();'> <input type = 'submit 'onclick = 'Return confirm ("delete selected files? "); 'Value = 'delete select'>

<Script language = 'javascript '>
Function selrev (){
With (document. myform ){
For (I = 0; I <elements. length; I ++ ){
Thiselm = elements [I];
If (thiselm. Name. Match (/dfile \ []/) thiselm. Checked =! Thiselm. checked;
}
}
}
Alert ('finished .');
</SCRIPT>
<?

Elseif ($ _ request ["myaction"] = "dodelete "):
Set_time_limit (0 );
If ($ _ post ['Password']! = $ Password) Die ("the entered password is incorrect. Please enter it again. ");

$ Dfile = $ _ post ["dfile"];
Echo "deleting file... <br> ";
If (is_array ($ dfile )){
For ($ I = count ($ dfile)-1; $ I> = 0; $ I --){
If (is_file ($ dfile [$ I]) {
If (@ unlink ($ dfile [$ I]) {
Echo "deleted files: $ dfile [$ I] <br> ";
} Else {
Echo "failed to delete the file: $ dfile [$ I] <br> ";
}
} Else {
If (@ rmdir ($ dfile [$ I]) {
Echo "deleted Directory: $ dfile [$ I] <br> ";
} Else {
Echo "failed to delete Directory: $ dfile [$ I] <br> ";
}
}

}
}
Echo "<br> finished. <br> <input type = 'button 'value = ', 'onclick = \ "window is returned. location = '$ _ server [php_self]'; \ "> <br>
<Script language = 'javascript '> ('finished.'); </SCRIPT> ";

Endif;

?>
</Form>
</Body>
</Html>

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.