Java Unzip InputStream unzip(InputStream is, File destDir)

Here you can find the source of unzip(InputStream is, File destDir)

Description

unzip

License

LGPL

Declaration

public static void unzip(InputStream is, File destDir) throws IOException 

Method Source Code


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

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 is, File destDir) throws IOException {
        final int BUFFER = 2048;
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
        try {/*from w w  w  .j  ava 2  s .  co  m*/
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    File dir = new File(destDir, entry.getName());
                    if (!dir.mkdir()) {
                        throw new IOException("Failed to create directory " + dir);
                    }
                } else {
                    int count;
                    byte contents[] = new byte[BUFFER];
                    // write the files to the disk
                    FileOutputStream fos = new FileOutputStream(new File(destDir, entry.getName()));
                    try {
                        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
                        try {
                            while ((count = zis.read(contents, 0, BUFFER)) != -1) {
                                dest.write(contents, 0, count);
                            }
                        } finally {
                            dest.close();
                        }
                    } finally {
                        fos.close();
                    }
                }
            }
        } finally {
            zis.close();
        }
    }
}

Related

  1. unzip(InputStream in, File toDir)
  2. unzip(InputStream input, File dest)
  3. unzip(InputStream input, String targetDir)
  4. unzip(InputStream inputStream, String destDirectory)
  5. unzip(InputStream inputStream, String outputFolder)
  6. unzip(InputStream libInputStream, String path)
  7. unzip(InputStream source, File target)
  8. unzip(InputStream zin, File file)