Java Unzip InputStream decompress(InputStream input, File destDir)

Here you can find the source of decompress(InputStream input, File destDir)

Description

decompress

License

Open Source License

Declaration

public static void decompress(InputStream input, File destDir) throws IOException 

Method Source Code


//package com.java2s;
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2013 OpenPlans//from  w ww.j  a  v a 2s.  c o  m
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Main {
    public static void decompress(InputStream input, File destDir) throws IOException {
        ZipInputStream zin = new ZipInputStream(input);
        ZipEntry entry = null;

        byte[] buffer = new byte[1024];
        while ((entry = zin.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
                continue;
            }

            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));

            int n = -1;
            while ((n = zin.read(buffer)) != -1) {
                out.write(buffer, 0, n);
            }

            out.flush();
            out.close();
        }
    }

    public static void decompress(final File inputFile, final File destDir) throws IOException {
        ZipFile zipFile = new ZipFile(inputFile);

        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            InputStream stream = zipFile.getInputStream(entry);

            if (entry.isDirectory()) {
                // Assume directories are stored parents first then children.
                (new File(destDir, entry.getName())).mkdir();
                continue;
            }

            File newFile = new File(destDir, entry.getName());
            FileOutputStream fos = new FileOutputStream(newFile);
            try {
                byte[] buf = new byte[1024];
                int len;

                while ((len = stream.read(buf)) >= 0)
                    saveCompressedStream(buf, fos, len);

            } catch (IOException e) {
                zipFile.close();
                IOException ioe = new IOException("Not valid COAMPS archive file type.");
                ioe.initCause(e);
                throw ioe;
            } finally {
                fos.flush();
                fos.close();

                stream.close();
            }
        }
        zipFile.close();
    }

    /**
     * @param len 
     * @param stream
     * @param fos
     * @throws IOException
     */
    public static void saveCompressedStream(final byte[] buffer, final OutputStream out, final int len)
            throws IOException {
        try {
            out.write(buffer, 0, len);

        } catch (Exception e) {
            out.flush();
            out.close();
            IOException ioe = new IOException("Not valid archive file type.");
            ioe.initCause(e);
            throw ioe;
        }
    }
}

Related

  1. decompress(ByteArrayInputStream xzStream)
  2. decompress(final int type, final InputStream is, final OutputStream os)
  3. decompress(InputStream inputStream)
  4. decompress(InputStream is, OutputStream os)
  5. decompressFile(InputStream is, OutputStream os)
  6. decompressStream(InputStream input)