Home Backend Development PHP Tutorial PHP package website online compression into zip

PHP package website online compression into zip

Jul 23, 2016 am 08:54 AM

  1. ?//在URL后参加 ?pwd=密码 查看生成密码
  2. $password = '8baa717e6265d1c1d762cc896151f821'; //l23
  3. ?>
  4. php ZIP压缩程序
  5. 在线压缩ZIP文件程序


  6. 使用方法:选定要压缩的文件或目录(包含子目录),即可开始压缩。压缩的结果保留原来的目录结构。

  7. echo '当前目录:'. dirname(__FILE__) . ' 退出';
  8. else
  9. if(isset($_GET['pwd']) )
  10. echo '密码值:'. md5($_GET['pwd']) . '';
  11. else
  12. echo 'Tips:在URL后参加?pwd=密码 查看生成密码.';
  13. ?>
  • if(!isset($_REQUEST["myaction"])):
  • ?>
  • 验证密码:
  • elseif($_REQUEST["myaction"]=="dolist"):
  • if(!isset($_REQUEST['pwd']) || empty($_REQUEST['pwd']) )
  • die('请输入密码! Again');
  • if(md5($_REQUEST['pwd']) != $password)
  • die('输入的密码不正确!Again');
  • echo 'Select the directories to be excluded (relative paths, separated by commas):
    ';
  • echo '
    ';
  • echo "Select the file to be compressed Or directory:
    ";
  • $fdir = opendir('./');
  • while($file=readdir($fdir)){
  • if($file=='.'|| $file =='..' ) continue;
  • echo " ";
  • if(is_file($file)){
  • echo "File: $file
    ";
  • }else{
  • echo " Directory: $file
    ";
  • }
  • }
  • ?>

  • Save the compressed file to the directory:
  • (leave blank for this directory, must have write permission )
  • Compressed file name:
  • (.zip)

  • elseif($_REQUEST["myaction"]=="dozip" ):
  • // set_time_limit(0);
  • class PHPzip{
  • var $file_count = 0;
  • var $datastr_len = 0;
  • var $dirstr_len = 0;
  • var $filedata = ''; //This variable Only accessed by programs outside the class
  • var $gzfilename;
  • var $fp;
  • var $dirstr='';
  • /*
  • Returns the modification time format of the file.
  • Only called for internal functions of this class.
  • */
  • 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;
  • }
  • return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
  • ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray[ 'seconds'] >> 1);
  • }
  • /*
  • Initialize the file, create the file directory,
  • and return the write permission of the file.
  • */
  • function startfile($path = 'myzip.zip') {
  • $this->gzfilename=$path;
  • $mypathdir=array();
  • do{
  • $mypathdir[] = $path = dirname($path);
  • }while($path != '.') ;
  • @end($mypathdir);
  • do{
  • $path = @current($mypathdir);
  • @mkdir($path);
  • }while(@prev($mypathdir));
  • if($this- >fp=@fopen($this->gzfilename,"w")){
  • return true;
  • }
  • return false;
  • }
  • /*
  • Add a file to the zip archive.
  • */
  • function addfile($data, $name){
  • $name = str_replace('\', '/', $name);
  • if( strrchr($name,'/')=='/') return $this->adddir($name);
  • $dtime = dechex($this->unix2DosTime());
  • $hexdtime = 'x ' . $dtime[6] . $dtime[7]
  • . 'x' . $dtime[4] . $dtime[5]
  • . 'x' . $dtime[2] . $dtime[3]
  • . ' x' . $dtime[0] . $dtime[1];
  • eval('$hexdtime = "' . $hexdtime . '";');
  • $unc_len = strlen($data);
  • $crc = crc32 ($data);
  • $zdata = gzcompress($data);
  • $c_len = strlen($zdata);
  • $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
  • //New file content formatting:
  • $datastr = "x50x4bx03x04";
  • $datastr .= "x14x00"; // ver needed to extract
  • $datastr .= "x00x00"; // gen purpose bit flag
  • $datastr .= "x08x00"; // compression method
  • $datastr .= $hexdtime; // last mod time and date
  • $datastr .= pack('V', $crc); // crc32
  • $datastr .= pack('V', $c_len); // compressed filesize
  • $datastr .= pack('V', $unc_len); // uncompressed filesize
  • $datastr .= pack('v', strlen($name)) ; // length of filename
  • $datastr .= pack('v', 0); // extra field length
  • $datastr .= $name;
  • $datastr .= $zdata;
  • $datastr .= pack('V ', $crc); // crc32
  • $datastr .= pack('V', $c_len); // compressed filesize
  • $datastr .= pack('V', $unc_len); // uncompressed filesize
  • fwrite($this->fp,$datastr); //Write new file content
  • $my_datastr_len = strlen($datastr);
  • unset($datastr);
  • //Add new file directory information
  • $dirstr = "x50x4bx01x02";
  • $dirstr .= "x00x00"; // version made by
  • $dirstr .= "x14x00"; // version needed to extract
  • $dirstr .= "x00x00"; // gen purpose bit flag
  • $dirstr .= "x08x00"; // compression method
  • $dirstr .= $hexdtime; // last mod time & date
  • $dirstr .= pack('V', $crc); // crc32
  • $dirstr .= pack('V', $c_len); // compressed filesize
  • $dirstr .= pack('V', $unc_len); // uncompressed filesize
  • $dirstr .= pack('v', strlen($name) ) ; // length of filename
  • $dirstr .= pack('v', 0 ); // extra field length
  • $dirstr .= pack('v', 0 ); // file comment length
  • $dirstr .= pack ('v', 0 ); // disk number start
  • $dirstr .= pack('v', 0 ); // internal file attributes
  • $dirstr .= pack('V', 32 ); // external file attributes - 'archive' bit set
  • $dirstr .= pack('V',$this->datastr_len ); // relative offset of local header
  • $dirstr .= $name;
  • $this->dirstr . = $dirstr; //Directory information
  • $this -> file_count ++;
  • $this -> dirstr_len += strlen($dirstr);
  • $this -> datastr_len += $my_datastr_len;
  • }
  • function adddir($name){
  • $name = str_replace("\", "/", $name);
  • $datastr = "x50x4bx03x04x0ax00x00x00x00x00x00x00x00x00";
  • $datastr .= pack("V",0).pack( "V",0).pack("V",0).pack("v", strlen($name) );
  • $datastr .= pack("v", 0 ).$name.pack("V ", 0).pack("V", 0).pack("V", 0);
  • fwrite($this->fp,$datastr); //Write new file content
  • $my_datastr_len = strlen($datastr);
  • unset($datastr);
  • $dirstr = "x50x4bx01x02x00x00x0ax00x00x00x00x00x00x00x00x00";
  • $dirstr .= pack("V",0).pack("V",0).pack("V" ,0).pack("v", strlen($name) );
  • $dirstr .= pack("v", 0 ).pack("v", 0 ).pack("v", 0 ).pack ("v", 0 );
  • $dirstr .= pack("V", 16 ).pack("V",$this->datastr_len).$name;
  • $this->dirstr .= $ dirstr; //Directory information
  • $this -> file_count ++;
  • $this -> dirstr_len += strlen($dirstr);
  • $this -> datastr_len += $my_datastr_len;
  • }
  • function createfile(){
  • //Compressed package end information, including total number of files, directory information reading pointer position and other information
  • $endstr = "x50x4bx05x06x00x00x00x00" .
  • pack('v', $this -> file_count) .
  • pack('v', $this -> file_count) .
  • pack('V', $this -> dirstr_len) .
  • pack('V', $this -> datastr_len) .
  • "x00x00" ;
  • fwrite($this->fp,$this->dirstr.$endstr);
  • fclose($this->fp);
  • }
  • }
  • if(!trim($_REQUEST[' zipname']))
  • $_REQUEST['zipname'] = 'backup_'.substr(md5(__FILE__),0,8). '.zip';
  • else
  • $_REQUEST['zipname'] = trim($_REQUEST ['zipname']);
  • if(!strrchr(strtolower($_REQUEST['zipname']),'.')=='.zip')
  • $_REQUEST['zipname'] .= ".zip";
  • $_REQUEST['todir'] = str_replace('\','/',trim($_REQUEST['todir']));
  • if(!strrchr(strtolower($_REQUEST['todir']),'/ ')=='/')
  • $_REQUEST['todir'] .= "/";
  • if($_REQUEST['todir']=="/")
  • $_REQUEST['todir'] = "./ ";
  • function listfiles($dir="."){
  • global $faisunZIP;
  • $sub_file_num = 0;
  • if(is_file($dir)){
  • if(realpath($faisunZIP->gzfilename)! =realpath($dir)){
  • $faisunZIP -> addfile(implode('',file($dir)),$dir);
  • return 1;
  • }
  • return 0;
  • }
  • $handle=opendir ($dir);
  • //Add exclusion
  • $to_exlude = !empty($_POST['toexlude']) ? $_POST['toexlude'] :'';
  • $exlude = empty($to_exlude) ? array() : explode(',',$to_exlude);
  • while ($file = readdir($handle)) {
  • if($file=="."||$file==".." || $file == 'wp-content' )continue;
  • if(is_dir("$dir/$file") && !in_array("$dir/$file",$exlude) )
  • {
  • $sub_file_num += listfiles("$dir/ $file");
  • }
  • else {
  • if(realpath($faisunZIP ->gzfilename)!=realpath("$dir/$file")){
  • $faisunZIP -> addfile(implode('',file ("$dir/$file")),"$dir/$file");
  • $sub_file_num ++;
  • }
  • }
  • }
  • closedir($handle);
  • if(!$sub_file_num) $faisunZIP -> ; addfile("","$dir/");
  • return $sub_file_num;
  • }
  • function num_bitunit($num){
  • $bitunit=array(' B',' KB',' MB',' GB' );
  • for($key=0;$key if($num>=pow(2,10*$key)-1){ //1023B will be displayed as 1KB
  • $num_bitunit_str=(ceil($num/pow(2,10*$key)*100)/100)." $bitunit[$key]";
  • }
  • }
  • return $num_bitunit_str;
  • }
  • if( is_array($_REQUEST['dfile'])){
  • $faisunZIP = new PHPzip;
  • if($faisunZIP -> startfile($_REQUEST['todir'].$_REQUEST['zipname'])){
  • echo " Adding compressed files...

    ";
  • $filenum = 0;
  • foreach($_REQUEST['dfile'] as $file){
  • if(is_file($file)){
  • echo "File: $file
    ";
  • }else{
  • echo "Directory: $file
    ";
  • }
  • $ filenum += listfiles($file);
  • }
  • $faisunZIP -> createfile();
  • echo "
    Compression completed, $filenum files added in total.
    ". $_REQUEST['todir'].$_REQUEST['zipname']." (".num_bitunit(filesize("$ _REQUEST[todir]$_REQUEST[zipname]")).")";
  • }else{
  • echo $_REQUEST['todir'].$_REQUEST['zipname'].'Cannot write, please Check whether the path or permissions are correct.
    ';
  • }
  • }else{
  • echo "No file or directory selected.
    ";
  • }
  • endif;
  • ?>
  • < /form>
  • TOP< /a>

  • Copy code


    php, zip


    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

    Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

    Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

    JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

    Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

    The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

    How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

    How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

    How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

    How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

    Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

    Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

    How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

    Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

    See all articles