List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream read
public int read(byte[] buffer, int start, int length) throws IOException
From source file:cz.muni.fi.xklinec.zipstream.Utils.java
/** * Reads whole section between current LocalFileHeader and the next Header * from the archive. If the ArchiveEntry being read is deflated, stream * automatically inflates the data. Output is always uncompressed. * //from w w w . ja va2 s. c o m * @param zip * @return * @throws IOException */ public static byte[] readAll(ZipArchiveInputStream zip) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); long skipped = 0; long value = Long.MAX_VALUE; byte[] b = new byte[1024]; while (skipped != value) { long rem = value - skipped; int x = zip.read(b, 0, (int) (b.length > rem ? rem : b.length)); if (x == -1) { return bos.toByteArray(); } bos.write(b, 0, x); skipped += x; } return bos.toByteArray(); }
From source file:org.apache.slider.common.tools.SliderUtils.java
public static InputStream getApplicationResourceInputStream(FileSystem fs, Path appPath, String entry) throws IOException { InputStream is = null;//w ww .ja v a 2 s. co m FSDataInputStream appStream = null; try { appStream = fs.open(appPath); ZipArchiveInputStream zis = new ZipArchiveInputStream(appStream); ZipArchiveEntry zipEntry; boolean done = false; while (!done && (zipEntry = zis.getNextZipEntry()) != null) { if (entry.equals(zipEntry.getName())) { int size = (int) zipEntry.getSize(); if (size != -1) { log.info("Reading {} of size {}", zipEntry.getName(), zipEntry.getSize()); byte[] content = new byte[size]; int offset = 0; while (offset < size) { offset += zis.read(content, offset, size - offset); } is = new ByteArrayInputStream(content); } else { log.debug("Size unknown. Reading {}", zipEntry.getName()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while (true) { int byteRead = zis.read(); if (byteRead == -1) { break; } baos.write(byteRead); } is = new ByteArrayInputStream(baos.toByteArray()); } done = true; } } } finally { IOUtils.closeStream(appStream); } return is; }
From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java
public void validateZipAndClose(InputStream in) throws IOException { ZipArchiveInputStream zip = new ZipArchiveInputStream(in); try {/*from w w w . jav a2 s.co m*/ byte[] buf = new byte[1024]; ZipArchiveEntry entry = zip.getNextZipEntry(); if (entry == null) throw new BadRequest("Archive is empty"); do { entry.getName(); entry.getMethod(); entry.getSize(); while (zip.read(buf, 0, buf.length) >= 0) ; entry = zip.getNextZipEntry(); } while (entry != null); } finally { zip.close(); } }
From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java
public InputStream readZipEntry(String match) throws IOException { InputStream in = this.openInputStream(); try {//from www . j a v a 2 s. co m ZipArchiveInputStream zip = new ZipArchiveInputStream(in); byte[] buf = new byte[1024]; ZipArchiveEntry entry = zip.getNextZipEntry(); do { if (entry.getName().equals(match)) { return zip; } long size = entry.getSize(); if (size > 0) { zip.skip(size); } else { while (zip.read(buf, 0, buf.length) >= 0) ; } entry = zip.getNextZipEntry(); } while (entry != null); zip.close(); } catch (RuntimeException | Error | IOException e) { in.close(); throw e; } return null; }
From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java
public XMLEventReader createAtomFeedFromArchive(final String id, final String entryPattern) throws IOException { final FileObject file = this; final XMLEventFactory ef = XMLEventFactory.newInstance(); final byte[] buf = new byte[1024]; final ZipArchiveInputStream zip = new ZipArchiveInputStream(file.openInputStream()); return new XMLEventReaderBase() { private boolean started; private boolean ended; public void close() throws XMLStreamException { try { zip.close();/* w w w .j av a 2 s .c om*/ } catch (IOException e) { throw new XMLStreamException(e); } } protected boolean more() throws XMLStreamException { try { ZipArchiveEntry entry; if (!started) { Namespace atom = ef.createNamespace(FEED.getPrefix(), FEED.getNamespaceURI()); add(ef.createStartDocument()); add(ef.createStartElement(FEED, null, Arrays.asList(atom).iterator())); add(ef.createStartElement(TITLE, null, null)); add(ef.createCharacters(file.getName())); add(ef.createEndElement(TITLE, null)); add(ef.createStartElement(ID, null, null)); add(ef.createCharacters(id)); add(ef.createEndElement(ID, null)); Attribute href = ef.createAttribute("href", file.toUri().toASCIIString()); List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", "application/zip")); add(ef.createStartElement(LINK, attrs.iterator(), null)); add(ef.createEndElement(LINK, null)); add(ef.createStartElement(UPDATED, null, null)); add(ef.createCharacters(format(new Date(file.getLastModified())))); add(ef.createEndElement(UPDATED, null)); started = true; return true; } else if (started && !ended && (entry = zip.getNextZipEntry()) != null) { String name = entry.getName(); String link = entryPattern.replace("{entry}", PercentCodec.encode(name)); MimetypesFileTypeMap mimetypes = new javax.activation.MimetypesFileTypeMap(); String type = mimetypes.getContentType(name); if (type == null || type.length() == 0) { type = "application/octet-stream"; } add(ef.createStartElement(ENTRY, null, null)); add(ef.createStartElement(TITLE, null, null)); add(ef.createCharacters(name)); add(ef.createEndElement(TITLE, null)); Attribute href = ef.createAttribute("href", link); List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", type)); add(ef.createStartElement(LINK, attrs.iterator(), null)); add(ef.createEndElement(LINK, null)); long size = entry.getSize(); if (size > 0) { zip.skip(size); } else { while (zip.read(buf, 0, buf.length) >= 0) ; } add(ef.createEndElement(ENTRY, null)); return true; } else if (!ended) { add(ef.createEndElement(FEED, null)); add(ef.createEndDocument()); ended = true; return true; } else { return false; } } catch (IOException e) { throw new XMLStreamException(e); } } }; }