本类实现参考 php manual 评论

[php] view plain copy
/**
* function: 解压zip 格式的文件
* author:friker
* date:2015-15-14
* reference:http://php.net/manual/zh/ref.zip.php
* all rights reserved:wujiangwei123@126.com
*/ class Unzip{ public function __construct(){
//init code here...
header("content-type:text/html;charset=utf8");
} /**
* 解压文件到指定目录
*
* @param string zip压缩文件的路径
* @param string 解压文件的目的路径
* @param boolean 是否以压缩文件的名字创建目标文件夹
* @param boolean 是否重写已经存在的文件
*
* @return boolean 返回成功 或失败
*/
public function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true){ if ($zip = zip_open($src_file)){
if ($zip){
$splitter = ($create_zip_name_dir === true) ? "." : "/";
if($dest_dir === false){
$dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
} // 如果不存在 创建目标解压目录
$this->create_dirs($dest_dir); // 对每个文件进行解压
while ($zip_entry = zip_read($zip)){
// 文件不在根目录
$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
if ($pos_last_slash !== false){
// 创建目录 在末尾带 /
$this->create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
} // 打开包
if (zip_entry_open($zip,$zip_entry,"r")){ // 文件名保存在磁盘上
$file_name = $dest_dir.zip_entry_name($zip_entry); // 检查文件是否需要重写
if ($overwrite === true || $overwrite === false && !is_file($file_name)){
// 读取压缩文件的内容
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); @file_put_contents($file_name, $fstream);
// 设置权限
chmod($file_name, 0777);
echo "save: ".$file_name."<br />";
} // 关闭入口
zip_entry_close($zip_entry);
}
}
// 关闭压缩包
zip_close($zip);
}
}else{
return false;
}
return true;
} /**
* 创建目录
*/
public function create_dirs($path){
if (!is_dir($path)){
$directory_path = "";
$directories = explode("/",$path);
array_pop($directories); foreach($directories as $directory){
$directory_path .= $directory."/";
if (!is_dir($directory_path)){
mkdir($directory_path);
chmod($directory_path, 0777);
}
}
}
} } /*
using:
$z = new Unzip();
$z->unzip("./bootstrap-3.3.4.zip",'./unzipres/', true, false);
*/

PHP 解压 ZIP 文件到指定文件夹的更多相关文章

  1. apache ant解压zip。支持多级文件夹解压

    package cn.liuc.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcepti ...

  2. 使用Python解压zip、rar文件

    解压 zip 文件 基本解压操作 import zipfile ''' 基本格式:zipfile.ZipFile(filename[,mode[,compression[,allowZip64]]]) ...

  3. Android 解压zip文件你知道多少?

    对于Android常用的压缩格式ZIP,你了解多少? Android的有两种解压ZIP的方法,你知道吗? ZipFile和ZipInputStream的解压效率,你对比过吗? 带着以上问题,现在就开始 ...

  4. (转载)C#压缩解压zip 文件

    转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...

  5. java实现解压zip文件,(亲测可用)!!!!!!

    项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...

  6. AIX解压ZIP文件

    AIX系统自身是没有解压ZIP文件的,但在AIX安装oracle数据库服务器的话,在$ORACLE_HOME/bin路径下方却有unzip命令,可以解压ZIP文件. 一.shell脚本   之前的版本 ...

  7. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天

    PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...

  8. 【VC++技术杂谈008】使用zlib解压zip压缩文件

    最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...

  9. 通过javascript在网页端解压zip文件并查看压缩包内容

    WEB前端解压ZIP压缩包 web前端解压zip文件有什么用: 只考虑标准浏览器的话, 服务器只要传输压缩包到客户端, 节约了带宽, 而且节约了传输时间, 听起来好像很厉害的说:     如果前端的代 ...

随机推荐

  1. 自动化测试-20.selenium之FireFox下载项配置

    前言: 当我们在使用Selenium运行自动化测试时,偶尔需要用到下载功能,但浏览器的下载可能会弹出下载窗口,或者下载路径不是我们想要保存的位置,所以在通过Selenium启动浏览器时需要做相关的设置 ...

  2. swift3.0 简单直播和简单网络音乐播放器

    本项目采用swift3.0所写,适配iOS9.0+,所有界面均采用代码布局. 第一个tab写的是简单直播,传统MVC模式,第二个tab写的是简单网络音乐播放器.传说MVVM模式(至于血统是否纯正我就不 ...

  3. 实验楼 Linux 基础入门(新版)挑战:寻找文件

    传送门:https://www.shiyanlou.com/courses/running 挑战:寻找文件 实验环境: 用户名:shiyanlou 密码:76036575 寻找文件 介绍 有一个非常重 ...

  4. Android Hook框架adbi源码浅析(一)

    adbi(The Android Dynamic Binary Instrumentation Toolkit)是一个Android平台通用hook框架,基于动态库注入与inline hook技术实现 ...

  5. 【leetcode】20-ValidParentheses

    problem Valid Parentheses code class Solution { public: bool isValid(string s) { stack<char> p ...

  6. 基于Hexo+Node.js+github+coding搭建个人博客——基础篇

    附上个人教程:http://www.ookamiantd.top/2017/build-blog-hexo-base/ 搭建此博客的动机以及好处在此就不多谈了,之前已经表达过,详情请看Start My ...

  7. K - FatMouse and Cheese

    最近一直在写dp,然后别的就啥也不管了(wtcl),很明显的最简单的搜索题竟然卡了,一开始的思路是每一个格子都只能是从四周的格子转化过来的,只要找到四周最大的那个那么dp[i][j]=max+a[i] ...

  8. HPU组队赛B:问题(二进制枚举)

    时间限制1 Second 内存限制 512 Mb 题目描述 你有n个问题,你已经估计了第i个问题的难度为Ci,现在你想使用这些问题去构造一个问题集.比赛的问题集必须包含至少两个问题,而且比赛的总难度必 ...

  9. NetCore平台下使用RPC框架Hprose

    NetCore下使用RPC框架Hprose https://www.jianshu.com/p/c903fca44d5d Hprose是国内非常优秀的RPC框架,和其它RPC框架比较起来,其它框架一般 ...

  10. 《DSP using MATLAB》Problem 6.15

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...