PHP zip Compressed Package Operation Class Complete Instance

  • 2021-10-24 19:06:44
  • OfStack

This article describes the PHP zip compressed package operation class as an example. Share it for your reference, as follows:


<?php
/**
 * Zip  Package tool 
 *
 * @author wengxianhu
 * @date 2013-08-05
 */
class ZipFolder
{
  protected $zip;
  protected $root;
  protected $ignored_names;
  public function __construct(){
    $this->zip = new ZipArchive;
  }
  /**
   *  Decompression zip File to the specified folder 
   *
   * @access public
   * @param string $zipfile  Compressed file path 
   * @param string $path   The destination path to which the compressed packet is decompressed 
   * @return booleam  Successful decompression returns  true  Otherwise return  false
  */
  public function unzip ($zipfile, $path) {
    if ($this->zip->open($zipfile) === true) {
      $file_tmp = @fopen($zipfile, "rb");
      $bin = fread($file_tmp, 15); // Read-only 15 Byte   For different file types, header information does not 1 Sample. 
      fclose($file_tmp);
      /*  For only zip To process the compressed packet of  */
      if (true === $this->getTypeList($bin))
      {
        $result = $this->zip->extractTo($path);
        $this->zip->close();
        return $result;
      }
      else
      {
        return false;
      }
    }
    return false;
  }
  /**
   *  Create a compressed file 
   *
   * @access public
   * @param string $zipfile  Path to the compressed file to be generated 
   * @param strng $folder  Path to the folder to be compressed 
   * @param array $ignored  List of files to ignore 
   * @return booleam  Successful generation of compressed package returned true  Otherwise return  false
  */
   public function zip ($zipfile, $folder, $ignored = null) {
    $this->ignored_names = is_array($ignored) ? $ignored : $ignored ? array($ignored) : array();
    if ($this->zip->open($zipfile, ZIPARCHIVE::CREATE) !== true) {
      throw new Exception("cannot open <$zipfile>\n");
    }
    $folder = substr($folder, -1) == '/' ? substr($folder, 0, strlen($folder)-1) : $folder;
    if(strstr($folder, '/')) {
      $this->root = substr($folder, 0, strrpos($folder, '/')+1);
      $folder = substr($folder, strrpos($folder, '/')+1);
    }
    $this->createZip($folder);
    return $this->zip->close();
  }
  /**
   *  Recursively add files to compressed packages 
   *
   * @access private
   * @param string $folder  Folder path to add to zip package 
   * @param string $parent  Folder parent path added to compressed package 
   * @return void
  */
  private function createZip ($folder, $parent=null) {
    $full_path = $this->root . $parent . $folder;
    $zip_path = $parent . $folder;
    $this->zip->addEmptyDir($zip_path);
    $dir = new DirectoryIterator($full_path);
    foreach($dir as $file) {
      if(!$file->isDot()) {
        $filename = $file->getFilename();
        if(!in_array($filename, $this->ignored_names)) {
          if($file->isDir()) {
            $this->createZip($filename, $zip_path.'/');
          }else {
            $this->zip->addFile($full_path.'/'.$filename, $zip_path.'/'.$filename);
          }
        }
      }
    }
  }
  /**
   *  Read the list of compressed package files and directories 
   *
   * @access public
   * @param string $zipfile  Compressed package file 
   * @return array  File and Directory List 
  */
  public function fileList($zipfile) {
    $file_dir_list = array();
    $file_list = array();
    if ($this->zip->open($zipfile) == true) {
      for ($i = 0; $i < $this->zip->numFiles; $i++) {
        $numfiles = $this->zip->getNameIndex($i);
        if (preg_match('/\/$/i', $numfiles))
        {
          $file_dir_list[] = $numfiles;
        }
        else
        {
          $file_list[] = $numfiles;
        }
      }
    }
    return array('files'=>$file_list, 'dirs'=>$file_dir_list);
  }
  /**
  *  Get the mapping table of file header and file type 
  *
  * @author wengxianhu
  * @date 2013-08-10
  * @param $bin string  Documentary 2 Bimodal pre-bimodal 1 Segment character 
  * @return boolean
  */
  private function getTypeList ($bin)
  {
    $array = array(
      array("504B0304", "zip")
    );
    foreach ($array as $v)
    {
      $blen = strlen(pack("H*", $v[0])); // Get the number of bytes of file header tag 
      $tbin = substr($bin, 0, intval($blen)); /// You need to compare file header lengths 
      if(strtolower($v[0]) == strtolower(array_shift(unpack("H*", $tbin))))
      {
        return true;
      }
    }
    return false;
  }
}

For more readers interested in PHP related content, please check the topics on this site: "PHP Operation zip File and Compression Skills Summary", "php File Operation Summary", "PHP Basic Grammar Introduction Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php String (string) Usage Summary", "php+mysql Database Operation Introduction Tutorial" and "php Common Database Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: