/** * Returns an InputStream for reading the contents of the given entry. * * @param ze the entry to get the stream for. * @return a stream to read the entry from. * @throws IOException if unable to create an input stream from the zipenty * @throws ZipException if the zipentry uses an unsupported feature */ public java.io.InputStream getInputStream(ZipArchiveEntry ze) //throws IOException, ZipException { IAC_OffsetEntry offsetEntry = entries.get(ze); if (offsetEntry == null) { return(null); } ZipUtil.checkRequestedFeatures(ze); long start = offsetEntry.dataOffset; BoundedInputStream bis = new BoundedInputStream(this, start, ze.getCompressedSize()); switch (ze.getMethod()) { case ZipArchiveEntry.STORED: return(bis); case ZipArchiveEntry.DEFLATED: bis.addDummy(); return(new java.util.zip.InflaterInputStream(bis, new java.util.zip.Inflater(true))); default: throw new java.util.zip.ZipException("Found unsupported compression method " + ze.getMethod()); } }
/** * Writes bytes to ZIP entry. * @param b the byte array to write * @param offset the start position to write from * @param length the number of bytes to write * @throws IOException on error */ public override void write(byte[] b, int offset, int length) //throws IOException { ZipUtil.checkRequestedFeatures(entry); if (entry.getMethod() == DEFLATED) { if (length > 0) { if (!def.finished()) { if (length <= DEFLATER_BLOCK_SIZE) { def.setInput(b, offset, length); deflateUntilInputIsNeeded(); } else { int fullblocks = length / DEFLATER_BLOCK_SIZE; for (int i = 0; i < fullblocks; i++) { def.setInput(b, offset + i * DEFLATER_BLOCK_SIZE, DEFLATER_BLOCK_SIZE); deflateUntilInputIsNeeded(); } int done = fullblocks * DEFLATER_BLOCK_SIZE; if (done < length) { def.setInput(b, offset + done, length - done); deflateUntilInputIsNeeded(); } } } } } else { writeOut(b, offset, length); written += length; } crc.update(b, offset, length); count(length); }
public override int read(byte[] buffer, int start, int length) //throws IOException { if (closed) { throw new java.io.IOException("The stream is closed"); } if (inf.finished() || current == null) { return(-1); } // avoid int overflow, check null buffer if (start <= buffer.Length && length >= 0 && start >= 0 && buffer.Length - start >= length) { ZipUtil.checkRequestedFeatures(current); if (!supportsDataDescriptorFor(current)) { throw new UnsupportedZipFeatureException(Feature.DATA_DESCRIPTOR, current); } if (current.getMethod() == ZipArchiveOutputStream.STORED) { if (hasDataDescriptor) { if (lastStoredEntry == null) { readStoredEntry(); } return(lastStoredEntry.read(buffer, start, length)); } int csize = (int)current.getSize(); if (readBytesOfEntry >= csize) { return(-1); } if (offsetInBuffer >= lengthOfLastRead) { offsetInBuffer = 0; if ((lengthOfLastRead = inJ.read(buf)) == -1) { return(-1); } count(lengthOfLastRead); bytesReadFromStream += lengthOfLastRead; } int toRead = length > lengthOfLastRead ? lengthOfLastRead - offsetInBuffer : length; if ((csize - readBytesOfEntry) < toRead) { toRead = csize - readBytesOfEntry; } java.lang.SystemJ.arraycopy(buf, offsetInBuffer, buffer, start, toRead); offsetInBuffer += toRead; readBytesOfEntry += toRead; crc.update(buffer, start, toRead); return(toRead); } if (inf.needsInput()) { fill(); if (lengthOfLastRead > 0) { bytesReadFromStream += lengthOfLastRead; } } int read = 0; try { read = inf.inflate(buffer, start, length); } catch (java.util.zip.DataFormatException e) { throw new java.util.zip.ZipException(e.getMessage()); } if (read == 0) { if (inf.finished()) { return(-1); } else if (lengthOfLastRead == -1) { throw new java.io.IOException("Truncated ZIP file"); } } crc.update(buffer, start, read); return(read); } throw new java.lang.ArrayIndexOutOfBoundsException(); }