Home>Article>Backend Development> How to implement native zip testing in php (pure code)

How to implement native zip testing in php (pure code)

不言
不言 Original
2018-08-03 11:19:53 1483browse

This article introduces you to the article about how to implement native zip testing (pure code) in PHP. It has a good reference value and I hope it can help friends in need.

//简单测试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 ['dirname'] = rtrim(substr($filePath, 0, strrpos($filePath, '/')), "/") . "/"; $path_parts ['basename'] = ltrim(substr($filePath, strrpos($filePath, '/')), "/"); $path_parts ['extension'] = substr(strrchr($filePath, '.'), 1); $path_parts ['filename'] = ltrim(substr($path_parts ['basename'], 0, strrpos($path_parts ['basename'], '.')), "/"); return $path_parts; }

Recommended related articles:

PHP implements the code for writing LOG logs

Naming rules for php variables and php variables Usage (with code)

The above is the detailed content of How to implement native zip testing in php (pure code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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