java zipinputstream_java.util.zip获取Zip文件条目InputStream

该博客展示了如何使用Java的ZipInputStream类处理Zip文件的条目。通过创建ZipInputStream对象,遍历每个条目,读取条目内容并将其写入到目标文件中。示例代码包括创建目录、读取条目字节、将字节写入文件等操作。
摘要由CSDN通过智能技术生成

packagecom.test;importjava.io.BufferedInputStream;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importcom.tool.zip.InflaterInputStream;importcom.tool.zip.ZipEntry;importcom.tool.zip.ZipInputStream;public classTestZip {//private static String tempZip = "f:\\tempZip.zip";

/***

*@paraminput Zip文件的流

*@throwsException*/

public void upload(InputStream input) throwsException {//System.setProperty("sun.zip.encoding",//System.getProperty("sun.jnu.encoding"));

ZipInputStream zis= newZipInputStream(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 = newFile(path);if (!file.exists()) {

file.createNewFile();

toWrite(is, file);

}

}

}

}/*** 向file文件写入字节

*@paramins

*@paramfile*/

public static voidtoWrite(InputStream ins, File file) {try{

OutputStream os= newFileOutputStream(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[]字节

*@paramzis

*@return

*/

public byte[] getByte(InflaterInputStream zis) {try{

ByteArrayOutputStream bout= newByteArrayOutputStream();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();returnbuf;

}catch(IOException e) {

e.printStackTrace();return null;

}

}public staticString join(Object[] o, String flag) {

StringBuffer str_buff= newStringBuffer();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);

}returnstr_buff.toString();

}//test method

public static void main(String[] args) throwsException {

TestZip test= newTestZip();

String filePath= "f:\\test.zip";//File file = new File(filePath);

InputStream input = new BufferedInputStream(newFileInputStream(

filePath));

test.upload(input);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值