转载https://blog.csdn.net/weixin_39723544/article/details/80611810

更多内容 转载https://blog.csdn.net/justry_deng/article/details/82846356

工具类

import java.io.*;
import java.util.zip.*; public class ZipUtils {
/**
* Default buff byte size
*
*/
private static final int DEFAULT_BUFF_SIZE = 1024; /**
* Default basedir value
*
*/
private static final boolean DEFAULT_DIR = false; public static void decompress(String srcPath) throws Exception {
decompress(new File(srcPath));
} public static void decompress(File srcFile) throws Exception {
File baseFile = srcFile.getParentFile();
decompress(srcFile, baseFile);
} public static void decompress(String srcPath, String destPath) throws Exception {
decompress(new File(srcPath), new File(destPath));
} public static void decompress(File srcFile, File destFile) throws Exception {
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32());
ZipInputStream zis = new ZipInputStream(cis);
doDecompress(destFile, zis);
zis.close();
} private static void doDecompress(File destFile, ZipInputStream zis) throws Exception {
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
String dir = destFile.getPath() + File.separator + zipEntry.getName();
File dirFile = new File(dir);
fileProber(dirFile);
if (zipEntry.isDirectory()) {
dirFile.mkdirs();
} else {
doDecompressFile(dirFile, zis);
}
zis.closeEntry();
}
} private static void doDecompressFile(File destFile, ZipInputStream zis) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int len;
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
while ((len = zis.read(buff, 0 ,DEFAULT_BUFF_SIZE)) != -1) {
bos.write(buff, 0, len);
}
bos.close();
} /**
* 文件探测
*
* When the parent file not exist.Create it.
*
* @param dirFile
* @throws Exception
*/
public static void fileProber(File dirFile) throws Exception {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
fileProber(parentFile);
parentFile.mkdirs();
}
} public static void compress(String srcPath, String destPath,boolean dirFlag) throws Exception {
compress(new File(srcPath), new File(destPath), dirFlag);
} public static void compress(String srcPath, String destPath) throws Exception {
compress(new File(srcPath), new File(destPath), DEFAULT_DIR);
} public static void compress(File srcFile, File destFile, boolean dirFlag) throws Exception {
compress(srcFile, new ZipOutputStream(new FileOutputStream(destFile)), dirFlag);
} public static void compress(File srcFile, ZipOutputStream zos, boolean dirFlag) throws Exception {
if (srcFile.isDirectory()) {
if (dirFlag) {
doCompress(zos, srcFile, srcFile.getName() + File.separator);
} else {
doCompress(zos, srcFile, "");
}
} else {
doCompress(zos, srcFile, "");
}
zos.close();
} public static void doCompress(ZipOutputStream zos, File file, String baseDir) throws Exception {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
doCompress(zos, files[i], baseDir);
}
} else {
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
In
putStream in = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(baseDir + File.separator + file.getName()));
int len;
while ((len = in.read(buff,0 ,DEFAULT_BUFF_SIZE)) != -1) {
zos.write(buff, 0, len);
}
in.close();
}
}
}
} 压缩
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; public class condense {
private static final boolean DEFAULT_DIR = true;
private static final int DEFAULT_BUFF_SIZE = 1024; // 压缩入口1
public static void compress(String srcPath, String destPath,boolean dirFlag) throws Exception {
compress(new File(srcPath), new File(destPath), dirFlag);
} // 压缩入口2
public static void compress(String srcPath, String destPath) throws Exception {
compress(new File(srcPath), new File(destPath), DEFAULT_DIR);
} // 压缩入口3
public static void compress(File srcFile, File destFile, boolean dirFlag) throws Exception {
compress(srcFile, new ZipOutputStream(new FileOutputStream(destFile)), dirFlag);
} public static void compress(File srcFile, ZipOutputStream zos, boolean dirFlag) throws Exception {
// 需要解压的压缩文件对象
// 压缩输出流
// 是否在压缩文件时创建一个父文件夹后再压缩
if (srcFile.isDirectory()) {
if (dirFlag) {
doCompress(zos, srcFile, srcFile.getName() + File.separator);
} else {
doCompress(zos, srcFile, "");
}
} else {
doCompress(zos, srcFile, "");
}
zos.close();
} public static void doCompress(ZipOutputStream zos, File file, String baseDir) throws Exception {
if (file.isDirectory()) {
// 递归循环,只压缩其中所有文件
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
doCompress(zos, files[i], baseDir);
}
} else {
// 进行文件压缩的操作
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
InputStream in = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(baseDir + File.separator + file.getName()));
int len;
while ((len = in.read(buff,0 ,DEFAULT_BUFF_SIZE)) != -1) {
zos.write(buff, 0, len);
}
in.close();
}
}
}
解压
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; public class decompress {
/**
* Default buff byte size
*/
private static final int DEFAULT_BUFF_SIZE = 1024; // 程序入口1
public static void decompress(String srcPath) throws Exception {
decompress(new File(srcPath));
} // 程序入口2
public static void decompress(File srcFile) throws Exception {
File baseFile = srcFile.getParentFile();
decompress(srcFile, baseFile);
} // 程序入口3
public static void decompress(String srcPath, String destPath) throws Exception {
decompress(new File(srcPath), new File(destPath));
} // 程序基本入口
public static void decompress(File srcFile, File destFile) throws Exception {
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32());
ZipInputStream zis = new ZipInputStream(cis);
// 解压操作
doDecompress(destFile, zis);
zis.close();
} private static void doDecompress(File destFile, ZipInputStream zis) throws Exception {
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
String dir = destFile.getPath() + File.separator + zipEntry.getName();
File dirFile = new File(dir);
// 如果父文件夹不存在,则递归创建其父文件夹
fileProbe(dirFile);
if (zipEntry.isDirectory()) {
// 如果zipEntry是目录,则创建目录
dirFile.mkdirs();
} else {
// 解压压缩文件的其中具体的一个zipEntry对象
doDecompressFile(dirFile, zis);
}
zis.closeEntry();
}
} // 一般意义上的文件复制操作
private static void doDecompressFile(File destFile, ZipInputStream zis) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int len;
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
while ((len = zis.read(buff, 0 ,DEFAULT_BUFF_SIZE)) != -1) {
bos.write(buff, 0, len);
}
bos.close();
} /**
* 文件探测
*
* When the parent file not exist.Create it.
*
* @param dirFile
* @throws Exception
*/
public static void fileProbe(File dirFile) throws Exception {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
fileProbe(parentFile);
parentFile.mkdirs();
}
}
}
 

