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. 解决:java compiler level does not match the version of the installed java project facet错误

    java compiler level does not match the version of the installed java project facet错误的解决 因工作的关系,Eclip ...

  2. vue2.0 监听滚动 锚点定位

    vue中监听滚动的方法其实可以用: // Chrome document.body.scrollTop // Firefox document.documentElement.scrollTop // ...

  3. Vue全局混入

    混入 (mixins) 是一种分发 Vue 组件中可复用功能的非常灵活的方式.混入对象可以包含任意组件选项.当组件使用混入对象时,所有混入对象的选项将被混入该组件本身的选项. 数据对象合并 数据对象在 ...

  4. leaflet 地图容器大小改变时,地图自适应新容器

    window.onload = function () { changeDivHeight(); } //当浏览器窗口大小改变时,设置显示内容的高度 window.onresize = functio ...

  5. shiro框架学习-1-shiro基本概念

    1. 什么是权限控制 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自己被授权的资源, ...

  6. C#与JAVA的互通

    C#与JAVA的互通 https://blog.csdn.net/sinat_35165183/article/details/68936910

  7. 工作笔记--js-点赞按钮和踩踩按钮互斥??怎么写?

    效果图: html: css: .an{ margin-top:0px; position: relative; .popzframe,.popcframe{ display: none; word- ...

  8. C# 指定索引排序 (原)

    private void test(string[] sortkey_list, string[] list_temp) { //打开excel到dt: " }; string[] roww ...

  9. const与#define的区别

    1.const (1)为什么需要const成员函数? C中常用:“ #define 变量名 变量值”定义一个值替代,然而却有个致命缺点:缺乏类型检测机制,这样预处理在C++中成为可能引发错误的隐患,于 ...

  10. TTTTTTTTTTTTTTTTTT POJ 1330

    题意:给一个有根树,一个查询节点(u,v)的最近公共祖先: #include <iostream> #include <cstdio> #include <cstring ...