Example usage for java.util.zip ZipFile getEntry

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

Introduction

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

Prototype

public ZipEntry getEntry(String name) 

Source Link

Document

Returns the zip file entry for the specified name, or null if not found.

Usage

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

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

    try {// ww  w .j  a v  a 2 s. c  o  m
        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(".nuxeo-archive");

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

From source file:PolyGlot.IOHandler.java

/**
 * Gets the dictionary File from the filename of the pgd, whether it's raw
 * XML or an archive//w w w  .ja va 2 s  .com
 *
 * @param _filename the filename of the actual file
 * @return the File object of the dictionary
 * @throws java.io.IOException
 */
public static InputStream getDictFile(String _filename) throws IOException {
    InputStream rawFile = new FileInputStream(_filename);

    if (isFileZipArchive(_filename)) {
        ZipFile zipFile = new ZipFile(_filename);

        ZipEntry xmlEntry = zipFile.getEntry(PGTUtil.dictFileName);

        return zipFile.getInputStream(xmlEntry);
    }

    return rawFile;
}

From source file:PolyGlot.IOHandler.java

/**
 * Gets font from save file if possible, null otherwise
 *
 * @param _path The path of the PGD file
 * @return a Font object if the PGD file is both a zip archive and contains
 * a font// w  ww .ja  v a 2s.  c o  m
 * @throws java.io.IOException
 * @throws java.awt.FontFormatException
 */
public static Font getFontFrom(String _path) throws IOException, FontFormatException {
    Font ret = null;

    if (isFileZipArchive(_path)) {
        ZipFile zipFile = new ZipFile(_path);

        ZipEntry fontEntry = zipFile.getEntry(PGTUtil.fontFileName);

        if (fontEntry != null) {
            final File tempFile = File.createTempFile("stream2file", ".tmp");
            tempFile.deleteOnExit();

            FileOutputStream out = new FileOutputStream(tempFile);
            IOUtils.copy(zipFile.getInputStream(fontEntry), out);

            try {
                ret = Font.createFont(Font.TRUETYPE_FONT, tempFile);
            } catch (FontFormatException e) {
                throw new FontFormatException(
                        "Could not load language font. Possible incompatible font: " + e.getMessage());
            } catch (IOException e) {
                throw new FontFormatException("Could not load language font. I/O exception: " + e.getMessage());
            }
        }
    }

    return ret;
}

From source file:org.apache.oozie.tools.OozieDBImportCLI.java

private static void checkDBVersion(EntityManager entityManager, ZipFile zipFile) throws IOException {
    try {//from   www  . ja va2  s.com
        String currentDBVersion = (String) entityManager
                .createNativeQuery("select data from OOZIE_SYS where name='db.version'").getSingleResult();
        String dumpDBVersion = null;
        ZipEntry entry = zipFile.getEntry(OOZIEDB_SYS_INFO_JSON);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(zipFile.getInputStream(entry), "UTF-8"));
        String line;
        Gson gson = new Gson();
        while ((line = reader.readLine()) != null) {
            List<String> info = gson.fromJson(line, List.class);
            if (info.size() > 1 && "db.version".equals(info.get(0))) {
                dumpDBVersion = info.get(1);
            }
        }
        reader.close();
        if (currentDBVersion.equals(dumpDBVersion)) {
            System.out.println("Loading to Oozie database version " + currentDBVersion);
        } else {
            System.err.println("ERROR Oozie database version mismatch.");
            System.err.println("Oozie DB version:\t" + currentDBVersion);
            System.err.println("Dump DB version:\t" + dumpDBVersion);
            System.exit(1);
        }
    } catch (Exception e) {
        System.err.println();
        System.err.println("Error during DB version check: " + e.getMessage());
        System.err.println();
        System.err.println("Stack trace for the error was (for debug purposes):");
        System.err.println("--------------------------------------");
        e.printStackTrace(System.err);
        System.err.println("--------------------------------------");
        System.err.println();
    }
}

From source file:org.nuxeo.common.utils.ZipUtils.java

