Example usage for java.util.zip ZipInputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for reading the next entry.

Usage

From source file:org.openecomp.sdc.ZipUtil.java

private static Map<String, byte[]> readZip(byte[] zipAsBytes) {

    Map<String, byte[]> fileNameToByteArray = new HashMap<String, byte[]>();

    byte[] buffer = new byte[1024];
    ZipInputStream zis = null;
    try {//from w  w  w  . java  2 s  .  c  om

        zis = new ZipInputStream(new ByteArrayInputStream(zipAsBytes));
        // get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();

            if (false == ze.isDirectory()) {

                ByteArrayOutputStream os = new ByteArrayOutputStream();
                try {
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        os.write(buffer, 0, len);
                    }

                    // aClass.outputStreamMethod(os);
                    String aString = new String(os.toByteArray(), "UTF-8");

                    fileNameToByteArray.put(fileName, os.toByteArray());

                } finally {
                    if (os != null) {
                        os.close();
                    }
                }
            }
            ze = zis.getNextEntry();

        }

        zis.closeEntry();
        zis.close();

    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    } finally {
        if (zis != null) {
            try {
                zis.closeEntry();
                zis.close();
            } catch (IOException e) {
                // TODO: add log
            }

        }
    }

    return fileNameToByteArray;

}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java

public static String getShapefileNameFromCompressed(InputStream is) {
    String shapefileName = null;/*from w ww  . j a  va 2s .  c o  m*/
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry ze;
    try {
        while (((ze = zis.getNextEntry()) != null) && (shapefileName == null)) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName().toLowerCase();
                if (fileName.endsWith(".shp")) {
                    shapefileName = fileName;
                }
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            zis.closeEntry();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            zis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return shapefileName;
}

From source file:org.bonitasoft.engine.io.IOUtil.java

private static void extractZipEntries(final ZipInputStream zipInputstream, final File outputFolder)
        throws IOException {
    ZipEntry zipEntry;//from   w  w w. java 2  s .c  o m
    while ((zipEntry = zipInputstream.getNextEntry()) != null) {
        try {
            // For each entry, a file is created in the output directory "folder"
            final File outputFile = new File(outputFolder.getAbsolutePath(), zipEntry.getName());
            // If the entry is a directory, it creates in the output folder, and we go to the next entry (continue).
            if (zipEntry.isDirectory()) {
                mkdirs(outputFile);
                continue;
            }
            writeZipInputToFile(zipInputstream, outputFile);
        } finally {
            zipInputstream.closeEntry();
        }
    }
}

From source file:org.jenkinsci.plugins.fortifycloudscan.util.ArchiveUtil.java

public static void unzip(File directory, File zipFile) throws FileNotFoundException, IOException {
    if (!directory.exists()) {
        directory.mkdirs();//  w w w  . j  a v a  2  s .c o  m
    }
    byte[] buffer = new byte[2048];

    FileInputStream fInput = null;
    ZipInputStream zipInput = null;
    try {
        fInput = new FileInputStream(zipFile);
        zipInput = new ZipInputStream(fInput);
        ZipEntry entry = zipInput.getNextEntry();
        while (entry != null) {
            String entryName = entry.getName();
            File file = new File(directory.getAbsolutePath() + File.separator + entryName);
            if (entry.isDirectory()) {
                File newDir = new File(file.getAbsolutePath());
                if (!newDir.exists()) {
                    newDir.mkdirs();
                }
            } else {
                if (!file.getParentFile().isDirectory() && !file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                FileOutputStream fOutput = new FileOutputStream(file);
                int count;
                while ((count = zipInput.read(buffer)) > 0) {
                    fOutput.write(buffer, 0, count);
                }
                fOutput.close();
            }
            zipInput.closeEntry();
            entry = zipInput.getNextEntry();
        }
        zipInput.closeEntry();
        zipInput.close();
        fInput.close();
    } catch (FileNotFoundException e) {
        throw new FileNotFoundException(e.getMessage());
    } catch (IOException e) {
        throw new IOException(e);
    } finally {
        try {
            zipInput.closeEntry();
        } catch (IOException e) {
        }
        IOUtils.closeQuietly(zipInput);
        IOUtils.closeQuietly(fInput);
    }
}

From source file:org.roda_project.commons_ip.utils.ZIPUtils.java

public static void unzip(Path zip, final Path dest) throws IOException {
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zip.toFile()));
    ZipEntry zipEntry = zipInputStream.getNextEntry();

    if (zipEntry == null) {
        // No entries in ZIP
        zipInputStream.close();//from   w  w w.ja  v a  2 s. c  om
    } else {
        while (zipEntry != null) {
            // for each entry to be extracted
            String entryName = zipEntry.getName();
            Path newFile = dest.resolve(entryName);

            if (zipEntry.isDirectory()) {
                Files.createDirectories(newFile);
            } else {
                if (!Files.exists(newFile.getParent())) {
                    Files.createDirectories(newFile.getParent());
                }

                OutputStream newFileOutputStream = Files.newOutputStream(newFile);
                IOUtils.copyLarge(zipInputStream, newFileOutputStream);

                newFileOutputStream.close();
                zipInputStream.closeEntry();
            }

            zipEntry = zipInputStream.getNextEntry();
        } // end while

        zipInputStream.close();
    }
}

From source file:org.openremote.controller.utils.ZipUtil.java

/**
 * Unzip a zip.// w ww  .j  a v a 2s .  c  o  m
 * 
 * @param inputStream the input stream
 * @param targetDir the target dir
 * 
 * @return true, if success
 */
