java 文件读取zip,如何在Java中读取.zip文件中的文件?

I would like to parse a .zip file. The .zip file contains one folder. The folder in turn contains several files. I would like to read all files without writing the .zip file to disk. I have the following code:

zipFile = new ZipFile(file);

Enumeration extends ZipEntry> entries = zipFile.entries();

while(entries.hasMoreElements()){

ZipEntry entry = entries.nextElement();

InputStream stream = zipFile.getInputStream(entry);

InputStreamReader reader = new InputStreamReader(stream, "UTF-8");

Scanner inputStream = new Scanner(reader);

inputStream.nextLine();

while (inputStream.hasNext()) {

String data = inputStream.nextLine(); // Gets a whole line

String[] line = data.split(SEPARATOR); // Splits the line up into a string array

}

inputStream.close();

stream.close();

}

zipFile.close();

The problem is that this only works when the files are directly in the .zip file. How can I adapt my code so that it also works when the files are inside a folder in the .zip file?

解决方案

You could put the code that reads content inside an if

ZipEntry entry = entries.nextElement();

if (!entry.isDirectory()) {

InputStream stream = zipFile.getInputStream(entry);

...

stream.close();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值