java 读取 inputstream_java – 如何使用InputStream从ZIP读取文件?

下面是一个关于如何提取ZIP文件的简单示例,您需要检查该文件是否是目录.但这是最简单的.

您缺少的步骤是读取输入流并将内容写入写入输出流的缓冲区.

// Expands the zip file passed as argument 1, into the

// directory provided in argument 2

public static void main(String args[]) throws Exception

{

if(args.length != 2)

{

System.err.println("zipreader zipfile outputdir");

return;

}

// create a buffer to improve copy performance later.

byte[] buffer = new byte[2048];

// open the zip file stream

InputStream theFile = new FileInputStream(args[0]);

ZipInputStream stream = new ZipInputStream(theFile);

String outdir = args[1];

try

{

// now iterate through each item in the stream. The get next

// entry call will return a ZipEntry for each file in the

// stream

ZipEntry entry;

while((entry = stream.getNextEntry())!=null)

{

String s = String.format("Entry: %s len %d added %TD",

entry.getName(), entry.getSize(),

new Date(entry.getTime()));

System.out.println(s);

// Once we get the entry from the stream, the stream is

// positioned read to read the raw data, and we keep

// reading until read returns 0 or less.

String outpath = outdir + "/" + entry.getName();

FileOutputStream output = null;

try

{

output = new FileOutputStream(outpath);

int len = 0;

while ((len = stream.read(buffer)) > 0)

{

output.write(buffer, 0, len);

}

}

finally

{

// we must always close the output file

if(output!=null) output.close();

}

}

}

finally

{

// we must always close the zip file.

stream.close();

}

}

代码摘录来自以下网站:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值