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:com.skwas.cordova.appinfo.AppInfo.java

public static long getBuildDate(Context context) {

    ZipFile zf = null;
    try {//from   w w  w  .ja v a 2 s  .c  o  m
        ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
        zf = new ZipFile(ai.sourceDir);
        ZipEntry ze = zf.getEntry("classes.dex");
        long time = ze.getTime();
        return time;

    } catch (Exception e) {
    }

    finally {
        try {
            if (zf != null)
                zf.close();
        } catch (IOException e) {
        }
    }

    return 0l;
}

From source file:gov.nih.nci.caintegrator.common.Cai2Util.java

/**
 * Validate a ZIP file.// www  .  j ava 2 s .  c  o m
 * @param file the zip file to be validated.
 * @return boolean if valid or not.
 */
public static boolean isValidZipFile(final File file) {
    ZipFile zipfile = null;
    try {
        zipfile = new ZipFile(file);
        ZipFile zipIn2 = new ZipFile(file);
        zipIn2.close();
        LOGGER.info("zipFile validated = " + file.getAbsolutePath());
        return true;
    } catch (ZipException e) {
        return false;
    } catch (IOException e) {
        return false;
    } catch (Exception e) {
        return false;
    } finally {
        try {
            if (zipfile != null) {
                zipfile.close();
                zipfile = null;
            }
        } catch (Exception e) {
            LOGGER.info("Exception in isValidZipFile = " + e);
        }
    }
}

From source file:net.fabricmc.installer.installer.MultiMCInstaller.java

public static void install(File mcDir, String version, IInstallerProgress progress) throws Exception {
    File instancesDir = new File(mcDir, "instances");
    if (!instancesDir.exists()) {
        throw new FileNotFoundException(Translator.getString("install.multimc.notFound"));
    }/* w  w w .  j a  v  a2  s . co m*/
    progress.updateProgress(Translator.getString("install.multimc.findInstances"), 10);
    String mcVer = version.split("-")[0];
    List<File> validInstances = new ArrayList<>();
    for (File instanceDir : instancesDir.listFiles()) {
        if (instanceDir.isDirectory()) {
            if (isValidInstance(instanceDir, mcVer)) {
                validInstances.add(instanceDir);
            }
        }
    }
    if (validInstances.isEmpty()) {
        throw new Exception(Translator.getString("install.multimc.noInstances").replace("[MCVER]", mcVer));
    }
    List<String> instanceNames = new ArrayList<>();
    for (File instance : validInstances) {
        instanceNames.add(instance.getName());
    }
    String instanceName = (String) JOptionPane.showInputDialog(null,
            Translator.getString("install.multimc.selectInstance"),
            Translator.getString("install.multimc.selectInstance"), JOptionPane.QUESTION_MESSAGE, null,
            instanceNames.toArray(), instanceNames.get(0));
    if (instanceName == null) {
        progress.updateProgress(Translator.getString("install.multimc.canceled"), 100);
        return;
    }
    progress.updateProgress(
            Translator.getString("install.multimc.installingInto").replace("[NAME]", instanceName), 25);
    File instnaceDir = null;
    for (File instance : validInstances) {
        if (instance.getName().equals(instanceName)) {
            instnaceDir = instance;
        }
    }
    if (instnaceDir == null) {
        throw new FileNotFoundException("Could not find " + instanceName);
    }
    File patchesDir = new File(instnaceDir, "patches");
    if (!patchesDir.exists()) {
        patchesDir.mkdir();
    }
    File fabricJar = new File(patchesDir, "Fabric-" + version + ".jar");
    if (!fabricJar.exists()) {
        progress.updateProgress(Translator.getString("install.client.downloadFabric"), 30);
        FileUtils.copyURLToFile(new URL("http://maven.modmuss50.me/net/fabricmc/fabric-base/" + version
                + "/fabric-base-" + version + ".jar"), fabricJar);
    }
    progress.updateProgress(Translator.getString("install.multimc.createJson"), 70);
    File fabricJson = new File(patchesDir, "fabric.json");
    if (fabricJson.exists()) {
        fabricJson.delete();
    }
    String json = readBaseJson();
    json = json.replaceAll("%VERSION%", version);

    ZipFile fabricZip = new ZipFile(fabricJar);
    ZipEntry dependenciesEntry = fabricZip.getEntry("dependencies.json");
    String fabricDeps = IOUtils.toString(fabricZip.getInputStream(dependenciesEntry), Charset.defaultCharset());
    json = json.replace("%DEPS%", stripDepsJson(fabricDeps.replace("\n", "")));
    FileUtils.writeStringToFile(fabricJson, json, Charset.defaultCharset());
    fabricZip.close();
    progress.updateProgress(Translator.getString("install.success"), 100);
}

From source file:org.silverpeas.file.ZipUtil.java

/**
 * ---------------------------------------------------------------------
 * @param from// w  ww.j ava  2  s .  co m
 * @param to
 * @throws IOException
 * @throws IllegalArgumentException
 * @see
 */
