Note that this PHP tutorial File compression code is used to help zip.exe files, so we need to extract zip.exe compressed files.
<? Php
If (! IsSet ($ _ GET ['dirname'])
{
Show_input_form ();
}
Else
{
// Check if empty
If (empty ($ _ GET ['dirname'])
{
Hg_exit ("Enter the folder name! ");
}
// Check valid dirname
If (FALSE! = Strpos ($ _ GET ['dirname'], "/")
{
Hg_exit ("/" is an invalid folder name! ");
}
If (FALSE! = Strstr ($ _ GET ['dirname'], "...")
{
Hg_exit ("" .. "is an invalid folder name! ");
}
// Check valid dir
If (! Is_dir ($ _ GET ['dirname'])
{
Hg_exit ("" {$ _ GET ['dirname']} "is not a valid folder! ");
}
$ SzData = "";
$ SzInfo = "";
$ File_count = @ ZipDir ($ _ GET ['dirname'], & $ szData, & $ szInfo );
$ Info_size_16byte = @ sprintf ("% 016d", @ strlen ($ szInfo ));
$ SzData = @ sprintf ("% 016d", $ file_count). $ info_size_16byte. $ szInfo. $ szData;
$ Filename = $ _ GET ['dirname']. ". dat ";
If (function_exists (gzencode ))
{
$ SzData = gzencode ($ szData );
$ Filename. = ". gz ";
}
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length:". strlen ($ szData ));
Header ("Content-Disposition: attachment; filename = $ filename ");
Echo $ szData;
}
Function show_input_form ()
{
Echo HtmlHead ("file packaging ");
Echo "<form name =" input "> n"
. "Enter the folder to be packaged. Note that only the folder in the current directory can be downloaded! <P> n"
. "<Input name =" dirname "> n"
. "<Input type =" button "value =" OK "onClick =" show_download_link (dirname. value); "> n"
. "</Form> n ";
Echo "<script> n"
. "Input. dirname. focus (); n"
. "Function show_download_link (dir) n"
."{"
. "Var top = (screen. height-200)/2; n"
. "Var left = (screen. width-300)/2; n"
. "Newwin = window. open ('','', 'width = 300, height = 200, top = '+ top +', left = '+ left +', resizable = 0, scrollbars = auto'); n"
. "Url =" {$ _ SERVER ['php _ SELF ']} "+ "? Dirname = "+ dir; n"
. "Newwin.doc ument. write ('<a href =' + url + '> click this link to download, <br> or right-click here and choose "Save as" </a>'); n"
."}"
. "</Script> n ";
Echo HtmlFoot ();
}
Function ZipDir ($ szDirName, & $ szData, & $ szInfo)
{
// Write dir header
$ SzInfo. = "$ szDirName | [dir] n ";
$ File_count = 0;
$ HDir = OpenDir ($ szDirName );
While ($ file = ReadDir ($ hDir ))
{
If ($ file = "." | $ file = "..") continue;
$ SzCurFile = "$ szDirName/$ file ";
If (Is_Dir ($ szCurFile ))
{
$ File_count + = ZipDir ($ szCurFile, & $ szData, & $ szInfo );
}
Else if (Is_File ($ szCurFile ))
{
$ HCurFile = fopen ($ szCurFile, "rb ");
$ Size = filesize ($ szCurFile );
$ SzStream = fread ($ hCurFile, $ size );
Fclose ($ hCurFile );
$ File_count ++;
// Write info
$ SzInfo. = "$ szCurFile | $ sizen ";
// Write data
$ SzData. = $ szStream;
}
}
// Write dir footer
$ SzInfo. = "$ szDirName | [/dir] n ";
Return $ file_count;
}
Function hg_exit ($ str)
{
Echo HtmlHead ("Error, exit! ");
Echo "Echo HtmlFoot ();
Exit;
}
Function HtmlHead ($ title)
{
Return ". "<Meta http-equiv =" Content-Type "content =" text/html; charset = gb2312 "> n"
. "<Style type =" text/css tutorial "> n"
. "Body, input, td {font: 12px verdana} n"
. "</Style> n"
. "</Head> nn <body> nn ";
}
Function HtmlFoot ()
{
Return Copyright (). "n </body> nn }
Function Copyright ()
{
Return "<center> <font size =" 5 "face =" _ GB2312 "color =" red "> Delete this file immediately after use, to avoid being discovered and used by others! </Font> </center> n"
<Br> . "<Center> n"
. "<P style =" font-family: verdana; font-size: 12px "> Contact us: n"
. "<A href =" http://www.bkjia.com/"target =" _ blank "> http://www.bkjia.com/</a> </p> n"
. "</Center> n"
. "</Body> n"
. "N"
. "</Html> ";
}
?>