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:com.comcast.magicwand.spells.web.iexplore.IePhoenixDriver.java

private static void extract(File src, String dstDir) throws IOException {
    ZipInputStream zis = null;
    ZipEntry entry;//from   w  ww . jav a2s  .c  o  m

    try {
        zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(src)));

        while (null != (entry = zis.getNextEntry())) {
            File dst = Paths.get(dstDir, entry.getName()).toFile();
            FileOutputStream fos = new FileOutputStream(dst);

            try {
                IOUtils.copy(zis, fos);
                fos.close();
            } finally {
                IOUtils.closeQuietly(fos);
            }
        }

        zis.close();
    } finally {
        IOUtils.closeQuietly(zis);
    }
}

From source file:com.oprisnik.semdroid.app.parser.manifest.AXMLConverter.java

public static String getManifestString(byte[] apk) {
    String result = null;/*from   w w w . j  av  a  2  s .  c o  m*/
    ZipInputStream zis = null;
    ByteArrayInputStream fis = null;
    try {

        fis = new ByteArrayInputStream(apk);

        zis = new ZipInputStream(fis);
        for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
            if (entry.getName().equals("AndroidManifest.xml")) {
                result = getManifestString(zis);
                break;
            }
        }
    } catch (FileNotFoundException e) {
        IOUtils.closeQuietly(fis);
    } catch (IOException e) {
    } finally {
        IOUtils.closeQuietly(zis);
    }
    return result;
}

From source file:Main.java