IO流之ZipInputStream和ZipOutputStream的认识及使用的更多相关文章

  1. Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)

    Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...

  2. java IO流详解

    流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  3. IO流

    流的概念和作用 学习JavaIO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特 ...

  4. java IO流 Zip文件操作

    一.简介 压缩流操作主要的三个类 ZipOutputStream.ZipFile.ZipInputStream ,经常可以看到各种压缩文件:zip.jar.GZ格式的压缩文件 二.ZipEntry   ...

  5. java 文件压缩和解压(ZipInputStream, ZipOutputStream)

    最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...

  6. io流操作大全

    JAVA 中的IO流 一.流的概念        流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部 ...

  7. Java输入、输入、IO流 类层次关系梳理

    本文主要关注在Java编程中涉及到的IO相关的类库.方法.以及对各个层次(抽线.接口继承)的流之间的关系进行梳理 相关学习资料 http://baike.baidu.com/view/1007958. ...

  8. Java IO流详尽解析

    流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  9. java开发之IO流

    一直对IO流记不清楚,从别的地方转过来. 看下图: 流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两 ...

随机推荐

  1. 玩转ArduinoJson库 V6版本

    1.前言     前面,博主已经讲解了ArduinoJson库的V5版本.为了节省时间以及不讨论重复内容,博主建议读者先去阅读一下 玩转ArduinoJson库 V5版本 .重点了解几个东西: JSO ...

  2. 百万年薪python之路 -- Socket

    Socket 1. 为什么学习socket 你自己现在完全可以写一些小程序了,但是前面的学习和练习,我们写的代码都是在自己的电脑上运行的,虽然我们学过了模块引入,文件引入import等等,我可以在程序 ...

  3. 百万年薪python之路 -- 文件操作练习

    1.有如下文件,a1.txt,里面的内容为: 老男孩是最好的学校, 全心全意为学生服务, 只为学生未来,不为牟利. 我说的都是真的.哈哈 分别完成以下的功能: a,将原文件全部读出来并打印. with ...

  4. vue系列---Mustache.js模板引擎介绍及源码解析(十)

    mustache.js(3.0.0版本) 是一个javascript前端模板引擎.官方文档(https://github.com/janl/mustache.js) 根据官方介绍:Mustache可以 ...

  5. Java 异常(二) 自定义异常

    上篇文章介绍了java中异常机制,本文来演示一下自定义异常 上篇文章讲到非运行时异常和运行时异常,下面我们来看一下简单实现代码. 首先,先来看下演示目录 非运行时异常 也称 检查时异常 public ...

  6. 基于MVC的RESTful风格的实现

    基于MVC的RESTful风格的实现 1.RESTful风格阐述 REST服务是一种ROA(Resource-Oriented Architecture,面向资源的架构)应用.主要特点是方法信息存在于 ...

  7. (一)初识EasyTouch

    Easy Touch是一个手指触控(可以鼠标)的插件,可以非常方便的实现各种功能,使用插件第一步是添加Easy Touch组件,可以右键添加也可以在一个空的游戏物体上添加Easy Touch脚本(非事 ...

  8. Unity3D图像后处理特效——Depth of Field 3.4

    Depth of Field 3.4 is a common postprocessing effect that simulates the properties of a camera lens. ...

  9. 学 Python (Learn Python The Hard Way)

    学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 ...

  10. PyQt图形化布局

    安装PyQt第三方库 pip install PyQt5 安装Qt Designer(Qt的布局工具) pip install PyQt5-tools PyChram设置Qt工具 配置Qt Desig ...