<?php
use ZipArchive; class Zip
{
/**
* @var array $files 需要压缩的文件或文件夹
*/
public $files = []; /**
* 排除的文件
*/
public $notFile = []; /**
* 压缩或者解压密码
*/
public $passowrd = null; //压缩包名字及输出地址
public $zipName = 'package.zip'; //检测的根目录,默认为APP根目录
public $rootPath = AR; private function addDir($folder, $zipFile, $rootPath, $folderSub = null)
{
// $folder = $this->rootPath . $folderb;
if (is_dir($folder)) {
$handle = opendir($folder);
while (false !== $f = readdir($handle)) {
if ($f != '.' && $f != '..') {
// Remove prefix from file path before add to zip.
$localPath = substr($filePath, $rootPath);
if (is_file($filePath)) {
$this->addFile($filePath, $localPath, $zipFile);
} elseif (is_dir($filePath)) {
// Add sub-directory.
$zipFile->addEmptyDir($localPath);
$this->addDir($filePath, $zipFile, $rootPath, $folderSub);
}
}
}
closedir($handle);
} else {
$this->addFile($folder, $folderSub, $zipFile);
}
} private function addFile($filePath, $localPath, $zipFile)
{
$zipFile->addFile($filePath, $localPath);
if ($this->passowrd) {
$zipFile->setEncryptionName($localPath, ZipArchive::EM_AES_256);
}
} /**
* 打包成ZIP
*/
public function zip()
{
$zip = new ZipArchive();
$zip->open(AR . $this->zipName, ZIPARCHIVE::CREATE);
if ($this->passowrd) {
$zip->setPassword($this->passowrd);
}
foreach ($this->files as $row) {
$pathInfo = pathinfo($this->rootPath . $row);
is_dir($this->rootPath . $row) ? $zip->addEmptyDir($pathInfo['basename']) : '';
$z = $this->addDir($this->rootPath . $row, $zip, strlen($pathInfo['dirname'] . '/'), $row);
}
foreach ($this->notFile as $row) {
$zip->deleteName($row);
}
$zip->close();
return $z;
} /**
* 解压压缩包
*/
public function uzip($file, $path)
{
$zip = new ZipArchive();
$this->passowrd ? $zip->setPassword($this->passowrd) : '';
$zip->open($file);
$zip->extractTo($path);
$zip->close();
}
}

为防止网络爬虫,已删除关键代码,如有需要请发送博客地址到下边提示的邮箱里

PHP使用ZipArchive压缩、解压缩、加密压缩包等的更多相关文章

  1. Linux下的压缩解压缩命令详解

    linux zip命令zip -r myfile.zip ./*将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzipunzip -o - ...

  2. Qt之QuaZIP(zip压缩/解压缩)

    简述 QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库.适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它工具打开 ...

  3. Linux/centos/redhat下各种压缩解压缩方式详解

    1.zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip -o -d ...

  4. Linux下的常见压缩解压缩命令

    Linux常见压缩解压缩命令 常见压缩文件扩展名 .Z compress 程序压缩的文件: .zip zip 程序压缩的文件: .gz gzip 程序压缩的文件: .bz2 bzip2 程序压缩的文件 ...

  5. shutil 拷贝 / 移动 / 压缩 / 解压缩

    # shutil_demo.py 高级文件操作(拷贝 / 移动 / 压缩 / 解压缩) import shutil def shutil_demo(): # 拷贝文件 shutil.copy2('fi ...

  6. Linux下的压缩解压缩命令详解及实例

    实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...

  7. tar、7z(7zip)压缩/解压缩指令的使用

    本文介绍tar.7z指令的使用方法 tar指令 在Linux中,使用的最多的压缩/解压缩指令就是tar指令了. tar指令用来将多个文件/目录结构打包.在实际使用中,往往使用tar对压缩的支持,即同时 ...

  8. 【13】MD5编码、Zlib压缩解压缩

    1.MD5加密 /// <summary> /// 使用MD5加密算法 /// </summary> /// <param name="md5MessageSt ...

  9. Linux的压缩/解压缩文件处理 zip & unzip

    Linux的压缩/解压缩命令详解及实例 压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip 另:有些服 ...

  10. tar压缩解压缩

    Linux下的tar压缩解压缩命令详解tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以 ...

随机推荐

  1. 【MAUI】为 Label、Image 等控件添加点击事件

    一.前言 已经习惯了 WPF.WinForm 中"万物皆可点击"的方式. 但是在 MAUI 中却不行了. 在 MAUI 中,点击.双击的效果,是需要通过"手势识别器&qu ...

  2. 更好的Android多线程下载框架

    /** * 作者:Pich * 原文链接:http://me.woblog.cn/ * QQ群:129961195 * Github:https://github.com/lifengsofts */ ...

  3. NC17857 起床困难综合症

    NC17857 起床困难综合症 题目 题目描述 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争 ...

  4. 021(Keywords Search)(AC自动机)

    题目:http://ybt.ssoier.cn:8088/problem_show.php?pid=1479 题目思路:一道AC自动机的模板题 备注:还不会字典树和KMP的尽早回去重修 如果让你在一篇 ...

  5. eclipse调用MySQL数据库的方法

    今天来总结一下使用如何使用eclipse调用MySQL数据库的数据. 一.设置eclipse 我们首先来设置一下eclipse. 在下部的Servers中右键选择new,选择server 之后在新弹出 ...

  6. shell脚本函数及数组

    函数介绍: 函数function是由若干条shell命令组成的语句块,实现代码重用和模块话编程. 它与shell程序形式上是相似的,不同的是它不是一个单独的进程,不能独立运行,而是shell程序的一部 ...

  7. odoo14 button 事件调用python方法如何传递参数

    1 <field name="user_ids" 2 mode="kanban" 3 nolabel="1" 4 options=&q ...

  8. Serverless之Knative部署应用实例;

    1.什么是Knative? Knative是Google2018的Google Cloud Next大会上发布的一款基于kubernetes的Serverless框架. knative的目的是在kub ...

  9. 8. 利用Ansible快速构建MGR | 深入浅出MGR

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 目录 1. 安装ansbile 2. 配置ansible 3. 建立ssh信任 4. 测试ansible 5. 使用ans ...

  10. Spring源码 21 Bean生命周期

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...