Example usage for java.util.zip ZipFile close

List of usage examples for java.util.zip ZipFile close

Introduction

In this page you can find the example usage for java.util.zip ZipFile close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP file.

Usage

From source file:org.dbgl.util.FileUtils.java

public static String[] getExecutablesInZipOrIsoOrFat(final String archive) throws IOException {
    List<String> result = new ArrayList<String>();
    File arcFile = new File(archive);

    if (archive.toLowerCase().endsWith(ARCHIVES[0])) { // zip
        ZipFile zfile = new ZipFile(arcFile);
        for (Enumeration<? extends ZipEntry> entries = zfile.entries(); entries.hasMoreElements();) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();
            if (!entry.isDirectory() && isExecutable(name)) {
                result.add(PlatformUtils.archiveToNativePath(name));
            }/*from w ww  . j  a  v a 2  s .c om*/
        }
        zfile.close();
    } else if (archive.toLowerCase().endsWith(ARCHIVES[1])) { // 7-zip
        MyRandomAccessFile istream = new MyRandomAccessFile(archive, "r");
        IInArchive zArchive = new Handler();
        if (zArchive.Open(istream) != 0) {
            throw new IOException(
                    Settings.getInstance().msg("general.error.opensevenzip", new Object[] { archive }));
        }
        for (int i = 0; i < zArchive.size(); i++) {
            SevenZipEntry entry = zArchive.getEntry(i);
            String name = entry.getName();
            if (!entry.isDirectory() && isExecutable(name)) {
                result.add(PlatformUtils.archiveToNativePath(name));
            }
        }
        zArchive.close();
    } else if (isIsoFile(archive)) {
        ISO9660FileSystem iso = new ISO9660FileSystem(new File(archive));
        for (Enumeration<ISO9660FileEntry> entries = iso.getEntries(); entries.hasMoreElements();) {
            ISO9660FileEntry entry = entries.nextElement();
            String name = entry.getPath();
            if (!entry.isDirectory() && isExecutable(name)) {
                result.add(PlatformUtils.archiveToNativePath(name));
            }
        }
        iso.close();
    } else if (isFatImage(archive)) {
        BlockDevice device = new FileDisk(new File(archive));
        result.addAll(readFatEntries(new FatFileSystem(device).getRoot(), ""));
        device.close();
    }

    Collections.sort(result, new FilenameComparator());
    return result.toArray(new String[result.size()]);
}

From source file:com.koushikdutta.superuser.MainActivity.java

File extractSu() throws IOException, InterruptedException {
    ZipFile zf = new ZipFile(getPackageCodePath());
    ZipEntry su = zf.getEntry("assets/" + getArch() + "/su");
    InputStream zin = zf.getInputStream(su);
    File ret = getFileStreamPath("su");
    FileOutputStream fout = new FileOutputStream(ret);
    StreamUtility.copyStream(zin, fout);
    zin.close();/*from  ww  w.  j av a 2  s . c  o m*/
    zf.close();
    fout.close();
    return ret;
}

From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java

/**
 * Extracts the packageFile and writes its content in temporary directory
 *
 * @param packageFile the packageFile (zip format)
 * @param tempDir the tempDir directory//from w w w. j  a  v a2 s.co  m
 */
public static void extractPackage(File packageFile, File tempDir) throws SoundPackageException {

    if (packageFile == null) {
        throw new SoundPackageException(new IllegalArgumentException("missing package file"));
    }

    ZipFile packageZip = null;
    try {
        packageZip = new ZipFile(packageFile);
        Enumeration<?> entries = packageZip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String entryName = entry.getName();
            if (log.isDebugEnabled()) {
                log.debug("ZipEntry name: " + entryName);
            }
            if (entry.isDirectory()) {
                File dir = new File(tempDir, entryName);
                if (dir.mkdirs())
                    log.info("successfully created dir: " + dir.getAbsolutePath());
            } else {
                FileUtil.createFileFromInputStream(getPackageEntryStream(packageFile, entryName),
                        tempDir + File.separator + entryName);
            }
        }
    } catch (FileNotFoundException ex) {
        throw new SoundPackageException(ex);
    } catch (IOException ex) {
        throw new SoundPackageException(ex);
    } finally {
        try {
            if (packageZip != null)
                packageZip.close();
        } catch (IOException ex) {
            log.error(ex.getMessage(), ex);
        }
    }
}

From source file:org.polago.deployconf.DeployConfRunner.java