private static void loadICUData(Context context, File destDir) throws IOException {
    OutputStream out = null;/*from  w  w  w.  j  av a  2  s.  co m*/
    ZipInputStream in = null;
    File icuDir = new File(destDir, "icu");
    File icuDataFile = new File(icuDir, "icudt53l.dat");
    try {
        if (!icuDir.exists())
            icuDir.mkdirs();
        if (!icuDataFile.exists()) {
            in = new ZipInputStream(context.getAssets().open("icudt53l.zip"));
            in.getNextEntry();
            out = new FileOutputStream(icuDataFile);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
    } catch (IOException e) {
        if (icuDataFile.exists())
            icuDataFile.delete();
        throw e;
    } finally {
        if (in != null)
            in.close();
        if (out != null) {
            out.flush();
            out.close();
        }
    }
}

From source file:de.micromata.genome.gdbfs.FileSystemUtils.java

/**
 * Copy from zip./*w w w .j  av  a  2  s  . co m*/
 *
 * @param zipIn the zip in
 * @param target the target
 */
public static void copyFromZip(InputStream zipIn, FsObject target) {
    ZipInputStream zin = new ZipInputStream(zipIn);
    try {
        while (true) {
            ZipEntry ze = zin.getNextEntry();
            if (ze == null) {
                break;
            }
            copyFromZip(ze, zin, target);
            zin.closeEntry();
        }
    } catch (IOException ex) {
        throw new RuntimeIOException(ex);
    }
}

From source file:com.ariatemplates.attester.maven.ArtifactExtractor.java

public static void unzip(File zipFile, File outputFolder) throws IOException {
    ZipInputStream zipInputStream = null;
    try {//w ww  .  ja  v  a2  s  .  c  o  m
        ZipEntry entry = null;
        zipInputStream = new ZipInputStream(FileUtils.openInputStream(zipFile));
        while ((entry = zipInputStream.getNextEntry()) != null) {
            File outputFile = new File(outputFolder, entry.getName());

            if (entry.isDirectory()) {
                outputFile.mkdirs();
                continue;
            }

            OutputStream outputStream = null;
            try {
                outputStream = FileUtils.openOutputStream(outputFile);
                IOUtils.copy(zipInputStream, outputStream);
                outputStream.close();
            } catch (IOException exception) {
                outputFile.delete();
                throw new IOException(exception);
            } finally {
                IOUtils.closeQuietly(outputStream);
            }
        }
        zipInputStream.close();
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
}

From source file:com.joliciel.talismane.extensions.corpus.PosTaggerStatistics.java

public static PosTaggerStatistics loadFromFile(File inFile) {
    try {// w w  w.  j a va 2  s  . com
        ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
        zis.getNextEntry();
        ObjectInputStream in = new ObjectInputStream(zis);
        PosTaggerStatistics stats = null;
        try {
            stats = (PosTaggerStatistics) in.readObject();
        } catch (ClassNotFoundException e) {
            LogUtils.logError(LOG, e);
            throw new RuntimeException(e);
        }
        return stats;
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    }
}

From source file:net.grinder.util.LogCompressUtils.java

/**
 * Decompress the given the {@link InputStream} into the given {@link OutputStream}.
 *
 * @param inputStream  input stream of the compressed file
 * @param outputStream file to be written
 * @param limit        the limit of the output
 *//*from  w  w  w.  j av a  2  s.co m*/
public static void decompress(InputStream inputStream, OutputStream outputStream, long limit) {
    ZipInputStream zipInputStream = null;
    try {
        zipInputStream = new ZipInputStream(inputStream);
        byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
        int count;
        long total = 0;
        checkNotNull(zipInputStream.getNextEntry(), "In zip, it should have at least one entry");
        do {
            while ((count = zipInputStream.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
                total += count;
                if (total >= limit) {
                    break;
                }
                outputStream.write(buffer, 0, count);
            }
        } while (zipInputStream.getNextEntry() != null);
        outputStream.flush();
    } catch (IOException e) {
        LOGGER.error("Error occurs while decompressing {}", e.getMessage());
        LOGGER.debug("Details : ", e);
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
}

From source file:cc.recommenders.utils.gson.GsonUtil.java

public static <T> List<T> deserializeZip(File zip, Class<T> classOfT) throws IOException {

    List<T> res = Lists.newLinkedList();
    ZipInputStream zis = null;
    try {/* w  w  w .  ja  v  a 2 s  .  c  om*/
        InputSupplier<FileInputStream> fis = Files.newInputStreamSupplier(zip);
        zis = new ZipInputStream(fis.getInput());
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final InputStreamReader reader = new InputStreamReader(zis);
                final T data = getInstance().fromJson(reader, classOfT);
                res.add(data);
            }
        }
    } finally {
        Closeables.closeQuietly(zis);
    }
    return res;
}

From source file:abfab3d.shapejs.Project.java

private static void extractZip(String zipFile, String outputFolder, Map<String, String> sceneFiles,
        List<String> resources) {
    byte[] buffer = new byte[1024];

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

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

        while (ze != null) {
            // Ignore directories
            if (ze.isDirectory())
                continue;

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            System.out.println("file unzip : " + newFile.getAbsoluteFile());

            FileOutputStream fos = new FileOutputStream(newFile);

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

            // Save path to the script and parameters files
            if (fileName.endsWith(".json")) {
                sceneFiles.put("paramFile", newFile.getAbsolutePath());
            } else if (fileName.endsWith(".js")) {
                sceneFiles.put("scriptFile", newFile.getAbsolutePath());
            } else {
                resources.add(newFile.getAbsolutePath());
            }

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

        zis.closeEntry();
        zis.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.nuvolect.securesuite.util.OmniZip.java

/**
 * Unzip the file into the target directory.
 * The added structure is updated to reflect any new files and directories created.
 * Overwrite files when requested./*from  ww  w.  j a  va2  s.co m*/
 * @param zipOmni
 * @param targetDir
 * @param added
 * @param httpIpPort
 * @return
 */
public static boolean unzipFile(OmniFile zipOmni, OmniFile targetDir, JsonArray added, String httpIpPort) {

    String volumeId = zipOmni.getVolumeId();
    String rootFolderPath = targetDir.getPath();
    boolean DEBUG = true;

    /**
     * Keep a list of all directories created.
     * Defer creating the directory object files until the zip is extracted.
     * This way the dir=1/0 settings can be set accurately.
     */
    ArrayList<OmniFile> directories = new ArrayList<>();

    ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream());

    ZipEntry entry = null;
    try {
        while ((entry = zis.getNextEntry()) != null) {

            if (entry.isDirectory()) {

                OmniFile dir = new OmniFile(volumeId, entry.getName());
                if (dir.mkdir()) {

                    directories.add(dir);
                    if (DEBUG)
                        LogUtil.log(LogUtil.LogType.OMNI_ZIP, "dir created: " + dir.getPath());
                }
            } else {

                String path = rootFolderPath + "/" + entry.getName();
                OmniFile file = new OmniFile(volumeId, path);
                // Create any necessary directories
                file.getParentFile().mkdirs();

                if (file.exists()) {
                    file = OmniUtil.makeUniqueName(file);
                }
                OutputStream out = file.getOutputStream();

                OmniFiles.copyFileLeaveInOpen(zis, out);
                if (DEBUG)
                    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "file created: " + file.getPath());

                if (added != null)
                    added.add(file.getFileObject(httpIpPort));
            }
        }
        zis.close();

        if (added != null) {

            /**
             * Iterate over the list of directories created and
             * create object files for each.
             * The full tree is now expanded such that the dir=1/0
             * can be set accurately.
             */
            for (OmniFile dir : directories)
                added.add(dir.getFileObject(httpIpPort));
        }

    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
        return false;
    }

    return true;
}