Java Unzip InputStream unzip(InputStream in, File destination)

Here you can find the source of unzip(InputStream in, File destination)

Description

unzip

License

Open Source License

Declaration

public static void unzip(InputStream in, File destination) throws IOException 

Method Source Code

//package com.java2s;
/**/*  w  w w  . j  a v a2 s  .c  om*/
 * Copyright (c) 2016 Couchbase, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    public static void unzip(InputStream in, File destination) throws IOException {
        byte[] buffer = new byte[1024];
        ZipInputStream zis = new ZipInputStream(in);
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(destination, fileName);
            if (ze.isDirectory()) {
                newFile.mkdirs();
            } else {
                new File(newFile.getParent()).mkdirs();
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
        in.close();
    }
}

Related

  1. decompressFile(InputStream is, OutputStream os)
  2. decompressStream(InputStream input)
  3. decompressZipArchive(final File zipFile, final File rootDirectoryToUnZip)
  4. unzip(InputStream from, String to, String pattern)
  5. unzip(InputStream in)
  6. unzip(InputStream in, File toDir)
  7. unzip(InputStream input, File dest)
  8. unzip(InputStream input, String targetDir)
  9. unzip(InputStream inputStream, String destDirectory)