/**
 * Gets a DeploymentConfig instance from a Zip file.
 *
 * @param source the ZipFile to use/* w  ww . j  a  v a2 s.  com*/
 * @return a DeploymentConfig representation of the ReadableByteChannel
 * @throws Exception indicating error
 */
private DeploymentConfig getDeploymentConfigFromZip(String source) throws Exception {

    ZipFile zipFile = new ZipFile(source);
    ZipEntry entry = zipFile.getEntry(deploymentTemplatePath);
    if (entry == null) {
        zipFile.close();
        throw new IllegalArgumentException(
                "No deployment template file found in file '" + source + "': " + deploymentTemplatePath);
    }
    InputStream is = zipFile.getInputStream(entry);

    DeploymentReader reader = new DeploymentReader(is, groupManager);
    DeploymentConfig result = reader.parse();
    is.close();
    zipFile.close();

    return result;
}

From source file:cpw.mods.fml.server.FMLServerHandler.java

private void searchZipForENUSLanguage(File source) throws IOException {
    ZipFile zf = new ZipFile(source);
    for (ZipEntry ze : Collections.list(zf.entries())) {
        Matcher matcher = assetENUSLang.matcher(ze.getName());
        if (matcher.matches()) {
            FMLLog.fine("Injecting found translation data in zip file %s at %s into language system",
                    source.getName(), ze.getName());
            StringTranslate.inject(zf.getInputStream(ze));
        }/*from  w  w w .  ja  va  2 s  .  c o  m*/
    }
    zf.close();
}

From source file:org.cloudifysource.shell.commands.TestRecipe.java

/**
 * Unzips a given file.//from  ww  w.ja v  a2 s  .  c o m
 *
 * @param inputFile
 *            The zip file to extract
 * @return The new folder, containing the extracted content of the zip file
 * @throws IOException
 *             Reporting a failure to extract the zipped file or close it afterwards
 */
private static File unzipFile(final File inputFile) throws IOException {

    ZipFile zipFile = null;
    try {
        final File baseDir = TestRecipe.createTempDir();
        zipFile = new ZipFile(inputFile);
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();

            if (entry.isDirectory()) {

                logger.fine("Extracting directory: " + entry.getName());
                final File dir = new File(baseDir, entry.getName());
                dir.mkdir();
                continue;
            }

            logger.finer("Extracting file: " + entry.getName());
            final File file = new File(baseDir, entry.getName());
            file.getParentFile().mkdirs();
            ServiceReader.copyInputStream(zipFile.getInputStream(entry),
                    new BufferedOutputStream(new FileOutputStream(file)));
        }
        return baseDir;

    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (final IOException e) {
                logger.log(Level.SEVERE, "Failed to close zip file after unzipping zip contents", e);
            }
        }
    }

}

From source file:com.koushikdutta.superuser.MainActivity.java

void doRecoveryInstall() {
    final ProgressDialog dlg = new ProgressDialog(this);
    dlg.setTitle(R.string.installing);//from   w w  w .j  a v a 2  s . c  om
    dlg.setMessage(getString(R.string.installing_superuser));
    dlg.setIndeterminate(true);
    dlg.show();
    new Thread() {
        void doEntry(ZipOutputStream zout, String entryName, String dest) throws IOException {
            ZipFile zf = new ZipFile(getPackageCodePath());
            ZipEntry ze = zf.getEntry(entryName);
            zout.putNextEntry(new ZipEntry(dest));
            InputStream in;
            StreamUtility.copyStream(in = zf.getInputStream(ze), zout);
            zout.closeEntry();
            in.close();
            zf.close();
        }

        public void run() {
            try {
                File zip = getFileStreamPath("superuser.zip");
                ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zip));
                doEntry(zout, "assets/update-binary", "META-INF/com/google/android/update-binary");
                doEntry(zout, "assets/install-recovery.sh", "install-recovery.sh");
                zout.close();

                ZipFile zf = new ZipFile(getPackageCodePath());
                ZipEntry ze = zf.getEntry("assets/" + getArch() + "/reboot");
                InputStream in;
                FileOutputStream reboot;
                StreamUtility.copyStream(in = zf.getInputStream(ze),
                        reboot = openFileOutput("reboot", MODE_PRIVATE));
                reboot.close();
                in.close();

                final File su = extractSu();

                String command = String.format("cat %s > /cache/superuser.zip\n", zip.getAbsolutePath())
                        + String.format("cat %s > /cache/su\n", su.getAbsolutePath())
                        + String.format("cat %s > /cache/Superuser.apk\n", getPackageCodePath())
                        + "mkdir /cache/recovery\n"
                        + "echo '--update_package=CACHE:superuser.zip' > /cache/recovery/command\n"
                        + "chmod 644 /cache/superuser.zip\n" + "chmod 644 /cache/recovery/command\n" + "sync\n"
                        + String.format("chmod 755 %s\n", getFileStreamPath("reboot").getAbsolutePath())
                        + "reboot recovery\n";
                Process p = Runtime.getRuntime().exec("su");
                p.getOutputStream().write(command.getBytes());
                p.getOutputStream().close();
                File rebootScript = getFileStreamPath("reboot.sh");
                StreamUtility.writeFile(rebootScript,
                        "reboot recovery ; " + getFileStreamPath("reboot").getAbsolutePath() + " recovery ;");
                p.waitFor();
                Runtime.getRuntime().exec(new String[] { "su", "-c", ". " + rebootScript.getAbsolutePath() });
                if (p.waitFor() != 0)
                    throw new Exception("non zero result");
            } catch (Exception ex) {
                ex.printStackTrace();
                dlg.dismiss();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setPositiveButton(android.R.string.ok, null);
                        builder.setTitle(R.string.install);
                        builder.setMessage(R.string.install_error);
                        builder.create().show();
                    }
                });
            }
        }
    }.start();
}

