Here you can find the source of getZipEntryByteContent(ZipEntry ze, ZipFile zip)
Parameter | Description |
---|---|
IOException | if a problem occured reading the zip entry. |
public static byte[] getZipEntryByteContent(ZipEntry ze, ZipFile zip) throws IOException
//package com.java2s; /******************************************************************************* * Copyright ? 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/* w ww . j a v a 2 s. c o m*/ * IBM Corporation - initial API and implementation * *******************************************************************************/ import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main { private static final int DEFAULT_READING_SIZE = 8192; /** * Returns the contents of the given zip entry as a byte array. * @throws IOException if a problem occured reading the zip entry. */ public static byte[] getZipEntryByteContent(ZipEntry ze, ZipFile zip) throws IOException { InputStream stream = null; try { stream = new BufferedInputStream(zip.getInputStream(ze)); return getInputStreamAsByteArray(stream, (int) ze.getSize()); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { } } } } /** * Returns the given input stream's contents as a byte array. * If a length is specified (ie. if length != -1), only length bytes * are returned. Otherwise all bytes in the stream are returned. * Note this doesn't close the stream. * @throws IOException if a problem occured reading the stream. */ public static byte[] getInputStreamAsByteArray(InputStream stream, int length) throws IOException { byte[] contents; if (length == -1) { contents = new byte[0]; int contentsLength = 0; int amountRead = -1; do { int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); // read at least 8K // resize contents if needed if (contentsLength + amountRequested > contents.length) { System.arraycopy(contents, 0, contents = new byte[contentsLength + amountRequested], 0, contentsLength); } // read as many bytes as possible amountRead = stream.read(contents, contentsLength, amountRequested); if (amountRead > 0) { // remember length of contents contentsLength += amountRead; } } while (amountRead != -1); // resize contents if necessary if (contentsLength < contents.length) { System.arraycopy(contents, 0, contents = new byte[contentsLength], 0, contentsLength); } } else { contents = new byte[length]; int len = 0; int readSize = 0; while ((readSize != -1) && (len != length)) { // See PR 1FMS89U // We record first the read size. In this case len is the actual read size. len += readSize; readSize = stream.read(contents, len, length - len); } } return contents; } }