一、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)的更多相关文章

  1. java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)

    // java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...

  2. Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)

    本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...

  3. 原生java 压缩解压zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  4. Java动态解压zip压缩包

    import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; impo ...

  5. JDK 自带压缩解压流

    代码如下 package com.test.java.zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStr ...

  6. Java压缩包解压到指定文件

    在获得一个以Zip格式压缩的文件之后,需要将其进行解压缩,还原成压缩前的文件.若是使用Java自带的压缩工具包来实现解压缩文件到指定文件夹的功能,因为jdk提供的zip只能按UTF-8格式处理,而Wi ...

  7. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  8. java代码解压zip文件

    import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp ...

  9. Java 压缩/ 解压 .Z 文件

    1.问题描述 公司项目有需要用 JAVA 解压 .z文件. .z 是 unix 系统常见的压缩文件. 2.源码 import com.chilkatsoft.CkUnixCompress; impor ...

随机推荐

  1. codeforces 200 div2 C. Rational Resistance 思路题

    C. Rational Resistance time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. angular的 表单

    一般来讲表单可能遇到的问题:1.如何数据绑定.2.验证表单.3.显示出错信息.4.整个form的验证.5.避免提交没有验证通过的表单.6.防止多系提交. input属性:nameng-modelng- ...

  3. 获取文本中所有的<img>标签的位置,获取所有img标签的src

    public static int[] GetImagePos(string str) { str = str.Replace("$", " "); str = ...

  4. PistgreSQL9.6手册(基础摘录)

    学习目的:基础使用. 能够开发RoR就行. git: https://github.com/postgres-cn/pgdoc-cn 1.2. 架构基础 PostgreSQL使用一种客户端/服务器的模 ...

  5. 2-15-MySQL进阶

    select select 字段列表 from 数据表 [[as] 别名] [where 条件] 别名: 数据表 [[as] 别名] select AA.money,BB.name from prod ...

  6. wikioi1036 商务旅行 挺水的lca

    链接:http://wikioi.com/problem/1036/ 题意不写了. 思路:很明显找到lca然后用两个点的深度相加-lca的深度就是这一步的最近步数. #include <stdi ...

  7. 修复Ubuntu下XTerm不能正常显示中文字体的问题

    打开/etc/X11/app-defaults/XTerm在最后添加如下代码: Xft.dpi:96         xpdf.title: PDF         XTerm*faceSize: 1 ...

  8. CF 160D Edges in MST 最小生成树的性质,寻桥,缩点,批量处理 难度:3

    http://codeforces.com/problemset/problem/160/D 这道题要求哪条边存在于某个最小生成树中,哪条边不存在于最小生成树中,哪条边绝对存在于最小生成树中 明显桥边 ...

  9. iOS开发之 - 键盘处理神器 IQKeyboardManager

    平常在开发中,用到输入框的地方不胜其数,当输入框位于屏幕底部时,弹起的键盘很可能覆盖输入框,导致用户看不到输入结果,体验较差...... IQKeyboardManager 可以很简单快捷的解决键盘遮 ...

  10. 【LeetCode 1_数组_哈希表】Two Sum

    解法一:O(N) vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, i ...