I want to reset the ZipInputStream (ie back to the start position) in order to read certain files in order. How do I do that? I am so stucked...
ZipEntry entry;
ZipInputStream input = new ZipInputStream(fileStream);//item.getInputStream());
int check =0;
while(check!=2){
entry = input.getNextEntry();
if(entry.getName().toString().equals("newFile.csv")){
check =1;
InputStreamReader inputStreamReader = new InputStreamReader(input);
reader = new CSVReader(inputStreamReader);
//read files
//reset ZipInputStream if file is read.
}
reader.close();
}
if(entry.getName().toString().equals("anotherFile.csv")){
check =2;
InputStreamReader inputStreamReader = new InputStreamReader(input);
reader = new CSVReader(inputStreamReader);
//read files
//reset ZipInputStream if file is read.
}
reader.close();
}
}
If possible (i.e. you have an actual file, not just a stream to read from), try to use the ZipFile class rather than the more low-level ZipInputStream. ZipFile takes care of jumping around in the file and opening streams to individual entries.
Try this. It will process each file in the zip using a csv processor. In my system, the input stream is a stream from an HTTP connection.
I have the same problem, i am getting InputStream from Blob and don't want to use temporary intermediary files so want to reset the ZipInputStream to re-read the ZipEntries again from the same object of ZipInputStream, But I got this by anotherway, it might help u.
please share if any one has a good solution, thanks
You can wrap the InputStream in a BufferedInputStream and call the methods mark() and reset(), like so:
The readLimit you pass to the mark method must be big enough to cover all the read operations you do with the inputStream. If you set it to the maximum supposed file size you can have in input you should be covered.
Actually there is no way to reset a
ZipInputStream
as you expect, because it does not support reset/markers etc. But you can use anByteArrayOutputStream
to buffer yourInputStream
asbyte[]
so you write a Class looking like
and a
openReadStream
-method like this:Now you are able to read an entry and close it afterwards. If you call
openReadStream
it will provide you a newZipInputStream
, so you can selectively read Entries like this:calling the method
readZipEntry
And you will get out a new InputStream. You can now read multiple times from the same Input.