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.sakaiproject.search.util.FileUtils.java

/**
 * unpack a segment from a zip//w w w .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;
    try {
        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());
            if (log.isDebugEnabled())
                log.debug("         Unpack " + f.getAbsolutePath());
            if (!f.getParentFile().exists()) {
                if (!f.getParentFile().mkdirs()) {
                    log.warn("unpack(): Failed to create parent folder: " + f.getParentFile().getPath());
                }
            }

            fout = new FileOutputStream(f);
            int len;
            while ((len = zin.read(buffer)) > 0) {
                fout.write(buffer, 0, len);
            }
            zin.closeEntry();
            fout.close();
            if (!f.setLastModified(ts)) {
                log.warn("upack(): failes to set modified date on " + f.getPath());
            }
        }
    } finally {
        try {
            fout.close();
        } catch (Exception ex) {
            log.warn("Exception closing file output stream", ex);
        }
    }
}

From source file:com.oeg.oops.VocabUtils.java

/**
 * Code to unzip a file. Inspired from/*from   www.  j a  v  a 2s.c  o m*/
 * http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/
 * Taken from
 * @param resourceName
 * @param outputFolder
 */
public static void unZipIt(String resourceName, String outputFolder) {

    byte[] buffer = new byte[1024];

    try {
        ZipInputStream zis = new ZipInputStream(VocabUtils.class.getResourceAsStream(resourceName));
        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()) {
                String temp = newFile.getAbsolutePath();
                new File(temp).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();

    } catch (IOException ex) {
        System.err.println("Error while extracting the reosurces: " + ex.getMessage());
    }
}

From source file:com.jaromin.alfresco.repo.content.transform.ZipFormatContentTransformer.java

/**
 * /*from  www  .  j  a v  a2s. c  o m*/
 * @param path
 * @param zip
 * @param out
 * @throws IOException
 */
protected void extractEntry(String path, ZipInputStream zip, OutputStream out) throws IOException {

    // Use as regex for more flexibility...
    Pattern p = Pattern.compile(path);

    ZipEntry entry = null;
    while ((entry = zip.getNextEntry()) != null) {
        Matcher m = p.matcher(entry.getName());
        if (m.matches()) {
            IOUtils.copy(zip, out);
            zip.closeEntry();
            break;
        }
    }
}

From source file:com.frostwire.util.ZipUtils.java

private static void unzipEntries(File folder, ZipInputStream zis, int itemCount, long time,
        ZipListener listener) throws IOException, FileNotFoundException {
    ZipEntry ze = null;/*from w  w  w  .j a v a2s .com*/

    int item = 0;

    while ((ze = zis.getNextEntry()) != null) {
        item++;

        String fileName = ze.getName();
        File newFile = new File(folder, fileName);

        LOG.debug("unzip: " + newFile.getAbsoluteFile());

        if (ze.isDirectory()) {
            if (!newFile.mkdirs()) {
                break;
            }
            continue;
        }

        if (listener != null) {
            int progress = (item == itemCount) ? 100 : (int) (((double) (item * 100)) / (double) (itemCount));
            listener.onUnzipping(fileName, progress);
        }

        FileOutputStream fos = new FileOutputStream(newFile);

        try {
            int n;
            byte[] buffer = new byte[1024];
            while ((n = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, n);

                if (listener != null && listener.isCanceled()) { // not the best way
                    throw new IOException("Uncompress operation cancelled");
                }
            }
        } finally {
            fos.close();
            zis.closeEntry();
        }

        newFile.setLastModified(time);
    }
}

From source file:org.olat.core.util.ZipUtil.java

/**
 * //from   www.ja  v  a2s  . c  o m
 * @param zipLeaf
 * @param targetDir
 * @param identity
 * @param roles
 * @return
 */
