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:Utils.java

/**
 * unpack a segment from a zip/*w ww .  j  a v a 2  s .c  o m*/
 * 
 * @param addsi
 * @param packetStream
 * @param version
 */
public static void unpack(InputStream source, File destination) throws IOException {
    ZipInputStream zin = new ZipInputStream(source);
    ZipEntry zipEntry = null;
    FileOutputStream fout = null;

    byte[] buffer = new byte[4096];
    while ((zipEntry = zin.getNextEntry()) != null) {

        long ts = zipEntry.getTime();
        // the zip entry needs to be a full path from the
        // searchIndexDirectory... hence this is correct

        File f = new File(destination, zipEntry.getName());

        f.getParentFile().mkdirs();

        fout = new FileOutputStream(f);
        int len;
        while ((len = zin.read(buffer)) > 0) {
            fout.write(buffer, 0, len);
        }
        zin.closeEntry();
        fout.close();
        f.setLastModified(ts);
    }
    fout.close();
}

From source file:Main.java

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    try {/*from  w w w.j  a  v a2 s.c  o  m*/
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

public static void unzip(InputStream fin, String targetPath, Context context) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin));
    try {//w  w  w  . java 2 s. c  o m
        ZipEntry zipEntry;
        int count;
        byte[] buffer = new byte[8192];
        while ((zipEntry = zis.getNextEntry()) != null) {
            File file = new File(targetPath, zipEntry.getName());
            File dir = zipEntry.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs()) {
                throw new FileNotFoundException("Failed to get directory: " + dir.getAbsolutePath());
            }
            if (zipEntry.isDirectory()) {
                continue;
            }
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
            Log.d("TEST", "Unzipped " + file.getAbsolutePath());
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

public static void readZipFile(String file) throws Exception {
    ZipFile zf = new ZipFile(file);
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry ze;/*from w  w  w .  j av a2s .  c om*/
    while ((ze = zin.getNextEntry()) != null) {
        if (ze.isDirectory()) {
        } else {
            System.err.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes");
            long size = ze.getSize();
            if (size > 0) {
                BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
            }
            System.out.println();
        }
    }
    zin.closeEntry();
}

From source file:Main.java

public static void unzip(String zipFileName, String unzipdir) {
    try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)))) {

        ZipEntry entry = null;//from ww w  .  j  a  va2s.  c o  m
        while ((entry = zis.getNextEntry()) != null) {
            // Extract teh entry's contents 
            extractEntryContent(zis, entry, unzipdir);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/***
 * Extract zipfile to outdir with complete directory structure
 * disclaimer, I didn't wrote that ...//from w w w  . j  av a2s.  co m
 * @param zipfile Input .zip file
 * @param outdir Output directory
 */
public static void extract(File zipfile, File outdir) {
    // todo: replace with more trustful method
    try {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zipfile));
        ZipEntry entry;
        String name, dir;
        while ((entry = zin.getNextEntry()) != null) {
            name = entry.getName();
            if (entry.isDirectory()) {
                mkdirs(outdir, name);
                continue;
            }
            dir = dirpart(name);
            if (dir != null) {
                mkdirs(outdir, dir);
            }
            extractFile(zin, outdir, name);
        }
        zin.close();
    } catch (IOException err) {
        throw new RuntimeException("Unable to extract quiz", err);
    }
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) {
    byte[] buffer = new byte[BUFFER_SIZE];

    try {/*  w  w  w . java2 s.  c o  m*/
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            System.out.println("file unzip : " + newFile.getAbsoluteFile());
            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.close();
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
        System.out.println("Done");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * kudos: http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)
 * @param zipFile/*from www.  ja  v a  2 s  . c  o m*/
 * @param destinationDirectory
 */
public static void unZip(String zipFile, String destinationDirectory) {
    try {
        FileInputStream fin = new FileInputStream(zipFile);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {
            Log.v("Decompress", "Unzipping " + ze.getName());
            String destinationPath = destinationDirectory + File.separator + ze.getName();
            if (ze.isDirectory()) {
                dirChecker(destinationPath);
            } else {
                FileOutputStream fout;
                try {
                    File outputFile = new File(destinationPath);
                    if (!outputFile.getParentFile().exists()) {
                        dirChecker(outputFile.getParentFile().getPath());
                    }
                    fout = new FileOutputStream(destinationPath);
                    for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }
                    zin.closeEntry();
                    fout.close();
                } catch (Exception e) {
                    // ok for now.
                    Log.v("Decompress", "Error: " + e.getMessage());
                }
            }
        }
        zin.close();
    } catch (Exception e) {
        Log.e("Decompress", "unzip", e);
    }
}

From source file:Main.java

/***
 * Extract zipfile to outdir with complete directory structure
 * @param zipfile Input .zip file/* w w w  .j  ava2s  .  c  om*/
 * @param outdir Output directory
 */
public static void extract(InputStream zipfile, File outdir) {
    try {
        ZipInputStream zin = new ZipInputStream(zipfile);
        ZipEntry entry;
        String name, dir;
        Log.i("OF", "uncompressinggggg ");
        while ((entry = zin.getNextEntry()) != null) {
            name = entry.getName();
            if (entry.isDirectory()) {
                mkdirs(outdir, name);
                continue;
            }
            dir = dirpart(name);
            if (dir != null)
                mkdirs(outdir, dir);

            extractFile(zin, outdir, name);
        }
        zin.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/***
 * Extract zipfile to outdir with complete directory structure
 * @param zipfile Input .zip file//  w  w  w . jav  a  2 s.co m
 * @param outdir Output directory
 */
public static void extract(InputStream zipfile, File outdir) {
    try {
        ZipInputStream zin = new ZipInputStream(zipfile);
        ZipEntry entry;
        String name, dir;
        Log.i("OF", "uncompressinggggg ");
        while ((entry = zin.getNextEntry()) != null) {
            name = entry.getName();
            if (entry.isDirectory()) {
                mkdirs(outdir, name);
                continue;
            }
            /* this part is necessary because file entry can come before
             * directory entry where is file located
             * i.e.:
             *   /foo/foo.txt
             *   /foo/
             */
            dir = dirpart(name);
            if (dir != null)
                mkdirs(outdir, dir);

            extractFile(zin, outdir, name);
        }
        zin.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}