To read ZipEntry from ZipInputStream
public class ZipEntityDemo { |
| public InputStream extract(ZipInputStream zis, String entityName) throws Exception { |
| ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| ZipEntry ze = null; |
| do { |
| ze = zis.getNextEntry(); |
| if (ze != null && ze.getName().equalsIgnoreCase(entityName)) { |
| |
| byte[] buffer = new byte[1]; |
| while (zis.available() == 1) { |
| zis.read(buffer); |
| out.write(buffer, 0, 1); |
| } |
| out.close(); |
| return new ByteArrayInputStream(out.toByteArray()); |
| } |
| } while (ze != null); |
| |
| return null; |
| } |
| } |
Here zis.getNextEntry() will return us the beginning of an entry. If the name of the entry match to our desired name we can start reading. Here we are reading byte by byte in a loop till last byte. We could have improved by using ze.
getSize(), and we could read multiple bytes till the size. But the getSize() methods some time returns -1 for some zipfiles. So it wouldn't be reliable. With out knowing the entry size only way is to read byte by byte till last.