-5

Could some one help me with the code snippet in converting zip byte[] to unzip byte[] in memory with out writing to a file intermediary

I looked in to this stack overflow "convert zip byte[] to unzip byte[]' but not able to get it using java code

Thanks Somu

1 Answers1

3

Here are the tools you need for that:

  1. ByteArrayInputStream - allows you to wrap an array of bytes as a stream.
  2. ZipInputStream - reads the zipped stream of bytes and presents them as unzipped ones.
  3. ByteArrayOutputStream - a stream that writes into internal byte buffer.
  4. (If using Java 9) InputStream#transferTo - copy from input stream to output stream. (If not using Java 9) Copy it manually
  5. ByteArrayOutputStream#toByteArray - extract buffer from the output stream.

Wire them all together and you are done.

bezmax
  • 25,562
  • 10
  • 53
  • 84
  • Thank you bezmax, I tried but no luck – SomashekharRao Ramasingu Jan 02 '18 at 14:45
  • ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(c) //here c containz zipped byte array ZipInputStream zipInputStreamn = new ZipInputStream(byteArrayInputStream) ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream() ByteStreams.copy(zipInputStreamn,byteArrayOutputStream) byte[] unZipFileByteArray = byteArrayOutputStream.toByteArray() println unZipFileByteArray.toString() //this is giving empty array – SomashekharRao Ramasingu Jan 02 '18 at 14:45