java 是否可以从 zipinputstream 获取 zipentry 的输入流?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3233555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is it possible to get a zipentry's inputstream from a zipinputstream?
提问by pstanton
I'm receiving a ZipInputStream from another source, and I need to provide the first entry's InputStream to another source.
我从另一个源接收 ZipInputStream,我需要将第一个条目的 InputStream 提供给另一个源。
I was hoping to be able to do this without saving a temp file on a device, however the only way I know of getting an InputStream for an individual entry is via ZipFile.getInputStream(entry) and since I have a ZipInputStream and not a ZipFile, that is not possible.
我希望能够在不在设备上保存临时文件的情况下执行此操作,但是我知道为单个条目获取 InputStream 的唯一方法是通过 ZipFile.getInputStream(entry) 并且因为我有一个 ZipInputStream 而不是 ZipFile , 这是不可能的。
So the best solution I have is
所以我最好的解决方案是
- save incoming InputStream to a file
- read file as ZipFile
- use first entry's InputStream
- delete temp file.
- 将传入的 InputStream 保存到文件
- 将文件读取为 ZipFile
- 使用第一个条目的 InputStream
- 删除临时文件。
回答by pstanton
figured:
想:
it's entirely possible, the call to ZipInputStream.getNextEntry()positions the InputStreamat the start of the entry and therefore supplying the ZipInputStreamis the equivalent of supplying a ZipEntry's InputStream.
完全有可能,在条目的开头ZipInputStream.getNextEntry()定位 the的调用InputStream,因此提供ZipInputStreamthe 相当于提供 a ZipEntry's InputStream。
the ZipInputStreamis smart enough to handle the entry's EOF downstream, or so it seems.
该ZipInputStream是足够聪明来处理条目的EOF下游,或者看起来是这样。
p.
p.
回答by Whitecat
In addition to @pstanton post here is an example of code. I solved the problem using the following code. It was difficult to understand what the previous answer without an example.
除了@pstanton 帖子之外,这里还有一个代码示例。我使用以下代码解决了这个问题。如果没有例子,很难理解之前的答案是什么。
//If you just want the first file in the zipped InputStream use this code.
//Otherwise loop through the InputStream using getNextEntry()
//till you find the file you want.
private InputStream convertToInputStream(InputStream stream) throws IOException {
ZipInputStream zis = new ZipInputStream(stream);
zis.getNextEntry();
return zis;
}
Using this code you can return an InputStream of the file that is zipped.
使用此代码,您可以返回压缩文件的 InputStream。
回答by EdwinR
The zip code is fairly easy but I had issues with returning ZipInputStream as Inputstream. For some reason, some of the files contained within the zip had characters being dropped. The below was my solution and so far its been working.
邮政编码相当简单,但我在将 ZipInputStream 作为 Inputstream 返回时遇到了问题。出于某种原因,包含在 zip 中的某些文件的字符被删除。下面是我的解决方案,到目前为止它一直在工作。
private Map<String, InputStream> getFilesFromZip(final DataHandler dhZ,
String operation) throws ServiceFault {
Map<String, InputStream> fileEntries = new HashMap<String, InputStream>();
try {
ZipInputStream zipIsZ = new ZipInputStream(dhZ.getDataSource()
.getInputStream());
try {
ZipEntry entry;
while ((entry = zipIsZ.getNextEntry()) != null) {
if (!entry.isDirectory()) {
Path p = Paths.get(entry.toString());
fileEntries.put(p.getFileName().toString(),
convertZipInputStreamToInputStream(zipIsZ));
}
}
}
finally {
zipIsZ.close();
}
} catch (final Exception e) {
faultLocal(LOGGER, e, operation);
}
return fileEntries;
}
private InputStream convertZipInputStreamToInputStream(
final ZipInputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(in, out);
InputStream is = new ByteArrayInputStream(out.toByteArray());
return is;
}

