Example usage for java.util.zip ZipInputStream getNextEntry

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

Introduction

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

Prototype

public ZipEntry getNextEntry() throws IOException 

Source Link

Document

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Usage

From source file:Main.java

/**
 * Reads the in_stream and extracts them to out_dir.
 * @param in_stream Input stream corresponding to the zip file.
 * @param out_dir Output directory for the zip file contents.
 * @throws IOException//from  www  . ja  v  a  2  s.c  om
 */
public static void zipExtract(InputStream in_stream, File out_dir) throws IOException {
    if (!out_dir.exists()) {
        if (!out_dir.mkdirs()) {
            throw new IOException("Could not create output directory: " + out_dir.getAbsolutePath());
        }
    }
    ZipInputStream zis = new ZipInputStream(in_stream);
    ZipEntry ze;
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            File fmd = new File(out_dir.getAbsolutePath() + "/" + ze.getName());
            fmd.mkdirs();
            continue;
        }
        FileOutputStream fout = new FileOutputStream(out_dir.getAbsolutePath() + "/" + ze.getName());
        while ((count = zis.read(buffer)) != -1) {
            fout.write(buffer, 0, count);
        }

        fout.close();
        zis.closeEntry();
    }
    zis.close();
}

From source file:Main.java

/***
 * Extract zipfile to outdir with complete directory structure
 * disclaimer, I didn't wrote that ...//from  ww w  .j a v  a  2s. 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:io.vertx.config.vault.utils.VaultDownloader.java

private static void unzip(File zipFilePath, File destDir) throws IOException {
    if (!destDir.exists()) {
        destDir.mkdir();//ww w.j a v a  2s. c  o  m
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDir.getAbsolutePath() + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:com.google.gdt.eclipse.designer.gwtext.actions.ConfigureGwtExtOperation.java

private static void extractZip(InputStream zipFile, IFolder targetFolder) throws Exception {
    ZipInputStream zipInputStream = new ZipInputStream(zipFile);
    while (true) {
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        if (zipEntry == null) {
            break;
        }//from   w  w  w. ja v  a2  s  .c o m
        if (!zipEntry.isDirectory()) {
            String entryName = zipEntry.getName();
            byte[] byteArray = IOUtils.toByteArray(zipInputStream);
            IOUtils2.setFileContents(targetFolder.getFile(new Path(entryName)),
                    new ByteArrayInputStream(byteArray));
        }
        zipInputStream.closeEntry();
    }
    zipInputStream.close();
}

From source file:Utils.java

/**
 * unpack a segment from a zip//from  w  ww  . ja  v  a  2s.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:com.joliciel.talismane.parser.ParsingConstrainerImpl.java

public static ParsingConstrainer loadFromFile(File inFile) {
    try {/*from   ww w  .  jav a  2s  . c  om*/
        ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
        zis.getNextEntry();
        ObjectInputStream in = new ObjectInputStream(zis);
        ParsingConstrainer parsingConstrainer = null;
        try {
            parsingConstrainer = (ParsingConstrainer) in.readObject();
        } catch (ClassNotFoundException e) {
            LogUtils.logError(LOG, e);
            throw new RuntimeException(e);
        }
        return parsingConstrainer;
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    }
}

From source file:Main.java

public static void unZipFolder(InputStream input, String outPathString) throws Exception {
    java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(input);
    java.util.zip.ZipEntry zipEntry = null;
    String szName = "";

    while ((zipEntry = inZip.getNextEntry()) != null) {
        szName = zipEntry.getName();//from  ww  w  .j ava  2s  . com

        if (zipEntry.isDirectory()) {

            // get the folder name of the widget
            szName = szName.substring(0, szName.length() - 1);
            java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);
            folder.mkdirs();

        } else {

            java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);
            file.createNewFile();
            // get the output stream of the file
            java.io.FileOutputStream out = new java.io.FileOutputStream(file);
            int len;
            byte[] buffer = new byte[1024];
            // read (len) bytes into buffer
            while ((len = inZip.read(buffer)) != -1) {
                // write (len) byte from buffer at the position 0
                out.write(buffer, 0, len);
                out.flush();
            }
            out.close();
        }
    } //end of while

    inZip.close();
}

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 ww w  . j a v  a 2  s .  c  o 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

/***
 * Extract zipfile to outdir with complete directory structure
 * @param zipfile Input .zip file/*from  ww  w.jav  a2 s. c o  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;
            }
            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/* www .  ja v  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();
    }
}