读取jar文件中的zip文件 [英] Reading a zip file within a jar file

查看:159
本文介绍了读取jar文件中的zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前我们的网络应用程序中有一些zip文件。我们希望在zip文件中删除特定的文本文档。这不是问题:

Previously we had some zip files within our web application. We would want to pares a specific text document within the zip file. This wasn't a problem:

URL url = getClass().getResource(zipfile);
ZipFile zip = new ZipFile(url.getFile().replaceAll("%20", " "));     
Entry entry = zip.getEntry("file.txt");

InputStream is = zip.getInputStream(entry);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

String line = reader.readLine();
while (line != null) {
    // do stuff
}

但是我们已经将这些zip文件移动到另一个模块中,并希望将它们打包到jar中。不幸的是,创建 ZipFile 现在失败了。我可以为zip获得 InputStream 但是我无法获得条目本身的输入流。

However we've moved these zip files into another module and want to package them within a jar. Unfortunately, creating the ZipFile now fails. I can get an InputStream for the zip: but I have no way of getting an input stream for the entry itself.

InputStream is = getClass().getResourceAsStream(zipfile);
ZipInputStream zis = new ZipInputStream(is);

ZipEntry entry = zis.getNextEntry();
while (entry != null && !entry.getName().equals("file.txt")) {
    entry = zis.getNextEntry();
}

但我无法获得条目本身的输入流。我尝试找到条目的长度并从 ZipInputStream 获取下一个n字节,但这对我不起作用。似乎读取的所有字节都是0。

but I have no way of getting an input stream for the entry itself. I tried finding the length of the entry and getting the next n bytes from the ZipInputStream but this didn't work for me. It seemed all bytes read were 0.

有没有办法解决这个问题,还是我必须将zip文件移回核心项目?

Is there a way round this or am I going to have to move the zip files back into the core project?

推荐答案

条目可以为您提供inner-zip文件的输入流。

entry can give you the inputstream of the inner-zip file.

InputStream innerzipstream = zip.getInputStream(entry);

所以你可以使用

new ZipInputStream(innerzipstream);

并要求ZipInputStream检索内部zip文件的内容(以有序方式,你没有随机访问,因为它是一个ZipInputStream)

and ask the ZipInputStream to retrieve the content of the inner-zip-file (in an ordered fashion, you don't have random access because it's a ZipInputStream)

看看 http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/ZipInputStream.html

顺序拉链访问

由于ZipInputStream正在读取邮政编码来自输入流的文件必须按顺序执行:

As ZipInputStream is reading a zip file from an input stream it has to do things in order:

// DO THIS for each entry
ZipEntry e = zipInputStreamObj.getNextEntry();
e.getName // and all data
int size = e.getSize(); // the byte count
while (size > 0) {
   size -= zipInputStreamObj.read(...);
}
zipInputStreamObj.closeEntry();
// DO THIS END

zipInputStreamObj.close();

注意:我不知道ZipInputStream.getNextEntry()是否返回到达zip文件末尾时是否为null。我希望如此,因为当没有更多条目时我不知道其他方式。

Note: I don't know if ZipInputStream.getNextEntry() returns null when end of zip file is reached or not. I hope so because I don't know other way to realize when there are no more entries.

这篇关于读取jar文件中的zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