From source file:com.fluidops.iwb.luxid.LuxidExtractor.java

/**
 * extracts a zip-file and returns references to the unziped files. If the file passed to this method
 * is not a zip-file, a reference to the file is returned.
 * /* w  ww. ja v a2 s . c o  m*/
 * @param fileName
 * @return
 * @throws Exception
 */
public static Set<File> extractZip(String fileName) throws Exception {
    File zipf = new File((new StringBuilder("luxid/")).append(fileName).toString());

    Set<File> toBeUploaded = new HashSet<File>();
    if (zipf.getName().endsWith(".zip")) {
        ZipFile zip = new ZipFile(zipf);
        for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            if (entry.isDirectory()) {
                logger.info((new StringBuilder("Extracting directory: ")).append(entry.getName()).toString());
                GenUtil.mkdir(new File(entry.getName()));
            } else {
                logger.info((new StringBuilder("Extracting file: ")).append(entry.getName()).toString());
                String entryPath = "luxid/" + entry.getName();
                FileOutputStream fileOutputStream = null;
                InputStream zipEntryStream = zip.getInputStream(entry);
                try {
                    fileOutputStream = new FileOutputStream(entryPath);
                    IOUtils.copy(zipEntryStream, fileOutputStream);
                } finally {
                    closeQuietly(zipEntryStream);
                    closeQuietly(fileOutputStream);
                }
                toBeUploaded.add(new File(entryPath));
            }
        }

        zip.close();
    } else {
        toBeUploaded.add(zipf);
    }

    return toBeUploaded;
}

From source file:org.sonar.fortify.fvdl.FortifyReportFile.java

private InputStream getInputStreamFromFprFile(File file) throws IOException {
    final ZipFile fprFile = new ZipFile(file);
    try {/*from   ww w  .ja  v  a 2  s . co  m*/
        final InputStream reportStream = fprFile
                .getInputStream(fprFile.getEntry(FortifyConstants.AUDIT_FVDL_FILE));
        return new InputStream() {
            @Override
            public int read() throws IOException {
                return reportStream.read();
            }

            @Override
            public void close() throws IOException {
                try {
                    reportStream.close();
                } finally {
                    fprFile.close();
                }
            }
        };
    } catch (IOException e) {
        fprFile.close();
        throw e;
    }
}

From source file:com.dibsyhex.apkdissector.ZipReader.java

public void getZipEntries(String name) {
    try {//from w  w  w.j ava 2s  . c o m
        File file = new File(name);
        ZipFile zipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> enumeration = zipFile.entries();

        System.out.println("Listing Entries in the apkfile");

        response.displayLog("Listing Entries in the apkfile");

        while (enumeration.hasMoreElements()) {
            Object key = enumeration.nextElement();
            //String s=key.toString()+":"+zipFile.getEntry(key.toString());
            String s = zipFile.getEntry(key.toString()).toString();
            System.out.println("  " + s);

            response.displayLog(s);
        }

        zipFile.close();
    } catch (Exception e) {
        System.out.println(e.toString());
        response.displayError(e.toString());
    }
}