本类实现参考 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. 30行python让图灵机器人和茉莉机器人无止尽的瞎扯蛋

    首先注册申请图灵机器人的API: http://www.tuling123.com/ 查看一下API的格式,很简单: { "key": "APIKEY", &q ...

  2. poj 2251 Dungeon Master (BFS 三维)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  3. json与cjson

    json安装: http://blog.csdn.net/u011641885/article/details/46755225 tar xvf json-c-0.9.tar.gz cd json-c ...

  4. Kaggle:Home Credit Default Risk 特征工程构建及可视化(2)

    博主在之前的博客 Kaggle:Home Credit Default Risk 数据探索及可视化(1) 中介绍了 Home Credit Default Risk 竞赛中一个优秀 kernel 关于 ...

  5. 如何在Ubuntu/Linux内使用 zip 压缩隐藏文件?

    假设你的当前目录结构为: A文件--B文件夹--C文件等 | | D文件夹-E文件 | | 等等文件(含隐藏) 那么在使用zip命令时加上 -r 即可包含整个目录结构和隐藏文件: zip -r B.z ...

  6. Python之路,第九篇:Python入门与基础9

    python3  集合set 集合set概念 集合是可变的容器:满足数学意义上的定义,求并集交集等 集合内的数据对象都是唯一的(不能重复多次) 集合是无序的存储结构,集合中的数据没有先后关系 集合是相 ...

  7. Unity 灯光探针用法教程

    Unity 灯光探针用详解 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...

  8. linux最常用的20个命令

    玩过Linux的人都会知道,Linux中的命令的确是非常多,但是玩过Linux的人也从来不会因为Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的命令就可以了.当然你也可以在使用时去找一下 ...

  9. SEO:网站优化内容

    一.内部优化 (1)meta标签优化:例如:TDK等的优化:        首页:网站名称 或者 网站名称_提供服务介绍or产品介绍 .        频道页:频道名称_网站名称.        文章 ...

  10. IIS 7上导入SSL证书

    1.将SSL证书文件上传至服务器 2.导入证书 1)在IIS设置项中,打开‘服务器证书’ 2)选择导入 3)将网站绑定SSL证书