简体   繁体   English

是否有可能从zipinputstream获得zipentry的输入流?

[英]is it possible to get a zipentry's inputstream from a zipinputstream?

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 所以我的最佳解决方案是

  1. save incoming InputStream to a file 将传入的InputStream保存到文件中
  2. read file as ZipFile 将文件读取为ZipFile
  3. use first entry's InputStream 使用第一个条目的InputStream
  4. delete temp file. 删除临时文件。

figured: 想通:

it's entirely possible, the call to ZipInputStream.getNextEntry() positions the InputStream at the start of the entry and therefore supplying the ZipInputStream is the equivalent of supplying a ZipEntry 's InputStream . 完全有可能,对ZipInputStream.getNextEntry()的调用将InputStream定位在条目的开头,因此提供ZipInputStream相当于提供ZipEntryInputStream

the ZipInputStream is smart enough to handle the entry's EOF downstream, or so it seems. ZipInputStream足够聪明,可以处理条目的EOF下游,或者看起来如此。

p. 页。

In addition to @pstanton post here is an example of code. 除了@pstanton之外,这里还有一个代码示例。 I solved the problem using the following code. 我使用以下代码解决了这个问题。 It was difficult to understand what the previous answer without an example. 如果没有示例,很难理解之前的答案。

//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。

The zip code is fairly easy but I had issues with returning ZipInputStream as Inputstream. 邮政编码相当容易,但我将ZipInputStream作为Inputstream返回时遇到了问题。 For some reason, some of the files contained within the zip had characters being dropped. 由于某种原因,zip中包含的某些文件会删除字符。 The below was my solution and so far its been working. 以下是我的解决方案,到目前为止一直在努力。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM