java.util.zip获取Zip文件条目InputStream
package com.test; import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import com.tool.zip.InflaterInputStream;
import com.tool.zip.ZipEntry;
import com.tool.zip.ZipInputStream; public class TestZip { //private static String tempZip = "f:\\tempZip.zip";
/**
*
* @param input Zip文件的流
* @throws Exception
*/
public void upload(InputStream input) throws Exception { // System.setProperty("sun.zip.encoding",
// System.getProperty("sun.jnu.encoding")); ZipInputStream zis = new ZipInputStream(input);
ZipEntry entry = null; while ((entry = zis.getNextEntry()) != null) { // System.out.printf("条目信息: 名称%1$b, 大小%2$d, 压缩时间%3$d \n",
// entry.getName(), entry.getSize(), entry.getTime()); if (entry.isDirectory()) { // is dir
// System.out.println(entry.getName() + "是一个目录");
File f = new File("f:" + File.separator + entry.getName());
if (!f.exists())
f.mkdirs();
} else { // byte[] data = getByte(zis); // 获取当前条目的字节数组 InputStream is = new ByteArrayInputStream(data); // 把当前条目的字节数据转换成Inputstream流
String[] names = entry.getName().split("/"); String path = "f:" + File.separator; path += join(names, File.separator); //System.out.println(path);
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
toWrite(is, file);
} }
}
}
/**
* 向file文件写入字节
* @param ins
* @param file
*/
public static void toWrite(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 获取条目byte[]字节
* @param zis
* @return
*/
public byte[] getByte(InflaterInputStream zis) {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
byte[] buf = null;
int length = 0; while ((length = zis.read(temp, 0, 1024)) != -1) {
bout.write(temp, 0, length);
} buf = bout.toByteArray();
bout.close();
return buf;
} catch (IOException e) {
e.printStackTrace();
return null;
}
} public static String join(Object[] o, String flag) {
StringBuffer str_buff = new StringBuffer(); for (int i = 0, len = o.length; i < len; i++) {
str_buff.append(String.valueOf(o[i]));
if (i < len - 1)
str_buff.append(flag);
} return str_buff.toString();
} // test method
public static void main(String[] args) throws Exception {
TestZip test = new TestZip();
String filePath = "f:\\test.zip";
// File file = new File(filePath);
InputStream input = new BufferedInputStream(new FileInputStream(
filePath)); test.upload(input);
}
}
java.util.zip获取Zip文件条目InputStream的更多相关文章
- java.util.Calendar获取时间区间问题
虽然java8的LocalDate已经出来,但是很多项目以及自己习惯上还是使用Date,这里还是简单介绍一下如何通过java.util.Calendar获取时间区间. 1 通过calendar.get ...
- java使用java.util.Properties读取properties文件的九种方法
直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...
- java工具类获取properties文件的配置
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...
- java 使用递归获取指定文件路径目录,删除指定的文件后缀(可扩展,根据具体需要的删除的后缀进行配置)~~
在工作开发过程中,每次用SVN提交代码全选择的时候,发现会产生很多不需要的文件后缀垃圾文件,感觉挺烦人的,一个一个删太麻烦了,如果产生多种后缀文件时,那删起来多费劲,是吧?想想,就写了一段程序通过递归 ...
- java调用ffmpeg获取视频文件信息的一些参数
一.下载ffmpeg http://www.ffmpeg.org/download.html 主要需要bin目录下的ffmpeg可执行文件 二.java代码实现 package com.aw.util ...
- Java反射机制获取Class文件
JAVA反射机制是在运行状态中,对于任意一个类(class文件),都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象方法的功能称为 ...
- Java thymeleaf模板获取资源文件的内容
我们在某些时候可能需要获取配置文件properties中的配置信息,而不需要用Java传给模板,在模板中就可以直接获取 我们需要在resources/下定义国际化配置文件即可,注意名称必须中messa ...
- JAVA利用poi获取world文件内容
本文主要简单介绍了利用poi包,读取world文件内容. 这个依然存在版本的问题,只能读取doc结尾的老版本文件. 话不多说,上代码: import java.io.File; import java ...
- Java基础(十三) 文件高级技术
文件高级技术 一.常见文件类型处理 一)属性文件 属性文件很简单,一行表示一个属性,属性就是键值对,键和值用(=)或者(:)分隔. #ready to work name = tang age = p ...
随机推荐
- vuejs
简介 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,并且非常容易 ...
- Caffe学习系列(17): caffe源码分析 vector<Blob<Dtype>*>& bottom(转)
转自:http://blog.csdn.net/qq_14975217/article/details/51524042 Blob:4个维度 n x c x h x w: bottom[0] .bot ...
- php之获取程序源码的方法
文件hello.php和index.php在同一目录 hello.php <?php class hello{ public function __construct() { echo 'hel ...
- win7使用自带资源管理器来登陆FTP
使用windows自带的资源管理器登陆FTP,其实很简单,打开计算机,在地址栏直接输入ftp://ftp.hostname即可. 但是这种方式需要每次输入ip地址,还要输入用户名和密码,比较麻烦.可能 ...
- nginx负载均衡基于ip_hash的session粘帖
nginx负载均衡基于ip_hash的session粘帖 nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除 ...
- 【YEOMAN】执行yo命令,报EACCES: permission denied, mkdir '/root/.config/configstore'
基础环境:CentOS7.Nodejs6.0之上,yo:1.8.4 在执行yo初始化webapp时,报错,错误内容如下: Error: EACCES: permission denied, mkdir ...
- go:interface{}、断言与类型转换
interface{}可用于向函数传递任意类型的变量,但对于函数内部,该变量仍然为interface{}类型(空接口类型), 不清楚这点将可能导致错误.如以下代码: package main impo ...
- css垂直居中 两种方法
在前端面试的时候我们经常会被问道怎样使一个元素在页面垂直居中呢,这也是一个老生常谈的问题了. 解决的方法基本都是使用定位来实现 div{display: fixed;left: 50%;top: 50 ...
- LeetCode之136. Single Number
-------------------------------------- 一个数异或它自己会得到0,0异或n会得到n,所以可以用异或来消除重复项. AC代码如下: public class Sol ...
- NYOJ题目20吝啬的国度
-----------------------------------------n-1条边的无向连通图是一棵树,又因为树上两点之间的路径是唯一的,所以解是唯一的.(注意并不一定是二叉树,所以最好采用 ...