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的更多相关文章

  1. GZip 压缩解压 工具类 [ GZipUtil ]

    片段 1 片段 2 pom.xml <dependency> <groupId>commons-codec</groupId> <artifactId> ...

  2. Linux打包压缩解压工具

    第1章      Linux 打包压缩解压工具一.压缩.解压工具 compress/uncompress gzip/gunzip bzip2/bunzip2/ bzcat xz/unxz/ xzcat ...

  3. 使用SharpZIpLib写的压缩解压操作类

    使用SharpZIpLib写的压缩解压操作类,已测试. public class ZipHelper { /// <summary> /// 压缩文件 /// </summary&g ...

  4. Java_压缩与解压工具类

    转载请注明出处:http://blog.csdn.net/y22222ly/article/details/52201675 zip压缩,解压 zip压缩与解压主要依靠java api的两个类: Zi ...

  5. zip文件解压工具类

    java解压zip文件 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...

  6. Zip包解压工具类

    最近在做项目的自动检测离线升级,使用到了解压zip包的操作,本着拿来主义精神,搞了个工具类(同事那边拿的),用着还不错. package com.winning.polaris.admin.utils ...

  7. linux下tar压缩/解压的使用(tar) 压缩/解压

    压缩: tar  -zcvf   压缩后文件名.tar.gz   被压缩文件 解压: tar  -zxvf  被解压文件 具体的可以在linux环境下 用 tar --help 查看详细说明格式:ta ...

  8. js压缩解压工具

    参看下面链接:http://js.clicki.cc/

  9. .NET使用ICSharpCode.SharpZipLib压缩/解压文件

    SharpZipLib是国外开源加压解压库,可以方便的对文件进行加压/解压 1.下载ICSharpCode.SharpZipLib.dll,并复制到bin目录下 http://www.icsharpc ...

随机推荐

  1. 微信支付之扫码支付、公众号支付、H5支付、小程序支付相关业务流程分析总结

    前言 很久以来,一直想写一篇微信支付有关的总结文档:一方面是总结自己的一些心得,另一方面也可以帮助别人,但是因种种原因未能完全理解透彻微信支付的几大支付方式,今天有幸做一些总结上的文章,也趁此机会,将 ...

  2. DRF介绍,DRF项目开发,DRF项目的视图类的dispatch源码解析

    目录 一.DRF介绍 1. 什么是DRF 2. 为什么要用DRF (1)使用DRF的原因 (2)站在开发者的角度来说用DRF的好处(暂时列举这么多) 二.用DRF开发后端项目 三.APIView请求生 ...

  3. 那些年我写过的mysql命令

    建表语句 #mysql5.7适用create table testfy ( id int primary key AUTO_INCREMENT, clipid int comment '影片编号', ...

  4. ESP8266-Soft AP模式 —— 谁想连上我

    AP是Access Point简称,也就是访问接入点,是网络的中心节点.一般家庭的无线路由器就是一个AP,众多站点(STA)加入到它所组成的无线网络,网络中的所有的通信都通过AP来转发完成. 软AP也 ...

  5. 【NOIP2016提高A组模拟9.24】总结

    第一题纯模拟,结果那个出题人脑子似乎进水了,空间限制开了1G!!! 导致我捉摸了半天为什么空间要开那么大,最后只能得出上面的结论. 第二题是个矩阵快速幂,比赛上我没把递推式求出来,但是根据各种乱搞,得 ...

  6. tensorflow conv2d

    https://www.cnblogs.com/qggg/p/6832342.html

  7. contenteditable 光标定位到最后

    在Vue做项目时,做了一个div[contenteditable=true]的组件作为文本输入框 在非手动输入值后,光标会丢失,经测试以下这段代码可用,直接将光标定位到最后 function keep ...

  8. C#调用Python(一)

    python文件中未引入其他包.模块 以下方法不适用于pyhton 文件有第三方包.模块,有第三方包,模块的实现方法,请戳这里→https://www.cnblogs.com/zhuanjiao/p/ ...

  9. spring mvc @Valid 数据验证

    //对书的单价校验不能是空,价格在20-100之间   @DecimalMax(value = "100", message = "价格不超过100元")   ...

  10. 分组统计 over(partition by

    sum( CASE WHEN ISNULL(b.zl, 0) = 0 THEN C.LLZL ELSE b.zl END * c.pccd * b.sl) over(partition by b.dj ...