Heim  >  Artikel  >  Backend-Entwicklung  >  So implementieren Sie native Zip-Tests in PHP (reiner Code)

So implementieren Sie native Zip-Tests in PHP (reiner Code)

不言
不言Original
2018-08-03 11:19:531493Durchsuche

Der Inhalt dieses Artikels befasst sich mit der Implementierung nativer Zip-Tests (reiner Code) in PHP. Er hat einen guten Referenzwert und ich hoffe, er kann Freunden in Not helfen.

//简单测试1
 public function zipTest(){
        $zip = new \ZipArchive();        
        $res = $zip->open('static/download/zip/test.zip',\ZipArchive::CREATE);        
        if ($res === TRUE) {           
         echo 'ok';            
         $zip->addFromString('test.txt', 'file content goes here');            
         //解压缩到test文件夹
            //$zip->extractTo('test');
            $zip->close();
        } else {            
        echo 'failed, code:' . $res;
        }
    }    //处理类class FilePackUtil{


    /**
     * 压缩成ZIP文件
     * @param array $file 文件数组
     * $file = array('E:/Resources/download.kekedj.com/mp3/XX/20180611/XXX/XXX.mp3', 'E:/Resources/download.kekedj.com/mp3/XX/20180611/XXX/XXX.mp3');
     * @param $name 存放名字.zip
     * @param $output 存放路径
     * @return bool|string
     */
    public static function zip($file = array(), $name, $output)
    {
        if(empty($file)) {            
        return "打包文件为空";
        }        
        if(empty($name)) {            return "请选择输出文件名";
        }        
        if(empty($output)) {            return "请选择输出目录";
        }        
        foreach ($file as $key => $value) {            
        $file [$key] = iconv("UTF-8", "gb2312//IGNORE", $value); //中文名转换
        }        
        if (! is_dir ( $output )) {
            mkdir ( $output, 0777 );
        }        
        $filename = iconv("UTF-8", "gb2312//IGNORE", $output . $name); // 中文名转换 最终生成的文件名以及服务器保存的路径
        if (!file_exists($filename)) {            
        //重新生成文件
            $zip = new ZipArchive (); //使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
            if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {                
            return '无法打开文件,或者文件创建失败。请检查路径、权限以及环境变量的配置';
            }            
            foreach ($file as $val) {                
            if (file_exists($val)) {                    
            $fileInfoArr = self::pathInfo($val);                    
            $zip->addFromString($fileInfoArr ['basename'], file_get_contents($val)); //中文使用这个
                }
            }            
            $zip->close(); //关闭
        }        
        if (!file_exists($filename)) {            
        return ("无法找到文件"); //即使创建,仍有可能失败。。。。
        }        
        return true;
    }    
    public static function remove($file)
    {
        if(!$file) return false;        
        $file = iconv("UTF-8", "gb2312//IGNORE", $file); // 中文名转换
        @unlink($file);
        exec("rm -rf " . $file);        
        return true;
    }    /**
     * 读取文件信息
     * @author ZhiyuanLi < 956889120@qq.com >
     * @param $filePath
     * @return array
     */
    private function pathInfo($filePath)
    {
        $path_parts = array();        
        $path_parts [&#39;dirname&#39;] = rtrim(substr($filePath, 0, strrpos($filePath, &#39;/&#39;)), "/") . "/";        
        $path_parts [&#39;basename&#39;] = ltrim(substr($filePath, strrpos($filePath, &#39;/&#39;)), "/");        
        $path_parts [&#39;extension&#39;] = substr(strrchr($filePath, &#39;.&#39;), 1);        
        $path_parts [&#39;filename&#39;] = ltrim(substr($path_parts [&#39;basename&#39;], 0, strrpos($path_parts [&#39;basename&#39;], &#39;.&#39;)), "/");        
        return $path_parts;
    }

Empfohlene verwandte Artikel:

PHP-Implementierungscode zum Schreiben von LOG-Protokollen

Benennungsregeln für PHP-Variablen und PHP-Variablen Verwendung (mit Code)

Das obige ist der detaillierte Inhalt vonSo implementieren Sie native Zip-Tests in PHP (reiner Code). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn