Java常用工具类之压缩解压
- package com.wazn.learn.util;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- import java.util.zip.ZipOutputStream;
- /**
- * 通过Java的Zip输入输出流实现压缩和解压文件
- *
- * @author yangzhenyu
- *
- */
- public final class ZipUtil {
- private ZipUtil() {
- // empty
- }
- /**
- * 压缩文件
- *
- * @param filePath
- * 待压缩的文件路径
- * @return 压缩后的文件
- */
- public static File zip(String filePath) {
- File target = null;
- File source = new File(filePath);
- if (source.exists()) {
- // 压缩文件名=源文件名.zip
- String zipName = source.getName() + ".zip";
- target = new File(source.getParent(), zipName);
- if (target.exists()) {
- target.delete(); // 删除旧的文件
- }
- FileOutputStream fos = null;
- ZipOutputStream zos = null;
- try {
- fos = new FileOutputStream(target);
- zos = new ZipOutputStream(new BufferedOutputStream(fos));
- // 添加对应的文件Entry
- addEntry("/", source, zos);
- } catch (IOException e) {
- throw new RuntimeException(e);
- } finally {
- IOUtil.closeQuietly(zos, fos);
- }
- }
- return target;
- }
- /**
- * 扫描添加文件Entry
- *
- * @param base
- * 基路径
- *
- * @param source
- * 源文件
- * @param zos
- * Zip文件输出流
- * @throws IOException
- */
- private static void addEntry(String base, File source, ZipOutputStream zos)
- throws IOException {
- // 按目录分级,形如:/aaa/bbb.txt
- String entry = base + source.getName();
- if (source.isDirectory()) {
- for (File file : source.listFiles()) {
- // 递归列出目录下的所有文件,添加文件Entry
- addEntry(entry + "/", file, zos);
- }
- } else {
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- try {
- byte[] buffer = new byte[1024 * 10];
- fis = new FileInputStream(source);
- bis = new BufferedInputStream(fis, buffer.length);
- int read = 0;
- zos.putNextEntry(new ZipEntry(entry));
- while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
- zos.write(buffer, 0, read);
- }
- zos.closeEntry();
- } finally {
- IOUtil.closeQuietly(bis, fis);
- }
- }
- }
- /**
- * 解压文件
- *
- * @param filePath
- * 压缩文件路径
- */
- public static void unzip(String filePath) {
- File source = new File(filePath);
- if (source.exists()) {
- ZipInputStream zis = null;
- BufferedOutputStream bos = null;
- try {
- zis = new ZipInputStream(new FileInputStream(source));
- ZipEntry entry = null;
- while ((entry = zis.getNextEntry()) != null
- && !entry.isDirectory()) {
- File target = new File(source.getParent(), entry.getName());
- if (!target.getParentFile().exists()) {
- // 创建文件父目录
- target.getParentFile().mkdirs();
- }
- // 写入文件
- bos = new BufferedOutputStream(new FileOutputStream(target));
- int read = 0;
- byte[] buffer = new byte[1024 * 10];
- while ((read = zis.read(buffer, 0, buffer.length)) != -1) {
- bos.write(buffer, 0, read);
- }
- bos.flush();
- }
- zis.closeEntry();
- } catch (IOException e) {
- throw new RuntimeException(e);
- } finally {
- IOUtil.closeQuietly(zis, bos);
- }
- }
- }
- }
Java常用工具类之压缩解压的更多相关文章
- JavaEE-实验一 Java常用工具类编程
该博客仅专为我的小伙伴提供参考而附加,没空加上代码具体解析,望各位谅解 1. 使用类String类的分割split 将字符串 “Solutions to selected exercises ca ...
- Java 压缩文件夹工具类(包含解压)
依赖jar <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons ...
- Java常用工具类整理
字符数组转String package com.sunsheen.hcc.fabric.utils; /** * 字符数组工具 * @author WangSong * */ public class ...
- Archiver 3 for Mac(解压缩工具) ,想压缩解压慢一点就这么难!
Archiver 3 for Mac是一款分割合并解压缩工具,简单实用且功能齐全,你只需简单的拖放文件就可以进行压缩,还可以设定解压密码,从而保护自己的隐私.如果文件很大你还可以切割文件.Archiv ...
- JAVA常用工具类汇总
一.功能方法目录清单: 1.getString(String sSource)的功能是判断参数是否为空,为空返回"",否则返回其值: 2.getString(int iSource ...
- Java常用工具类---XML工具类、数据验证工具类
package com.jarvis.base.util; import java.io.File;import java.io.FileWriter;import java.io.IOExcepti ...
- java常用工具类(二)
1.FtpUtil package com.itjh.javaUtil; import java.io.File; import java.io.FileOutputStream; import ja ...
- [转]Java常用工具类集合
转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...
- java常用工具类(java技术交流群57388149)
package com.itjh.javaUtil; import java.util.ArrayList; import java.util.List; /** * * String工具类. ...
随机推荐
- 无密码ssh登录linux
简介 ssh是常见的远程登录linux的方式,大部分时候需要输入用户名密码登录.本文介绍如何无密码登录linux,适用于mac和linux,windows不清楚. 不过这不是什么新的知识,基本上大家都 ...
- Mybatis批量删除之Error code 1064, SQL state 42000;
(一)小小的一次记载. (二):最近的项目都是使用MyBatis,批量新增自己都会写了,但是一次批量删除可把我给折腾了下,写法网上都有,但是照着做就是不行,最后问公司的人,问网友才得到答案,那就是jd ...
- NOIP模拟赛15
NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第一轮Day1 T1 天天去哪儿吃 直接枚举 #include<cstdio> #include<algorithm> using ...
- [NOIP2003]栈 题解(卡特兰数)
[NOIP2003]栈 Description 宁宁考虑的是这样一个问题:一个操作数序列,从1,2,一直到n(图示为1到3的情况),栈A的深度大于n. 现在可以进行两种操作: 1.将一个数,从操作数序 ...
- flex布局语法(阮一峰)
Flex 布局教程:语法篇 作者: 阮一峰 日期: 2015年7月10日 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + posi ...
- D - Keiichi Tsuchiya the Drift King Gym - 102028D (几何)
题目链接:https://cn.vjudge.net/contest/275150#problem/D 题目大意: 问你能满足那个矩形可以顺利通过的条件,然后求出最小的w 具体思路:首先,我们应该将情 ...
- GSON转换日期数据为特定的JSON数据
通过JSON传递数据的时候经常需要传递日期,Java中可以通过GSON将日期转换为特定格式的JSON数据. 1.普通的GSON转换日期 public void query(HttpServletReq ...
- 「caffe编译bug」.build_release/lib/libcaffe.so: undefined reference to cv::imread
转自:https://www.douban.com/note/568788483/ CXX/LD -o .build_release/tools/convert_imageset.bin.build_ ...
- <mvc:annotation-driven/>都做了那些事情
mvc:annotation-driven是一种简写的配置方式,那么mvc:annotation-driven到底做了哪些工作呢?如何替换掉mvc:annotation-driven呢? <mv ...
- Percona XtraDB Cluster(PXC) -集群环境安装
Percona XtraDB Cluster(PXC) ---服务安装篇 1.测试环境搭建: Ip 角色 OS PXC-version 172.16.40.201 Node1 Redhat/C ...