public static void unzip(final File fromZipFile, final File toDir) throws IOException {
    if (!toDir.exists()) {
        toDir.mkdirs();
    }
    final ZipFile zipFile = new ZipFile(fromZipFile);
    ZipInputStream zipInput = null;
    try {
        zipInput = new ZipInputStream(new FileInputStream(fromZipFile));
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            extractFile(zipFile, entry, toDir);
            zipInput.closeEntry();
        }
    } finally {
        if (zipInput != null) {
            IOUtils.closeQuietly(zipInput);
        }
        zipFile.close();
    }
}

From source file:com.netflix.nicobar.core.utils.__JDKPaths.java

static void processJar(final Set<String> pathSet, final File file) throws IOException {
    final ZipFile zipFile = new ZipFile(file);
    try {/*  w  w  w. ja v a 2  s.co m*/
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final String name = entry.getName();
            final int lastSlash = name.lastIndexOf('/');
            if (lastSlash != -1) {
                pathSet.add(name.substring(0, lastSlash));
            }
        }
        zipFile.close();
    } finally {
        IOUtils.closeQuietly(zipFile);
    }
}

From source file:org.nuxeo.ecm.platform.filemanager.service.extension.CSVZipImporter.java

public static ZipFile getArchiveFileIfValid(File file) throws IOException {
    ZipFile zip;

    try {/*from  w ww  .j  ava2  s  .  c om*/
        zip = new ZipFile(file);
    } catch (ZipException e) {
        log.debug("file is not a zipfile ! ", e);
        return null;
    } catch (IOException e) {
        log.debug("can not open zipfile ! ", e);
        return null;
    }

    ZipEntry marker = zip.getEntry(MARKER);

    if (marker == null) {
        zip.close();
        return null;
    } else {
        return zip;
    }
}

From source file:com.oneis.app.FileController.java

public static ZipFileExtractionResults zipFileExtraction(String zipFilePathname, String contentsDefault,
        String contentsRequested) throws IOException {
    ZipFileExtractionResults results = new ZipFileExtractionResults();

    ZipFile zipFile = new ZipFile(zipFilePathname);
    try {/*from  w  w  w . j a v a2  s . c  o  m*/
        // Find the right entry, trying for the requested filename by defaulting back on the default given
        ZipEntry e = zipFile.getEntry(contentsRequested);
        results.chosenFile = contentsRequested;
        if (e == null) {
            e = zipFile.getEntry(contentsDefault);
            results.chosenFile = contentsDefault;
        }
        if (e == null) {
            return null;
        }

        results.data = IOUtils.toByteArray(zipFile.getInputStream(e));
    } finally {
        zipFile.close();
    }

    results.etagSuggestion = results.chosenFile.hashCode() & 0xfffff;

    return results;
}

From source file:com.netflix.nicobar.core.utils.ClassPathUtils.java

/**
 * Get all of the directory paths in a zip/jar file
 * @param pathToJarFile location of the jarfile. can also be a zipfile
 * @return set of directory paths relative to the root of the jar
 *//* ww  w  .  ja v  a 2 s  .c o  m*/
public static Set<Path> getDirectoriesFromJar(Path pathToJarFile) throws IOException {
    Set<Path> result = new HashSet<Path>();
    ZipFile jarfile = new ZipFile(pathToJarFile.toFile());
    try {
        final Enumeration<? extends ZipEntry> entries = jarfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                result.add(Paths.get(entry.getName()));
            }
        }
        jarfile.close();
    } finally {
        IOUtils.closeQuietly(jarfile);
    }
    return result;
}

From source file:Main.java

public static String[] getXmlFiles(String path) {
    List<String> xmlFiles = new ArrayList<>();
    ZipFile zipFile = null;
    try {//from w  w  w  . j a v a2  s . c o m
        zipFile = new ZipFile(path);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();
            if (name.endsWith(".xml") && !name.equals("AndroidManifest.xml")) {
                xmlFiles.add(name);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ignored) {
            }
        }
    }
    Collections.sort(xmlFiles);
    return xmlFiles.toArray(new String[xmlFiles.size()]);
}

From source file:org.digidoc4j.utils.Helper.java

/**
 * Get the signature from a file.//  w w w  .java 2s .  co  m
 *
 * @param file  file containing the container
 * @param index index of the signature file
 * @return signature
 * @throws IOException when the signature is not found
 */
public static String extractSignature(String file, int index) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    String signatureFileName = "META-INF/signatures" + index + ".xml";
    ZipEntry entry = zipFile.getEntry(signatureFileName);

    if (entry == null)
        throw new IOException(signatureFileName + " does not exists in archive: " + file);

    InputStream inputStream = zipFile.getInputStream(entry);
    String signatureContent = IOUtils.toString(inputStream, "UTF-8");

    zipFile.close();
    inputStream.close();

    return signatureContent;
}