Java之解压流(ZipInputStream)
一、ZipInputStream相对于ZipOutputStream而言,使用上面简单的多了,相对的,既然存在压缩流,就会存在,解压的方式。
二、解压文件,流的使用过程中也是很常用的,在读取文件,根据文件类型进行处理,这样,就可以做到,最低成本的数据传输了
三、解压例子
/**
* 提供给用户使用的解压工具
* @param srcPath
* @param outPath
* @throws IOException
*/
public static void decompressionFile(String srcPath, String outPath) throws IOException {
//简单判断解压路径是否合法
if (!new File(srcPath).isDirectory()) {
//判断输出路径是否合法
if (new File(outPath).isDirectory()) {
if (!outPath.endsWith(File.separator)) {
outPath += File.separator;
}
//zip读取压缩文件
FileInputStream fileInputStream = new FileInputStream(srcPath);
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
//解压文件
decompressionFile(outPath, zipInputStream);
//关闭流
zipInputStream.close();
fileInputStream.close();
} else {
throw new RuntimeException("输出路径不合法!");
}
} else {
throw new RuntimeException("需要解压的文件不合法!");
}
} /**
* ZipInputStream是逐个目录进行读取,所以只需要循环
* @param outPath
* @param inputStream
* @throws IOException
*/
private static void decompressionFile(String outPath, ZipInputStream inputStream) throws IOException {
//读取一个目录
ZipEntry nextEntry = inputStream.getNextEntry();
//不为空进入循环
while (nextEntry != null) {
String name = nextEntry.getName();
File file = new File(outPath+name);
//如果是目录,创建目录
if (name.endsWith("/")) {
file.mkdir();
} else {
//文件则写入具体的路径中
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int n;
byte[] bytes = new byte[];
while ((n = inputStream.read(bytes)) != -) {
bufferedOutputStream.write(bytes, , n);
}
//关闭流
bufferedOutputStream.close();
fileOutputStream.close();
}
//关闭当前布姆
inputStream.closeEntry();
//读取下一个目录,作为循环条件
nextEntry = inputStream.getNextEntry();
}
}
四、测试:
public static void main(String[] args) throws IOException {
decompressionFile("D:\\srv.zip", "D:\\test");
}
Java之解压流(ZipInputStream)的更多相关文章
- java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)
// java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- 原生java 压缩解压zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- Java动态解压zip压缩包
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; impo ...
- JDK 自带压缩解压流
代码如下 package com.test.java.zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStr ...
- Java压缩包解压到指定文件
在获得一个以Zip格式压缩的文件之后,需要将其进行解压缩,还原成压缩前的文件.若是使用Java自带的压缩工具包来实现解压缩文件到指定文件夹的功能,因为jdk提供的zip只能按UTF-8格式处理,而Wi ...
- JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包
package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...
- java代码解压zip文件
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp ...
- Java 压缩/ 解压 .Z 文件
1.问题描述 公司项目有需要用 JAVA 解压 .z文件. .z 是 unix 系统常见的压缩文件. 2.源码 import com.chilkatsoft.CkUnixCompress; impor ...
随机推荐
- python学习笔记(日志系统实现)
博主今天在自己的接口自动化框架中添加了日志系统 基于python自带的logging库.包括日志主函数.生成日志文件: # -*- coding: utf-8 -*- # 日志系统 # 时间:2017 ...
- git源代码管理工具
git是一款源代码管理工具 是分布式版本管理工具 分布式管理必须先在本地提交然后才能提交到服务器: svn集中式版本管理工具 集中式版本管理工具离开服务器就做不了版本管理: 初始化仓库 1.用git初 ...
- Page.TryUpdateModel 方法
使用来自值提供程序的值更新指定的模型实例. 使用来自值提供程序的值更新指定的模型实例. 命名空间: System.Web.UI程序集: System.Web(System.Web.dll 中) ...
- h5桌面通知Notification
H5中的桌面通知Notification 前言: 对于一个前端开发者,逛网页总会留意一些新奇的功能,对于上班总会用到Teambition的我,总是能收到Notification...所以今天就来研究下 ...
- Javascript 面向对象编程(补充):封装
Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有class(类). 那么,如果 ...
- JS获取当前时间到30天之后的日期区间
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 基于Open XML 导出数据到Excel
数据导出的结果: 步骤1.新建一个Excel 文档,模板根据自己需要设置 步骤2.使用OpenXml 打开Excel 文件 步骤3.点击ReflectCode 功能,生成相应的代码文档 using ...
- 创芯Xilinx Microblaze 学习系列第一集
创芯Xilinx Microblaze 学习系列第一集 Xilinx ISE Design Suite 13.2 The MicroBlaze™ embedded processor soft cor ...
- 第13课:HTML基础之DOM操作2
1. 1)d.innerHTML:标签中的所有内容 删除某个标签 a)可以直接修改innerHTML的值: b) 2)d.innerText:标签中的文本内容 3)input.value='aaa' ...
- Xadmin的配置及使用
xadmin是Django的第三方扩展,可是使Django的admin站点使用更方便. 1. 安装 通过如下命令安装xadmin的最新版 pip install https://github.com/ ...