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.apache.stratos.python.cartridge.agent.integration.tests.PythonAgentIntegrationTest.java

private void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();/*from   ww w . j  a v  a 2 s . c om*/
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + 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:org.walkmod.gradle.providers.ClassLoaderConfigurationProvider.java

private void unzipAAR(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();//from w  w w .j  av  a2s . c om
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            if (entry.getName().endsWith(".jar")) {
                // if the entry is a file, extracts it
                extractFile(zipIn, filePath);
            }
        } else {
            if (entry.getName().endsWith("jars/") || entry.getName().endsWith("libs/")) {
                File dir = new File(filePath);
                dir.mkdir();
            }
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:org.wso2.carbon.connector.FileUnzipConnector.java

/**
 * Decompress the compressed file into the given directory.
 *
 * @param messageContext The message context that is generated for processing unzip operation.
 * @return true, if zip file successfully extracts and false, if not.
 * @throws FileSystemException On error parsing the file name, determining if the file exists and creating the
 * folder./*from w w w.  j a v  a  2s .c  o  m*/
 */
private boolean unzip(MessageContext messageContext) throws FileSystemException {
    String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.NEW_FILE_LOCATION);
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    FileSystemOptions opts = FileConnectorUtils.init(messageContext);
    FileObject remoteFile = manager.resolveFile(source, opts);
    FileObject remoteDesFile = manager.resolveFile(destination, opts);

    if (!remoteFile.exists()) {
        log.error("File does not exist.");
        return false;
    }
    if (!remoteDesFile.exists()) {
        //create a folder
        remoteDesFile.createFolder();
    }
    //open the zip file
    ZipInputStream zipIn = new ZipInputStream(remoteFile.getContent().getInputStream());
    try {
        ZipEntry entry = zipIn.getNextEntry();

        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destination + File.separator + entry.getName();
            // create remote object
            FileObject remoteFilePath = manager.resolveFile(filePath, opts);
            if (log.isDebugEnabled()) {
                log.debug("The created path is " + remoteFilePath.toString());
            }
            try {
                if (!entry.isDirectory()) {
                    // if the entry is a file, extracts it
                    extractFile(zipIn, remoteFilePath);
                } else {
                    // if the entry is a directory, make the directory
                    remoteFilePath.createFolder();
                }
            } finally {
                try {
                    zipIn.closeEntry();
                    entry = zipIn.getNextEntry();
                } catch (IOException e) {
                    log.error("Error while closing the ZipInputStream", e);
                }
            }
        }
    } catch (IOException e) {
        throw new SynapseException("Error while reading the next ZIP file entry", e);
    } finally {
        // close the zip file
        try {
            zipIn.close();
        } catch (IOException e) {
            log.error("Error while closing the ZipInputStream", e);
        }
        // close the StandardFileSystemManager
        manager.close();
        try {
            remoteFile.close();
            remoteDesFile.close();
        } catch (FileSystemException e) {
            log.error("Error while closing the FileObject", e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("File extracted to" + destination);
    }
    return true;
}

From source file:de.static_interface.sinklibrary.Updater.java

/**
 * Part of Zip-File-Extractor, modified by Gravity for use with Bukkit
 *///from  ww  w. j  a  v a 2s  .  co m
private void unzip(File zipFile) {
    byte[] buffer = new byte[1024];

    try {
        File outputFolder = zipFile.getParentFile();
        if (!outputFolder.exists()) {
            outputFolder.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);
            if (!newFile.exists() && newFile.getName().toLowerCase().endsWith(".jar")) {
                ze = zis.getNextEntry();
                continue;
            }

            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();

        zipFile.delete();

    } catch (IOException ex) {
        SinkLibrary.getInstance().getLogger()
                .warning("The auto-updater tried to unzip a new update file, but was unsuccessful.");
        result = Updater.UpdateResult.FAIL_DOWNLOAD;
        ex.printStackTrace();
    }
}

From source file:net.sourceforge.vulcan.core.support.AbstractFileStore.java

@Override
public final PluginMetaDataDto extractPlugin(InputStream is) throws StoreException {
    final File pluginsDir = getPluginsRoot();
    String toplevel = null;//from w  ww.jav a 2s.c om
    File tmpDir = null;

    if (!pluginsDir.exists()) {
        createDir(pluginsDir);
    }

    final ZipInputStream zis = new ZipInputStream(is);
    ZipEntry entry;

    try {
        entry = zis.getNextEntry();
        if (entry == null || !entry.isDirectory()) {
            throw new InvalidPluginLayoutException();
        }
        toplevel = entry.getName();
        tmpDir = new File(pluginsDir, "tmp-" + toplevel);
        if (!tmpDir.exists()) {
            createDir(tmpDir);
        }

        final String id = toplevel.replaceAll("/", "");

        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.getName().startsWith(toplevel)) {
                throw new InvalidPluginLayoutException();
            }

            final File out = new File(pluginsDir, "tmp-" + entry.getName());

            if (entry.isDirectory()) {
                createDir(out);
                zis.closeEntry();
            } else {
                final FileOutputStream os = new FileOutputStream(out);

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

        checkPluginVersion(pluginsDir, id);

        return createPluginConfig(new File(pluginsDir, toplevel));
    } catch (DuplicatePluginIdException e) {
        throw e;
    } catch (Exception e) {
        if (e instanceof StoreException) {
            throw (StoreException) e;
        }
        throw new StoreException(e.getMessage(), e);
    } finally {
        try {
            zis.close();
        } catch (IOException ignore) {
        }
        if (tmpDir != null && tmpDir.exists()) {
            try {
                FileUtils.deleteDirectory(tmpDir);
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:org.freebxml.omar.common.Utility.java

/**
 *
 * Extracts Zip file contents relative to baseDir
 * @return An ArrayList containing the File instances for each unzipped file
 *///from ww  w.  ja  v  a2s  .com
public static ArrayList unZip(String baseDir, InputStream is) throws IOException {
    ArrayList files = new ArrayList();
    ZipInputStream zis = new ZipInputStream(is);

    while (true) {
        // Get the next zip entry.  Break out of the loop if there are
        //   no more.
        ZipEntry zipEntry = zis.getNextEntry();
        if (zipEntry == null)
            break;

        String entryName = zipEntry.getName();
        if (FILE_SEPARATOR.equalsIgnoreCase("\\")) {
            // Convert '/' to Windows file separator
            entryName = entryName.replaceAll("/", "\\\\");
        }
        String fileName = baseDir + FILE_SEPARATOR + entryName;
        //Make sure that directory exists.
        String dirName = fileName.substring(0, fileName.lastIndexOf(FILE_SEPARATOR));
        File dir = new File(dirName);
        dir.mkdirs();

        //Entry could be a directory
        if (!(zipEntry.isDirectory())) {
            //Entry is a file not a directory.
            //Write out the content of of entry to file 
            File file = new File(fileName);
            files.add(file);
            FileOutputStream fos = new FileOutputStream(file);

            // Read data from the zip entry.  The read() method will return
            //   -1 when there is no more data to read.
            byte[] buffer = new byte[1000];

            int n;

            while ((n = zis.read(buffer)) > -1) {
                // In real life, you'd probably write the data to a file.
                fos.write(buffer, 0, n);
            }
            zis.closeEntry();
            fos.close();
        } else {
            zis.closeEntry();
        }
    }

    zis.close();

    return files;
}

From source file:org.opennms.upgrade.api.AbstractOnmsUpgrade.java

/**
 * UNZIP a file./* w ww  .j av  a2  s  . c  om*/
 *
 * @param zipFile the input ZIP file
 * @param outputFolder the output folder
 * @throws OnmsUpgradeException the OpenNMS upgrade exception
 */
protected void unzipFile(File zipFile, File outputFolder) throws OnmsUpgradeException {
    try {
        if (!outputFolder.exists()) {
            if (!outputFolder.mkdirs()) {
                LOG.warn("Could not make directory: {}", outputFolder.getPath());
            }
        }
        FileInputStream fis;
        byte[] buffer = new byte[1024];
        fis = new FileInputStream(zipFile);
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder, fileName);
            log("  Unzipping to %s\n", newFile.getAbsolutePath());
            new File(newFile.getParent()).mkdirs();
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            zis.closeEntry();
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
        fis.close();
    } catch (Exception e) {
        throw new OnmsUpgradeException("Cannot UNZIP file because " + e.getMessage(), e);
    }
}

From source file:pt.webdetails.cpk.utils.ZipUtil.java

public void unzip(File zipFile, File destinationFolder) {
    byte[] buffer = new byte[1024];
    setFileInputStream(zipFile);/*w  w w.  ja  v a 2s  .  c  o m*/
    ZipInputStream zis = new ZipInputStream(fis);
    try {

        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            String filename = entry.getName();
            File newFile = null;

            if (entry.isDirectory()) {
                newFile = new File(
                        destinationFolder.getAbsolutePath() + File.separator + filename + File.separator);
                newFile.mkdirs();
                newFile.mkdir();
            } else {
                newFile = new File(destinationFolder.getAbsolutePath() + File.separator + filename);
                newFile.createNewFile();
                FileOutputStream fos = new FileOutputStream(newFile);
                int len = 0;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
            }

            entry = zis.getNextEntry();
        }

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

    } catch (IOException ex) {
        Logger.getLogger(ZipUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.tinymediamanager.core.tvshow.tasks.TvShowSubtitleDownloadTask.java

@Override
protected void doInBackground() {
    // let the DownloadTask handle the whole download
    super.doInBackground();

    MediaFile mf = new MediaFile(file);

    if (mf.getType() != MediaFileType.SUBTITLE) {
        String basename = FilenameUtils.getBaseName(file.getFileName().toString());

        // try to decompress
        try {/*from   w w w  . java2  s  . c om*/
            byte[] buffer = new byte[1024];

            ZipInputStream is = new ZipInputStream(new FileInputStream(file.toFile()));

            // get the zipped file list entry
            ZipEntry ze = is.getNextEntry();

            while (ze != null) {
                String zipEntryFilename = ze.getName();
                String extension = FilenameUtils.getExtension(zipEntryFilename);

                // check is that is a valid file type
                if (!Globals.settings.getSubtitleFileType().contains("." + extension)) {
                    ze = is.getNextEntry();
                    continue;
                }

                File destination = new File(file.getParent().toFile(), basename + "." + extension);
                FileOutputStream os = new FileOutputStream(destination);

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

                os.close();
                mf = new MediaFile(destination.toPath());

                // only take the first subtitle
                break;
            }
            is.closeEntry();
            is.close();

            Utils.deleteFileSafely(file);
        } catch (Exception e) {
        }
    }

    mf.gatherMediaInformation();
    episode.removeFromMediaFiles(mf); // remove old (possibly same) file
    episode.addToMediaFiles(mf); // add file, but maybe with other MI values
    episode.saveToDb();
}

From source file:link.kjr.file_manager.MainActivity.java

public void decompressZipFile(String path) throws IOException {
    FileInputStream fis = new FileInputStream(path);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze = zis.getNextEntry();
    while (ze != null) {

        File f = new File(currentPath + "/" + ze.getName());

        File parentfile = f.getParentFile();
        parentfile.mkdirs();/*from  w  w  w .j a  v  a2  s.co m*/

        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(currentPath + "/" + ze.getName());
        byte[] data = new byte[1024];
        int length = 0;
        length = zis.read(data, 0, 1024);
        fos.write(data, 0, length);
        fos.close();
        zis.closeEntry();
        ze = zis.getNextEntry();
    }
    zis.close();
    fis.close();
}