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 ...
随机推荐
- [Opencv]图像的梯度与边缘检测(转)
文章来源:https://blog.csdn.net/on2way/article/details/46851451 梯度简单来说就是求导,在图像上表现出来的就是提取图像的边缘(不管是横向的.纵向的. ...
- JS文档DOM
访问指定节点 通过document节点获取 document.getElementById(elementId) document.getElementsByName(elementName) d ...
- pix2pix-tensorflow搭建及其使用
目录 pix2pix-tensorflow搭建过程 1. 环境搭建 2. 环境说明 3. 开始搭建 4. 训练结果说明 5. 数据集 5.1 图片格式说明 5.3 从先用图片创建图像对 5.4 如何进 ...
- Python - TypeError: unicode argument expected, got 'str'
参考:TypeError: unicode argument expected, got 'str' Python代码: from io import StringIO def main(): f = ...
- springboot logback + log4j2日志管理
springboot的web项目中自带了日志组件: 我们看一下,springboot中找到日志组件. <dependency> <groupId>org.springframe ...
- git commit进行代码检查
使用Ant Design Pro提交代码的时候进行代码检查报了很多错 git commit --no-verify -m "commit" 就可以跳过代码检查 或者在项目里新建 ...
- html中用变量作为django字典的键值
若字典为dic={'name': Barbie, 'age': 20},则在html中dic.name为Barbie,dic.age为20. 但若字典为dic={'Barbie': 1, 'Roger ...
- hdu 3694 10 福州 现场 E - Fermat Point in Quadrangle 费马点 计算几何 难度:1
In geometry the Fermat point of a triangle, also called Torricelli point, is a point such that the t ...
- js 的各种排序算法 -- 待续
链接 function quickSort(arr,l,r){ if(l < r){ var i = l, j = r, x = arr[i]; while(i<j){ while(i&l ...
- composer update 提示 username
解决办法 暂时修改composer.json "repositories": { "packagist": { "type": " ...