Example usage for java.util.zip ZipInputStream read

List of usage examples for java.util.zip ZipInputStream read

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream read.

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:org.vulpe.commons.util.VulpeZipUtil.java

public static byte[] zip(final Map<String, FileInputStream> streams, final String outputFile) {
    byte[] buffer = new byte[1024];

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from w ww. j  a  v  a  2 s.c om*/
        final FileOutputStream fos = new FileOutputStream(outputFile);

        final ZipOutputStream out = new ZipOutputStream(fos);
        for (final String key : streams.keySet()) {
            final FileInputStream stream = streams.get(key);
            final ZipInputStream zin = new ZipInputStream(
                    new ByteArrayInputStream(IOUtils.toByteArray(stream)));
            out.putNextEntry(new ZipEntry(key));
            int len = 0;
            while ((len = zin.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.closeEntry();
            zin.close();
            stream.close();
        }

        out.close();

    } catch (IOException e) {
    }
    return baos.toByteArray();
}

From source file:io.vertx.config.vault.utils.VaultDownloader.java

private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[4096];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);// www.j  av a  2  s.co m
    }
    bos.close();
}

From source file:Main.java

public static void unzip(String zipFilePath, String fileName) {

    try {//w w w. j a  v a 2s  .  c  o m
        FileInputStream fin = new FileInputStream(zipFilePath);
        ZipInputStream zin = new ZipInputStream(fin);

        do {
            // no-op
        } while (!zin.getNextEntry().getName().equals(fileName));

        OutputStream os = new FileOutputStream(fileName);

        byte[] buffer = new byte[1024];
        int length;

        //read the entry from zip file and extract it to disk
        while ((length = zin.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
        os.close();
        zin.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:Main.java

private static void extractFile(ZipInputStream in, File outdir, String name) throws IOException {
    byte[] buffer = new byte[4096];

    final File file = new File(outdir, name);
    try (final FileOutputStream fos = new FileOutputStream(file)) {
        try (final BufferedOutputStream out = new BufferedOutputStream(fos)) {
            int count;
            while ((count = in.read(buffer)) != -1) {
                out.write(buffer, 0, count);
            }//from  w  ww  .j  a  v  a  2s .  co m
        }
    }
}

From source file:com.worldline.easycukes.commons.helpers.FileHelper.java

/**
 * Extracts a specific file from a zip folder
 *
 * @param zip      a {@link ZipInputStream} corresponding to the zip folder to be
 *                 extracted/*from  w  ww  .j  a  v a  2 s  .  c o  m*/
 * @param filePath the path of the file to be extracted from the zip folder
 * @throws IOException if anything's going wrong while extracting the content of the
 *                     zip
 */
private static void extractFile(@NonNull ZipInputStream zip, @NonNull String filePath) throws IOException {
    @Cleanup
    final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    final byte[] bytesIn = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = zip.read(bytesIn)) != -1)
        bos.write(bytesIn, 0, read);
}

From source file:Main.java

public static void extractEntryContent(ZipInputStream zis, ZipEntry entry, String unzipdir)
        throws IOException, FileNotFoundException {

    String entryFileName = entry.getName();
    String entryPath = unzipdir + File.separator + entryFileName;

    createFile(entryPath);//from  ww  w.  j  a  v a  2 s.com

    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entryPath));

    byte[] buffer = new byte[1024];
    int count = -1;
    while ((count = zis.read(buffer)) != -1) {
        bos.write(buffer, 0, count);
    }

    bos.close();
}

From source file:Main.java

public static void Unzip(String dir, byte[] data) throws Exception {
    ByteArrayInputStream input = new ByteArrayInputStream(data);
    ZipInputStream zipIn = new ZipInputStream(input);
    ZipEntry entry;// w  w  w  .  j a v a 2s .co  m
    while ((entry = zipIn.getNextEntry()) != null) {
        String outpath = dir + entry.getName();
        FileOutputStream output = null;
        try {
            File f = new File(outpath);
            f.getParentFile().mkdirs();

            output = new FileOutputStream(f);
            int len = 0;
            byte[] buffer = new byte[4096];
            while ((len = zipIn.read(buffer)) > 0) {
                output.write(buffer, 0, len);
            }
        } finally {
            if (output != null)
                output.close();
        }
    }

    zipIn.close();
}

From source file:Main.java

/**
 * Reads the in_stream and extracts them to out_dir.
 * @param in_stream Input stream corresponding to the zip file.
 * @param out_dir Output directory for the zip file contents.
 * @throws IOException/*from  w  ww.j  av  a  2s.  co  m*/
 */
public static void zipExtract(InputStream in_stream, File out_dir) throws IOException {
    if (!out_dir.exists()) {
        if (!out_dir.mkdirs()) {
            throw new IOException("Could not create output directory: " + out_dir.getAbsolutePath());
        }
    }
    ZipInputStream zis = new ZipInputStream(in_stream);
    ZipEntry ze;
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            File fmd = new File(out_dir.getAbsolutePath() + "/" + ze.getName());
            fmd.mkdirs();
            continue;
        }
        FileOutputStream fout = new FileOutputStream(out_dir.getAbsolutePath() + "/" + ze.getName());
        while ((count = zis.read(buffer)) != -1) {
            fout.write(buffer, 0, count);
        }

        fout.close();
        zis.closeEntry();
    }
    zis.close();
}

From source file:Main.java

public static byte[] readBytesFormZipFile(String fileName, String fname) {
    byte[] bytes = null;
    ByteArrayOutputStream os = null;
    ZipInputStream in = null;
    try {//from   w w w .j av  a 2s. com
        os = new ByteArrayOutputStream(4096);
        in = new ZipInputStream(new FileInputStream(fileName));
        boolean found = false;
        while (!found) {
            ZipEntry entry = in.getNextEntry();
            if (fname.equalsIgnoreCase(entry.getName())) {
                found = true;
            }
        }
        int read;
        byte[] buffer = new byte[4096];
        while ((read = in.read(buffer)) >= 0) {
            os.write(buffer, 0, read);
        }
        bytes = os.toByteArray();
    } catch (Exception e) {
        bytes = null;
    } finally {
        try {
            if (os != null) {
                os.flush();
                os = null;
            }
            if (in != null) {
                in.close();
                in = null;
            }
        } catch (Exception e) {
        }
    }

    return bytes;
}

From source file:prototypes.ws.proxy.soap.commons.io.Files.java

/**
 * Extracts a zip entry (file entry)/*from   w  w  w. j a  v  a  2  s .com*/
 *
 * @param zipIn
 * @param filePath
 * @throws IOException
 */
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
}