public static List<String> checkLockedFileBeforeUnzipNonStrict(VFSLeaf zipLeaf, VFSContainer targetDir,
        Identity identity, Roles roles) {
    List<String> lockedFiles = new ArrayList<String>();

    InputStream in = zipLeaf.getInputStream();
    net.sf.jazzlib.ZipInputStream oZip = new net.sf.jazzlib.ZipInputStream(in);

    VFSLockManager vfsLockManager = CoreSpringFactory.getImpl(VFSLockManager.class);

    try {
        // unzip files
        net.sf.jazzlib.ZipEntry oEntr = oZip.getNextEntry();
        while (oEntr != null) {
            if (oEntr.getName() != null && !oEntr.getName().startsWith(DIR_NAME__MACOSX)) {
                if (oEntr.isDirectory()) {
                    // skip MacOSX specific metadata directory
                    // directories aren't locked
                    oZip.closeEntry();
                    oEntr = oZip.getNextEntry();
                    continue;
                } else {
                    // search file
                    VFSContainer createIn = targetDir;
                    String name = oEntr.getName();
                    // check if entry has directories which did not show up as
                    // directories above
                    int dirSepIndex = name.lastIndexOf('/');
                    if (dirSepIndex == -1) {
                        // try it windows style, backslash is also valid format
                        dirSepIndex = name.lastIndexOf('\\');
                    }
                    if (dirSepIndex > 0) {
                        // get subdirs
                        createIn = getAllSubdirs(targetDir, name.substring(0, dirSepIndex), identity, false);
                        if (createIn == null) {
                            //sub directories don't exist, and aren't locked
                            oZip.closeEntry();
                            oEntr = oZip.getNextEntry();
                            continue;
                        }
                        name = name.substring(dirSepIndex + 1);
                    }

                    VFSLeaf newEntry = (VFSLeaf) createIn.resolve(name);
                    if (vfsLockManager.isLockedForMe(newEntry, identity, roles)) {
                        lockedFiles.add(name);
                    }
                }
            }
            oZip.closeEntry();
            oEntr = oZip.getNextEntry();
        }
    } catch (IOException e) {
        return null;
    } finally {
        FileUtils.closeSafely(oZip);
        FileUtils.closeSafely(in);
    }

    return lockedFiles;
}

From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java

/**
 * unzips a zip-stream into a target dir.
 * /*from www  .  ja v a 2s.  com*/
 * @param overwriteExisting
 *          if false, existing files will not be overwritten. Folders are always created.
 */
public static void unzip(final InputStream inputStream, final File targetdir, final boolean overwriteExisting)
        throws IOException {
    final ZipInputStream zis = new ZipInputStream(inputStream);
    while (true) {
        final ZipEntry entry = zis.getNextEntry();
        if (entry == null)
            break;

        final File newfile = new File(targetdir, entry.getName());
        if (entry.isDirectory())
            newfile.mkdirs();
        else {
            if (!newfile.getParentFile().exists())
                newfile.getParentFile().mkdirs();

            OutputStream os = null;
            try {
                if (!overwriteExisting && newfile.exists())
                    os = new NullOutputStream();
                else
                    os = new BufferedOutputStream(new FileOutputStream(newfile));

                IOUtils.copy(zis, os);
            } finally {
                IOUtils.closeQuietly(os);
                zis.closeEntry();
            }
        }
    }
}

From source file:com.ecofactor.qa.automation.platform.util.FileUtil.java

/**
 * Un zip a file to a destination folder.
 * @param zipFile the zip file//from   w ww  .j  a  v  a2 s  .  c  om
 * @param outputFolder the output folder
 */
public static void unZipFile(final String zipFile, final String outputFolder) {

    LogUtil.setLogString("UnZip the File", true);
    final byte[] buffer = new byte[1024];
    int len;
    try {
        // create output directory is not exists
        final File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }
        // get the zip file content
        final ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        final StringBuilder outFolderPath = new StringBuilder();
        final StringBuilder fileLogPath = new StringBuilder();
        ZipEntry zipEntry;
        while ((zipEntry = zis.getNextEntry()) != null) {
            final String fileName = zipEntry.getName();
            final File newFile = new File(
                    outFolderPath.append(outputFolder).append(File.separator).append(fileName).toString());
            LogUtil.setLogString(
                    fileLogPath.append("file unzip : ").append(newFile.getAbsoluteFile()).toString(), true);
            // create all non exists folders
            // else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();
            final FileOutputStream fos = new FileOutputStream(newFile);

            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            fileLogPath.setLength(0);
            outFolderPath.setLength(0);
        }

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

        LogUtil.setLogString("Done", true);

    } catch (IOException ex) {
        LOGGER.error("Error in unzip file", ex);
    }
}

