文件压缩、解压工具类。文件压缩格式为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 ...
随机推荐
- enums应用详解
枚举类: 获取枚举相关值:
- STM32Cube IDE 汉字字体变小解决办法
用STM32Cube IDE自动生成的工程,如果用汉字注释的话,字体会变小,如下图: 解决方法:选中变小的汉字->右击选择Preferences,如下图: 在弹出的对话框中可以看出默认的字体是C ...
- java数据结构5--集合Map
Map Map与Collection在集合框架中属并列存在 Map存储的是键值对<K,V> Map存储元素使用put方法,Collection使用add方法 Map集合没有直接取出元素的方 ...
- 任意修改网页内容JS代码
浏览器输入框执行,chrome需要粘贴后,需要在前面手打javascript: 因为粘贴的会自动过滤 javascript:document.body.contentEditable='true'; ...
- 34 String、StringBuffer、StringBuilder
String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间. StringBuffer是可变类,和线程安全的字符串操作类,任何对 ...
- layui静态表格固定td宽度,table固定td宽度
正在做一个项目,要求数据表的列是不固定的,有可能是有10列,有可能是20列,第一列宽度要固定,然后我怎么设置都没有用, 这个问题困扰了我三天,后来终于百度到了, 这个博客: https://www.c ...
- 两句话掌握 Python 最难知识点——元类
千万不要被所谓“元类是99%的python程序员不会用到的特性”这类的说辞吓住.因为每个中国人,都是天生的元类使用者 学懂元类,你只需要知道两句话: 道生一,一生二,二生三,三生万物 我是谁?我从哪来 ...
- forEach、map、filter、reduce的区别
1.相同点: 都会循环遍历数组中的每一项: map().forEach()和filter()方法里每次执行匿名函数都支持3个参数,参数分别是:当前元素.当前元素的索引.当前元素所属的数组: 匿名函数中 ...
- XML to HTML
本章讲解如何把 XML 数据显示为 HTML. 在 HTML 中显示 XML 数据 在上一节中,我们讲解了如何通过 JavaScript 来解析 XML 并访问 DOM. 本例遍历一个 XML 文件 ...
- golang rabbitmq实践 (二 实现简单的消息收发)
1:驱动 本来打算自己写一个驱动的,后来发现github上面已经有了,那我就直接拿现成的了, 驱动采用 github.com/streadway/amqp ,直接import就可以啦! 2:excha ...