I have code, where ZipInputSream is converted to byte[], but I don't know how I can convert that to inputstream.
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 ?
}
}
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
allows to read ZIP contents directly: iterate usinggetNextEntry()
until you find the entry you want to read and then just read from theZipInputStream
.If you don't want to just read ZIP content, but you need to apply an additional transform to the stream before passing to the next step, you can use
PipedInputStream
andPipedOutputStream
. The idea would be similar to this (written from memory, might not even compile):Please find below example of function that will extract all files from ZIP archive. This function will not work with files in subfolders:
Here is how I solved this problem. Now I can get single files from ZipInputStream to memory as InputStream.
ZipInputStream is a subclass of InputStream.
http://download.oracle.com/javase/6/docs/api/java/util/zip/ZipInputStream.html