From source file:com.thinkbiganalytics.feedmgr.util.ImportUtil.java

public static Set<ImportComponentOption> inspectZipComponents(InputStream inputStream, ImportType importType)
        throws IOException {
    Set<ImportComponentOption> options = new HashSet<>();
    ZipInputStream zis = new ZipInputStream(inputStream);
    ZipEntry entry;//  ww w  . jav  a2s. c o  m
    while ((entry = zis.getNextEntry()) != null) {
        if (entry.getName().startsWith(ExportImportTemplateService.NIFI_TEMPLATE_XML_FILE)) {
            options.add(new ImportComponentOption(ImportComponent.NIFI_TEMPLATE,
                    importType.equals(ImportType.TEMPLATE) ? true : false));
        } else if (entry.getName().startsWith(ExportImportTemplateService.TEMPLATE_JSON_FILE)) {
            options.add(new ImportComponentOption(ImportComponent.TEMPLATE_DATA,
                    importType.equals(ImportType.TEMPLATE) ? true : false));
        } else if (entry.getName()
                .startsWith(ExportImportTemplateService.NIFI_CONNECTING_REUSABLE_TEMPLATE_XML_FILE)) {
            options.add(new ImportComponentOption(ImportComponent.REUSABLE_TEMPLATE, false));
        } else if (importType.equals(ImportType.FEED)
                && entry.getName().startsWith(ExportImportFeedService.FEED_JSON_FILE)) {
            options.add(new ImportComponentOption(ImportComponent.FEED_DATA, true));
            options.add(new ImportComponentOption(ImportComponent.USER_DATASOURCES, true));
        }
    }
    zis.closeEntry();
    zis.close();

    return options;
}

From source file:io.sledge.core.impl.extractor.SledgeApplicationPackageExtractor.java

@Override
public List<String> getEnvironmentNames(ApplicationPackage appPackage) {
    List<String> envFiles = new ArrayList<>();
    ZipInputStream zipStream = getNewUtf8ZipInputStream(appPackage);

    try {//from w  ww. jav a2 s .co m
        ZipEntry zipEntry = null;

        while ((zipEntry = zipStream.getNextEntry()) != null) {

            if (zipEntry.isDirectory()) {
                zipStream.closeEntry();
                continue;
            }

            if (zipEntry.getName().startsWith("environments/")) {
                // test-publish.properties -> test-publish
                envFiles.add(getBaseName(zipEntry.getName()));
                zipStream.closeEntry();
            }
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            zipStream.close();
            appPackage.getPackageFile().reset();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    return envFiles;
}

From source file:net.sf.jasperreports.samples.wizards.SampleNewWizard.java

public static void unpackArchive(File theFile, File targetDir, IProgressMonitor monitor, Set<String> cpaths,
        Set<String> lpaths) {
    byte[] buffer = new byte[1024];
    ZipInputStream zis = null;
    try {//  w w w.j  av a  2  s.co  m
        zis = new ZipInputStream(new FileInputStream(theFile));

        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            File file = new File(targetDir, File.separator + ze.getName());

            new File(file.getParent()).mkdirs();
            String fname = file.getName();
            if (ze.isDirectory()) {
                if (fname.equals("src"))
                    cpaths.add(ze.getName());
            } else {
                FileOutputStream fos = new FileOutputStream(file);
                int len;
                while ((len = zis.read(buffer)) > 0)
                    fos.write(buffer, 0, len);

                fos.close();
            }
            if (file.getParentFile().getName().equals("lib")
                    && (fname.endsWith(".jar") || fname.endsWith(".zip")))
                lpaths.add(ze.getName());
            if (monitor.isCanceled()) {
                zis.close();
                return;
            }
            ze = zis.getNextEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            zis.closeEntry();
            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}