How to convert ZipInputStream to InputStream?

I have code where ZipInputSream is converted to byte [], but I do not know how I can convert it to input stream.

private void convertStream(String encoding, ZipInputStream in) throws IOException, UnsupportedEncodingException { final int BUFFER = 1; @SuppressWarnings("unused") int count = 0; byte data[] = new byte[BUFFER]; while ((count = in.read(data, 0, BUFFER)) != -1) { // How can I convert data to InputStream here ? } } 
+2
source share
5 answers

This is how I solved this problem. Now I can get individual files from ZipInputStream into memory as InputStream.

 private InputStream convertZipInputStreamToInputStream(ZipInputStream in, ZipEntry entry, String encoding) throws IOException { final int BUFFER = 2048; int count = 0; byte data[] = new byte[BUFFER]; ByteArrayOutputStream out = new ByteArrayOutputStream(); while ((count = in.read(data, 0, BUFFER)) != -1) { out.write(data); } InputStream is = new ByteArrayInputStream(out.toByteArray()); return is; } 
+2
source

The zip code is pretty simple, but I had trouble returning ZipInputStream as input. For some reason, some of the files contained in zip had discarded characters. Below was my decision, and so far it has worked.

 private Map<String, InputStream> getFilesFromZip(final DataHandler dhZ, String operation) throws ServiceFault { Map<String, InputStream> fileEntries = new HashMap<String, InputStream>(); try { DataSource dsZ = dhZ.getDataSource(); 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; } 
0
source

The following is an example of a function that will extract all files from a ZIP archive. This function will not work with files in subfolders:

 private static void testZip() { ZipInputStream zipStream = null; byte buff[] = new byte[16384]; int readBytes; try { FileInputStream fis = new FileInputStream("./test.zip"); zipStream = new ZipInputStream(fis); ZipEntry ze; while((ze = zipStream.getNextEntry()) != null) { if(ze.isDirectory()) { System.out.println("Folder : "+ze.getName()); continue;//no need to extract } System.out.println("Extracting file "+ze.getName()); //at this moment zipStream pointing to the beginning of current ZipEntry, eg archived file //saving file FileOutputStream outFile = new FileOutputStream(ze.getName()); while((readBytes = zipStream.read(buff)) != -1) { outFile.write(buff, 0, readBytes); } outFile.close(); } } catch (Exception e) { System.err.println("Error processing zip file : "+e.getMessage()); } finally { if(zipStream != null) try { zipStream.close(); } catch (IOException e) { e.printStackTrace(); } } } 
0
source

ZipInputStream allows ZipInputStream to directly read the contents of the ZIP: getNextEntry() with getNextEntry() until you find the record you want to read, and then just read it with ZipInputStream .

If you don’t want to just read the ZIP content, but you need to apply an additional conversion to the stream before proceeding to the next step, you can use PipedInputStream and PipedOutputStream . The idea would be similar to this (written from memory, maybe not even compilation):

 import java.io.PipedInputStream; import java.io.PipedOutputStream; public abstract class FilterThread extends Thread { private InputStream unfiltered; public void setUnfilteredStream(InputStream unfiltered) { this.unfiltered = unfiltered; } private OutputStream threadOutput; public void setThreadOutputStream(OutputStream threadOutput) { this.threadOutput = threadOutput; } // read from unfiltered stream, filter and write to thread output stream public abstract void run(); } ... public InputStream getFilteredStream(InputStream unfiltered, FilterThread filter) { PipedInputStream filteredInputStream = new PipedInputStream(); PipedOutputStream threadOutputStream = new PipedOutputStream(filteredInputStream); filter.setUnfilteredStream(unfiltered); filter.setThreadOuptut(threadOutputStream); filter.start(); return filteredInputStream; } ... public void clientCode() { ... ZipInputStream zis = ...;// get ZIP stream FilterThread filter = ...; // assign your implementation of FilterThread that transforms your ZipInputStream InputStream filteredZipInputStream = getFilteredStream(zis, filter); ... } 
-1
source

Source: https://habr.com/ru/post/950549/


All Articles