/**
 * Unzip directly the entry. The returned InputStream has to be closed.
 *
 * @return the input stream of the desired entry - has to be closed by the caller, or null if not found
 * @param file the source file// ww w.j  av a 2 s  .c  om
 * @param entryName the entry name that has to be extracted
 */
public static InputStream getEntryContentAsStream(File file, String entryName) throws IOException {
    InputStream result = null;
    ZipFile zip = new ZipFile(file);
    ZipEntry entry = zip.getEntry(entryName);
    if (entry != null) {
        result = zip.getInputStream(entry);
    }
    return result;
}

From source file:nl.strohalm.cyclos.themes.ThemeHandlerImpl.java

/**
 * Return the properties for the given zip file
 *//*from  w  w w  .  java 2  s . co  m*/
private static Properties properties(final ZipFile zipFile) throws IOException {
    final ZipEntry propertiesEntry = zipFile.getEntry(THEME_PROPERTIES_ENTRY);
    if (propertiesEntry == null) {
        throw new FileNotFoundException(THEME_PROPERTIES_ENTRY);
    }
    final Properties properties = new Properties();
    properties.load(zipFile.getInputStream(propertiesEntry));
    return properties;
}

From source file:org.envirocar.app.util.Util.java

/**
 * method to get the current version//from   ww w.  ja va2s .c om
 * 
 */
public static String getVersionString(Context ctx) {
    StringBuilder out = new StringBuilder("Version ");
    try {
        out.append(getVersionStringShort(ctx));
        out.append(" (");
        out.append(ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0).versionCode);
        out.append("), ");
    } catch (NameNotFoundException e) {
        logger.warn(e.getMessage(), e);
    }
    try {
        ApplicationInfo ai = ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(), 0);
        ZipFile zf = new ZipFile(ai.sourceDir);
        ZipEntry ze = zf.getEntry("classes.dex");
        long time = ze.getTime();
        out.append(SimpleDateFormat.getInstance().format(new java.util.Date(time)));
        zf.close();
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
    }

    return out.toString();
}

From source file:PolyGlot.IOHandler.java

/**
 * loads all images into their logographs from archive
 * @param logoCollection logocollection from dictionary core
 * @param fileName name/path of archive//from   w w  w.j  a  v  a  2s  . co m
 * @throws java.lang.Exception
 */
public static void loadImages(LogoCollection logoCollection, String fileName) throws Exception {
    if (!isFileZipArchive(fileName)) {
        return;
    }

    Iterator<LogoNode> it = logoCollection.getAllLogos().iterator();
    ZipFile zipFile = new ZipFile(fileName);

    while (it.hasNext()) {
        LogoNode curNode = it.next();
        ZipEntry imgEntry = zipFile.getEntry(PGTUtil.logoGraphSavePath + curNode.getId().toString() + ".png");

        BufferedImage img = ImageIO.read(zipFile.getInputStream(imgEntry));
        curNode.setLogoGraph(img);
    }
}

From source file:com.shazam.fork.TestClassScanner.java

private static void getDexClassesFromApk(File apkFile, File outputFolder) {
    ZipFile zip = null;
    InputStream classesDexInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {//w  w w  .j a  va  2  s. c om
        zip = new ZipFile(apkFile);

        int index = 1;
        String currentDex;
        while (true) {
            currentDex = CLASSES_PREFIX + (index > 1 ? index : "") + DEX_EXTENSION;
            ZipEntry classesDex = zip.getEntry(currentDex);
            if (classesDex != null) {
                File dexFileDestination = new File(outputFolder, currentDex);
                classesDexInputStream = zip.getInputStream(classesDex);
                fileOutputStream = new FileOutputStream(dexFileDestination);
                copyLarge(classesDexInputStream, fileOutputStream);
                index++;
            } else {
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeQuietly(classesDexInputStream);
        closeQuietly(fileOutputStream);
        closeZipQuietly(zip);
    }
}

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  w w . java  2 s  .  c  o m*/
        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;
    }
}