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

前端 未结 3 812
孤城傲影
孤城傲影 2020-12-10 23:39

I\'m receiving a ZipInputStream from another source, and I need to provide the first entry\'s InputStream to another source.

I was hoping to be able to do this witho

3条回答
  •  长情又很酷
    2020-12-11 00:39

    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.

    private Map getFilesFromZip(final DataHandler dhZ,
            String operation) throws ServiceFault {
        Map fileEntries = new HashMap();
        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;
    }
    

提交回复
热议问题