Example usage for java.util.zip ZipInputStream ZipInputStream

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

Introduction

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

Prototype

public ZipInputStream(InputStream in) 

Source Link

Document

Creates a new ZIP input stream.

Usage

From source file:Main.java

public static void unCompress(String zipPath, String toPath) throws IOException {
    File zipfile = new File(zipPath);
    if (!zipfile.exists())
        return;/*from w  ww  . j  a  v a 2 s . co  m*/

    if (!toPath.endsWith("/"))
        toPath += "/";

    File destFile = new File(toPath);
    if (!destFile.exists())
        destFile.mkdirs();

    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
    ZipEntry entry = null;

    try {

        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                File file = new File(toPath + entry.getName() + "/");
                file.mkdirs();
            } else {
                File file = new File(toPath + entry.getName());
                if (!file.getParentFile().exists())
                    file.getParentFile().mkdirs();

                FileOutputStream fos = null;

                try {
                    fos = new FileOutputStream(file);
                    byte buf[] = new byte[1024];
                    int len = -1;
                    while ((len = zis.read(buf, 0, 1024)) != -1) {
                        fos.write(buf, 0, len);
                    }
                } finally {

                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (Exception e) {
                        }
                    }
                }
            }
        }

    } finally {
        zis.close();
    }

}

From source file:Main.java

public static void unzip(InputStream is, String dir) throws IOException {
    File dest = new File(dir);
    if (!dest.exists()) {
        dest.mkdirs();/*from  w  w w. j a v  a 2  s  .co  m*/
    }

    if (!dest.isDirectory())
        throw new IOException("Invalid Unzip destination " + dest);
    if (null == is) {
        throw new IOException("InputStream is null");
    }

    ZipInputStream zip = new ZipInputStream(is);

    ZipEntry ze;
    while ((ze = zip.getNextEntry()) != null) {
        final String path = dest.getAbsolutePath() + File.separator + ze.getName();

        String zeName = ze.getName();
        char cTail = zeName.charAt(zeName.length() - 1);
        if (cTail == File.separatorChar) {
            File file = new File(path);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    throw new IOException("Unable to create folder " + file);
                }
            }
            continue;
        }

        FileOutputStream fout = new FileOutputStream(path);
        byte[] bytes = new byte[1024];
        int c;
        while ((c = zip.read(bytes)) != -1) {
            fout.write(bytes, 0, c);
        }
        zip.closeEntry();
        fout.close();
    }
}

From source file:Main.java

public static void unZip(String path) {
    int count = -1;
    int index = -1;

    String savepath = "";

    savepath = path.substring(0, path.lastIndexOf("."));
    try {// w  ww .j a  va2 s  .  c  o  m
        BufferedOutputStream bos = null;
        ZipEntry entry = null;
        FileInputStream fis = new FileInputStream(path);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

        while ((entry = zis.getNextEntry()) != null) {
            byte data[] = new byte[buffer];

            String temp = entry.getName();
            index = temp.lastIndexOf("/");
            if (index > -1)
                temp = temp.substring(index + 1);
            String tempDir = savepath + "/zip/";
            File dir = new File(tempDir);
            dir.mkdirs();
            temp = tempDir + temp;
            File f = new File(temp);
            f.createNewFile();

            FileOutputStream fos = new FileOutputStream(f);
            bos = new BufferedOutputStream(fos, buffer);

            while ((count = zis.read(data, 0, buffer)) != -1) {
                bos.write(data, 0, count);
            }

            bos.flush();
            bos.close();
        }

        zis.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Decompresses a given byte array that is a compressed folder.
 * /*from w w w . ja v  a2s .  co m*/
 * @param folderAsCompressedArray to decompress
 * @param unzippedLocation where the decompressed folder should be
 * @throws IOException e
 * @throws FileNotFoundException e
 */
public static void decompressFolderByteArray(byte[] folderAsCompressedArray, File unzippedLocation)
        throws IOException, FileNotFoundException {
    ZipInputStream zipFile = new ZipInputStream(new ByteArrayInputStream(folderAsCompressedArray));
    ZipEntry ze = null;
    final int minusOne = -1;
    while ((ze = zipFile.getNextEntry()) != null) {
        FileOutputStream fout = new FileOutputStream(
                new File(unzippedLocation, ze.getName()).getAbsolutePath());
        for (int c = zipFile.read(); c != minusOne; c = zipFile.read()) {
            fout.write(c);
        }
        zipFile.closeEntry();
        fout.close();
    }
    zipFile.close();
}

From source file:Main.java

public static void unzip(final InputStream input, final File destFolder) {
    try {//  w  w  w.  j a va  2s . c  o m
        byte[] buffer = new byte[4096];
        int read;
        ZipInputStream is = new ZipInputStream(input);
        ZipEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                String fileName = entry.getName();
                File fileFolder = destFolder;
                int lastSep = entry.getName().lastIndexOf(File.separatorChar);
                if (lastSep != -1) {
                    String dirPath = fileName.substring(0, lastSep);
                    fileFolder = new File(fileFolder, dirPath);
                    fileName = fileName.substring(lastSep + 1);
                }
                fileFolder.mkdirs();
                File file = new File(fileFolder, fileName);
                FileOutputStream os = new FileOutputStream(file);
                while ((read = is.read(buffer)) != -1) {
                    os.write(buffer, 0, read);
                }
                os.flush();
                os.close();
            }
        }
        is.close();
    } catch (Exception ex) {
        throw new RuntimeException("Cannot unzip stream to " + destFolder.getAbsolutePath(), ex);
    }
}

