IO流之ZipInputStream和ZipOutputStream的认识及使用
转载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];
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.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的认识及使用的更多相关文章
- Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)
Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...
- java IO流详解
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- IO流
流的概念和作用 学习JavaIO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特 ...
- java IO流 Zip文件操作
一.简介 压缩流操作主要的三个类 ZipOutputStream.ZipFile.ZipInputStream ,经常可以看到各种压缩文件:zip.jar.GZ格式的压缩文件 二.ZipEntry ...
- java 文件压缩和解压(ZipInputStream, ZipOutputStream)
最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...
- io流操作大全
JAVA 中的IO流 一.流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部 ...
- Java输入、输入、IO流 类层次关系梳理
本文主要关注在Java编程中涉及到的IO相关的类库.方法.以及对各个层次(抽线.接口继承)的流之间的关系进行梳理 相关学习资料 http://baike.baidu.com/view/1007958. ...
- Java IO流详尽解析
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- java开发之IO流
一直对IO流记不清楚,从别的地方转过来. 看下图: 流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两 ...
随机推荐
- 神奇的 SQL 之团结的力量 → JOIN
前言 开心一刻 闺蜜家暴富,买了一栋大别野,喊我去吃饭,菜挺丰盛的,筷子有些不给力,银筷子,好重,我说换个竹子的,闺蜜说,这种银筷子我家总共才五双,只有贵宾才能用~我咬着牙享受着贵宾待遇,终于,在 ...
- 《编写可维护的JavaScript》 笔记
<编写可维护的JavaScript> 笔记 我的github iSAM2016 概述 本书的一开始介绍了大量的编码规范,并且给出了最佳和错误的范例,大部分在网上的编码规范看过,就不在赘述 ...
- (四)适配器Adapter
只对简单应用进行描述.适配器与ListView配合使用可以快速生成item,效果如下例所示 一.简单模式 方式一 xml <ListView android:id="@+id/lv_t ...
- fenby C语言 P18
#include <stdio.h> int main(){ int i; for(i=1;i<=15;i++) { if((i%2==1)) { printf("%d\n ...
- char 、signed char、unsigned char
看如下代码: char c = -1; signed char sc = -1; unsigned char uc = -1; printf("c=%d, sc=%d, uc=%d, cx= ...
- Linux后台运行Jar方法
原文地址:http://blog.csdn.net/c1481118216 https://blog.csdn.net/c1481118216/article/details/53010963 在li ...
- 前端技术之:常见前端Web框架
Express 声称是快速.自由.小巧的Node.js Web框架,官网地址如下: https://expressjs.com/ https://github.com/expressjs/expres ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- 入门Android底层需要的一些技能
<Android的设计与实现> Android框架层<Linux系统编程手册> Linux系统编程<Android内核剖析> 编译框架和romC语言和Linux内核 ...
- NOIP模拟 7
考试最后半小时发现T1过不去任何样例,T飞,发现没调用solv函数,挽回40分 考试最后几分钟给T2测了一组自制样例,发现了bug,挽回20分 天- -检查这么重要的么(其实是我第一次打的时候就没验证 ...