Java Unzip InputStream unzip(InputStream zin, File file)

Here you can find the source of unzip(InputStream zin, File file)

Description

unzip

License

Apache License

Declaration

private static void unzip(InputStream zin, File file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    private static final int BUFFER_SIZE = 8192;

    private static void unzip(InputStream zin, File file) throws IOException {
        FileOutputStream out = null;
        try {//from w w w .  j  a  v a2 s . c  o m
            out = new FileOutputStream(file);
            joinStreams(zin, out);
            out.close();
        } finally {
            failsafeClose(out);
        }
    }

    /**
     * Reads all bytes from the input stream and writes them to the output
     * stream.  Bytes are read and written in chunks.  The caller retains
     * ownership of the streams - i.e. they are not closed by this method.
     *
     * @param input  stream to read from
     * @param output stream to write to
     * @throws IOException on any error
     */
    public static void joinStreams(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];
        int n;

        while ((n = input.read(buffer)) > 0) {
            output.write(buffer, 0, n);
        }
    }

    /**
     * Closes a given closeable if it is not null, and ignores thrown errors.  Useful for finally
     * clauses which perform last-ditch cleanup and don't want to throw (masking earlier errors).
     *
     * @param closeable to be closed
     */
    public static void failsafeClose(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                // Ignored.
            }
        }
    }
}

Related

  1. unzip(InputStream inputStream, String destDirectory)
  2. unzip(InputStream inputStream, String outputFolder)
  3. unzip(InputStream is, File destDir)
  4. unzip(InputStream libInputStream, String path)
  5. unzip(InputStream source, File target)