From source file:Main.java

private static List<String> extractClassNames(String path) throws IOException, NoSuchMethodException,
        SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    addToClassPath(path);/*from  ww  w .  j  ava2  s .  c  o m*/
    List<String> classNames = new ArrayList<String>();
    ZipInputStream zipStream = new ZipInputStream(new FileInputStream(path));
    try {
        for (ZipEntry entry = zipStream.getNextEntry(); entry != null; entry = zipStream.getNextEntry()) {
            if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
                String className = entry.getName().replace('/', '.');
                classNames.add(className.substring(0, className.length() - ".class".length()));
            }
        }
        return classNames;
    } finally {
        zipStream.close();
    }
}

From source file:Main.java

/**
 * /*from www .j  a  va  2  s.c  o  m*/
 * @param zipFile
 *            zip file
 * @param folderName
 *            folder to load
 * @return list of entry in the folder
 * @throws ZipException
 * @throws IOException
 */
public static List<ZipEntry> loadFiles(File zipFile, String folderName) throws ZipException, IOException {
    Log.d("loadFiles", folderName);
    long start = System.currentTimeMillis();
    FileInputStream fis = new FileInputStream(zipFile);
    BufferedInputStream bis = new BufferedInputStream(fis);
    ZipInputStream zis = new ZipInputStream(bis);

    ZipEntry zipEntry = null;
    List<ZipEntry> list = new LinkedList<ZipEntry>();
    String folderLowerCase = folderName.toLowerCase();
    int counter = 0;
    while ((zipEntry = zis.getNextEntry()) != null) {
        counter++;
        String zipEntryName = zipEntry.getName();
        Log.d("loadFiles.zipEntry.getName()", zipEntryName);
        if (zipEntryName.toLowerCase().startsWith(folderLowerCase) && !zipEntry.isDirectory()) {
            list.add(zipEntry);
        }
    }
    Log.d("Loaded 1", counter + " files, took: " + (System.currentTimeMillis() - start) + " milliseconds.");
    zis.close();
    bis.close();
    fis.close();
    return list;
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();/*from www . ja  v  a2s. c  om*/
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {

        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);

        //create all non exists folders
        //else you will hit FileNotFoundException for compressed folder
        if (ze.isDirectory())
            newFile.mkdirs();
        else {
            newFile.getParentFile().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();

}

From source file:Main.java

public static void unZip(InputStream zipFileIS, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();//from  w  ww  .  j  a v a 2s  .c o m
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(zipFileIS);
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {

        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.flush();
            fos.close();
        }
        ze = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.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;/*from   w  ww  .j  a va2 s .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();
}