public static boolean unzip(InputStream inputStream, String targetDir) {
    if (targetDir == null || "".equals(targetDir)) {
        throw new ExtractZipFileException("The resources path is null.");
    }
    File checkedTargetDir = new File(targetDir);
    if (!checkedTargetDir.exists()) {
        throw new ExtractZipFileException("The path " + targetDir + " doesn't exist.");
    }

    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    ZipEntry zipEntry;
    FileOutputStream fileOutputStream = null;
    File zippedFile = null;
    try {
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            if (!zipEntry.isDirectory()) {
                targetDir = targetDir.endsWith("/") || targetDir.endsWith("\\") ? targetDir : targetDir + "/";
                zippedFile = new File(targetDir, zipEntry.getName());
                FileUtils.deleteQuietly(zippedFile);
                FileUtils.touch(zippedFile);
                fileOutputStream = new FileOutputStream(zippedFile);
                int b;
                while ((b = zipInputStream.read()) != -1) {
                    fileOutputStream.write(b);
                }
                fileOutputStream.close();
            }
        }
    } catch (IOException e) {
        logger.error("Can't unzip file to " + zippedFile.getPath(), e);
        return false;
    } finally {
        try {
            zipInputStream.closeEntry();
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            logger.error("Error while closing stream.", e);
        }

    }
    return true;
}

From source file:de.spiritcroc.ownlog.FileHelper.java

public static ImportFiles unpackImport(Context context, InputStream inputStream) {
    File importingDir = new File(context.getCacheDir(), context.getString(R.string.import_file_dir));
    rmdir(importingDir);/* w w  w  . ja v a  2 s  .c om*/
    importingDir.mkdirs();
    ZipInputStream in = new ZipInputStream(inputStream);
    ZipEntry entry;
    try {
        while ((entry = in.getNextEntry()) != null) {
            String path = importingDir.getAbsolutePath() + File.separator + entry.getName();
            if (DEBUG)
                Log.d(TAG, "Unzipping path: " + path);
            File f = new File(path);
            if (entry.isDirectory()) {
                if (!f.isDirectory()) {
                    f.mkdirs();
                }
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
                FileOutputStream out = new FileOutputStream(path, false);
                try {
                    int size;
                    while ((size = in.read()) != -1) {
                        out.write(size);
                    }
                    in.closeEntry();
                } finally {
                    try {
                        out.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.w(TAG, "Import failed", e);
        return null;
    }
    File logDbFile = new File(importingDir, context.getString(R.string.import_file_db));
    File attachmentsDir = new File(importingDir, context.getString(R.string.import_file_attachments));
    if (logDbFile.exists()) {
        return new ImportFiles(logDbFile, attachmentsDir);
    } else {
        Log.w(TAG, "Import failed: database not found");
        return null;
    }
}

From source file:wordnice.utils.JavaUtils.java

public static void getClassesZip(Collection<String> set, ZipInputStream zip) throws Exception {
    ZipEntry ent = null;//ww  w  .  ja  v a 2  s.c o m
    while ((ent = zip.getNextEntry()) != null) {
        if (!ent.isDirectory()) {
            String clsn = ent.getName();
            if (clsn.endsWith(".class") && clsn.indexOf('$') == -1) {
                set.add(clsn.substring(0, clsn.length() - 6).replace(File.separatorChar, '.'));
            }
        }
        zip.closeEntry();
    }
    zip.close();
}

From source file:Main.java

public static boolean unzipFile(InputStream fis, String destDir) {
    final byte[] buffer = new byte[4096];
    ZipInputStream zis = null;
    Log.e("Unzip", "destDir = " + destDir);
    try {//from   w  w w .j a v  a 2s .  c om
        // make sure the directory is existent
        File dstFile = new File(destDir);
        if (!dstFile.exists()) {
            dstFile.mkdirs();
        } else {
            int fileLenght = dstFile.listFiles().length;
            if (fileLenght >= 2) {
                return true;
            }
        }
        zis = new ZipInputStream(fis);
        ZipEntry entry;

        while ((entry = zis.getNextEntry()) != null) {
            String fileName = entry.getName();

            if (entry.isDirectory()) {
                new File(destDir, fileName).mkdirs();

            } else {
                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream(new File(destDir, fileName)));
                int lenRead;

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

                bos.close();
            }

            zis.closeEntry();
        }

        return true;

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

    } finally {
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:org.dita2indesign.cmdline.Docx2Xml.java

/**
* @param zipInFile/*from w w w  . jav a  2s  .  c  om*/
*            Zip file to be unzipped
* @param outputDir
*            Directory to which the zipped files will be unpacked.
* @throws Exception 
* @throws ZipException 
*/
public static void unzip(File zipInFile, File outputDir) throws Exception {

    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile = new ZipFile(zipInFile);
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipInFile));
    ZipEntry entry = (ZipEntry) zipInputStream.getNextEntry();

    File curOutDir = outputDir;
    while (entry != null) {

        if (entry.isDirectory()) {
            // This is not robust, just for demonstration purposes.
            curOutDir = new File(curOutDir, entry.getName());
            curOutDir.mkdirs();
            continue;
        }

        File outFile = new File(curOutDir, entry.getName());
        File tempDir = outFile.getParentFile();
        if (!tempDir.exists())
            tempDir.mkdirs();
        outFile.createNewFile();
        BufferedOutputStream outstream = new BufferedOutputStream(new FileOutputStream(outFile));

        int n;
        byte[] buf = new byte[1024];
        while ((n = zipInputStream.read(buf, 0, 1024)) > -1)
            outstream.write(buf, 0, n);
        outstream.flush();
        outstream.close();
        zipInputStream.closeEntry();
        entry = zipInputStream.getNextEntry();
    }
    zipInputStream.close();

    zipFile.close();
}