文件压缩、解压工具类。文件压缩格式为zip
package com.JUtils.file; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; /**
* 文件压缩、解压工具类。文件压缩格式为zip
*
* @Author:chenssy
* @date:2016年5月24日 下午9:16:01
*/
public class ZipUitls {
/** 文件后缀名 */
private static final String ZIP_FILE_SUFFIX = ".zip"; /**
* 压缩文件
*
* @author:chenssy
* @date : 2016年5月24日 下午9:56:36
*
* @param :resourcePath
* 源文件
* @param :targetPath
* 目的文件,保存文件路径
*/ public static void main(String[] args) {
String fileName = "D:/file/compare.json";
String file = "D:/file/ZipUitls";
zipFile(fileName,file);
}
public static void zipFile(String resourcePath,String targetPath){
File resourcesFile = new File(resourcePath);
File targetFile = new File(targetPath); //目的文件不存在,则新建
if(!targetFile.exists()){
targetFile.mkdirs();
}
//文件名
String targetName = resourcesFile.getName() + ZIP_FILE_SUFFIX; ZipOutputStream out = null;
try {
FileOutputStream outputStream = new FileOutputStream(targetPath+"//"+targetName);
out = new ZipOutputStream(new BufferedOutputStream(outputStream)); compressedFile(out, resourcesFile, "");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
*
* @author:chenssy
* @date : 2016年5月24日 下午10:00:22
*
* @param out
* @param : resourcesFile
* @param dir
*/
private static void compressedFile(ZipOutputStream out, File file, String dir) {
FileInputStream fis = null;
try {
if (file.isDirectory()) { //文件夹
// 得到文件列表信息
File[] files = file.listFiles();
// 将文件夹添加到下一级打包目录
out.putNextEntry(new ZipEntry(dir + "/")); dir = dir.length() == 0 ? "" : dir + "/"; // 循环将文件夹中的文件打包
for (int i = 0; i < files.length; i++) {
compressedFile(out, files[i], dir + files[i].getName()); // 递归处理
}
} else { //如果是文件则打包处理
fis = new FileInputStream(file); out.putNextEntry(new ZipEntry(dir));
// 进行写操作
int j = 0;
byte[] buffer = new byte[1024];
while ((j = fis.read(buffer)) > 0) {
out.write(buffer, 0, j);
}
// 关闭输入流
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件压缩、解压工具类。文件压缩格式为zip的更多相关文章
- GZip 压缩解压 工具类 [ GZipUtil ]
片段 1 片段 2 pom.xml <dependency> <groupId>commons-codec</groupId> <artifactId> ...
- Linux打包压缩解压工具
第1章 Linux 打包压缩解压工具一.压缩.解压工具 compress/uncompress gzip/gunzip bzip2/bunzip2/ bzcat xz/unxz/ xzcat ...
- 使用SharpZIpLib写的压缩解压操作类
使用SharpZIpLib写的压缩解压操作类,已测试. public class ZipHelper { /// <summary> /// 压缩文件 /// </summary&g ...
- Java_压缩与解压工具类
转载请注明出处:http://blog.csdn.net/y22222ly/article/details/52201675 zip压缩,解压 zip压缩与解压主要依靠java api的两个类: Zi ...
- zip文件解压工具类
java解压zip文件 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...
- Zip包解压工具类
最近在做项目的自动检测离线升级,使用到了解压zip包的操作,本着拿来主义精神,搞了个工具类(同事那边拿的),用着还不错. package com.winning.polaris.admin.utils ...
- linux下tar压缩/解压的使用(tar) 压缩/解压
压缩: tar -zcvf 压缩后文件名.tar.gz 被压缩文件 解压: tar -zxvf 被解压文件 具体的可以在linux环境下 用 tar --help 查看详细说明格式:ta ...
- js压缩解压工具
参看下面链接:http://js.clicki.cc/
- .NET使用ICSharpCode.SharpZipLib压缩/解压文件
SharpZipLib是国外开源加压解压库,可以方便的对文件进行加压/解压 1.下载ICSharpCode.SharpZipLib.dll,并复制到bin目录下 http://www.icsharpc ...
随机推荐
- Java并发编程实战 第10章 避免活跃性危险
死锁 经典的死锁:哲学家进餐问题.5个哲学家 5个筷子 如果没有哲学家都占了一个筷子 互相等待筷子 陷入死锁 数据库设计系统中一般有死锁检测,通过在表示等待关系的有向图中搜索循环来实现. JVM没有死 ...
- maven参数详解
setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...
- JDK的二进制安装
JDK的二进制安装步骤 1. Jdk1.8二进制包下载路径http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads- ...
- while循环练习:
输入姑娘的年龄后,进行以下判断: 如果姑娘小于18岁,打印"不接受未成年" 如果姑娘大于18岁小于25岁,打印"心动表白" 如果姑娘大于25岁小于45岁,打印& ...
- 前端每日实战:158# 视频演示如何用纯 CSS 创作一个雨伞 toggle 控件
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/pxLbjv 可交互视频 此视频是可 ...
- 测试工具Telerik Test Studio发布R2 2019|支持VS 2019
Telerik Test Studio是一个用于功能性Web.桌面和移动测试的直观测试自动化工具,它能轻松地实现自动化测试.同时会为GUI.性能.加载和API测试提供完整的自动化测试解决方案. |更多 ...
- 集合操作符 Union / Union All / Intersect / Minus
集合操作符 Union / UnionAll / Intersect / Minus -- 生成测试数据 create table dept_01 as select * from dept wher ...
- NOIP2016提高A组模拟10.15总结
第一题,就是将原有的式子一步步简化,不过有点麻烦,搞了很久. 第二题,枚举上下边界,维护一个单调队列,二分. 比赛上没有想到,只打了个暴力,坑了80分. 第三题,贪心,最后的十多分钟才想到,没有打出来 ...
- 配置MongoDB的Windows服务
[1] 创建directorys和files Create a configuration file and a directory path for MongoDB log output (logp ...
- [jvm学习笔记]-类加载过程
JVM类加载的过程 加载=>验证=>准备=>解析=>初始化 5个阶段所执行的具体动作 加载 在加载阶段,虚拟机需要完成3个事情1.通过一个类的全限定名获取定义此类的二进制字节流 ...