Package org.apache.commons.compress.archivers.zip

Examples of org.apache.commons.compress.archivers.zip.ZipFile


    public void testExplicitFileEntry() throws Exception {
        File[] tmp = createTempDirAndFile();
        File archive = null;
        ZipArchiveOutputStream zos = null;
        ZipFile zf = null;
        FileInputStream fis = null;
        try {
            archive = File.createTempFile("test.", ".zip", tmp[0]);
            archive.deleteOnExit();
            zos = new ZipArchiveOutputStream(archive);
            ZipArchiveEntry in = new ZipArchiveEntry("foo");
            in.setTime(tmp[1].lastModified());
            in.setSize(tmp[1].length());
            zos.putArchiveEntry(in);
            byte[] b = new byte[(int) tmp[1].length()];
            fis = new FileInputStream(tmp[1]);
            while (fis.read(b) > 0) {
                zos.write(b);
            }
            fis.close();
            fis = null;
            zos.closeArchiveEntry();
            zos.close();
            zos = null;
            zf = new ZipFile(archive);
            ZipArchiveEntry out = zf.getEntry("foo");
            assertNotNull(out);
            assertEquals("foo", out.getName());
            assertEquals(tmp[1].length(), out.getSize());
            assertEquals(tmp[1].lastModified() / 2000,
                         out.getLastModifiedDate().getTime() / 2000);
View Full Code Here


        // We can only detect the exact type when given a TikaInputStream
        TikaInputStream tis = TikaInputStream.cast(input);
        if (tis != null) {
            try {
                ZipFile zip = new ZipFile(tis.getFile());
                try {
                    MediaType type = detectOpenDocument(zip);
                    if (type == null) {
                        type = detectOfficeOpenXML(zip, tis);
                    }
                    if (type == null) {
                        type = detectIWork(zip);
                    }
                    if (type != null) {
                        return type;
                    } else if (zip.getEntry("META-INF/MANIFEST.MF") != null) {
                        return MediaType.application("java-archive");
                    }
                } finally {
                    // TODO: shouldn't we record the open
                    // container so it can be later
                    // reused...?
                    // tis.setOpenContainer(zip);
                    zip.close();
                }
            } catch (IOException ignore) {
            }
        }
View Full Code Here

        }
        File sourceRoot = getSource(clazzInArchive != null ? clazzInArchive : ConfigUtils.class);
        log.info("Init Solr Managed Directory form {} to {} (override={})",
            new Object[]{sourceRoot,rootDir,override});
        if (sourceRoot.isFile()) {
            ZipFile archive = new ZipFile(sourceRoot);
            log.info("  - read from jar-file");
            try {
                for (@SuppressWarnings("unchecked")
                Enumeration<ZipArchiveEntry> entries = (Enumeration<ZipArchiveEntry>) archive.getEntries(); entries
                        .hasMoreElements();) {
                    ZipArchiveEntry entry = entries.nextElement();
                    if (!entry.isDirectory() && entry.getName().startsWith(CONFIG_DIR)) {
                        copyResource(rootDir, archive, entry, CONFIG_DIR, override);
                    }
View Full Code Here

                    "The parsed core name MUST NOT be empty (However NULL is supported)!");
        }
        String context = CORE_CONFIG_DIR + (coreName != null ? '/' + coreName : "");
        File sourceRoot = getSource(clazzInArchive != null ? clazzInArchive : ConfigUtils.class);
        if (sourceRoot.isFile()) {
            ZipFile archive = new ZipFile(sourceRoot);
            log.info(String.format("Copy core %s config from jar-file %s to %s (override=%s)",
                (coreName == null ? "" : coreName), sourceRoot.getName(), coreDir.getAbsolutePath(), override));
            try {
                for (@SuppressWarnings("unchecked")
                Enumeration<ZipArchiveEntry> entries = (Enumeration<ZipArchiveEntry>) archive.getEntries(); entries
                        .hasMoreElements();) {
                    ZipArchiveEntry entry = entries.nextElement();
                    if (entry.getName().startsWith(context)) {
                        copyResource(coreDir, archive, entry, context, override);
                    }
View Full Code Here

     * @param dependency the dependency to check
     * @return true if the dependency appears to be a JAR file; otherwise false
     */
    private boolean isZipFileActuallyJarFile(Dependency dependency) {
        boolean isJar = false;
        ZipFile zip = null;
        try {
            zip = new ZipFile(dependency.getActualFilePath());
            if (zip.getEntry("META-INF/MANIFEST.MF") != null
                    || zip.getEntry("META-INF/maven") != null) {
                final Enumeration<ZipArchiveEntry> entries = zip.getEntries();
                while (entries.hasMoreElements()) {
                    final ZipArchiveEntry entry = entries.nextElement();
                    if (!entry.isDirectory()) {
                        final String name = entry.getName().toLowerCase();
                        if (name.endsWith(".class")) {
View Full Code Here

        // We can only detect the exact type when given a TikaInputStream
        TikaInputStream tis = TikaInputStream.cast(input);
        if (tis != null) {
            try {
                ZipFile zip = new ZipFile(tis.getFile());
                try {
                    MediaType type = detectOpenDocument(zip);
                    if (type == null) {
                        type = detectOfficeOpenXML(zip, tis);
                    }
                    if (type == null) {
                        type = detectIWork(zip);
                    }
                    if (type != null) {
                        return type;
                    } else if (zip.getEntry("META-INF/MANIFEST.MF") != null) {
                        return MediaType.application("java-archive");
                    }
                } finally {
                    // TODO: shouldn't we record the open
                    // container so it can be later
                    // reused...?
                    // tis.setOpenContainer(zip);
                    zip.close();
                }
            } catch (IOException ignore) {
            }
        }
View Full Code Here

            return MediaType.APPLICATION_ZIP;
        }

        try {
            File file = TikaInputStream.get(input).getFile();
            ZipFile zip = new ZipFile(file);

            MediaType type = detectOpenDocument(zip);
            if (type == null) {
                type = detectOfficeOpenXML(zip, TikaInputStream.get(input));
            }
            if (type == null) {
                type = detectIWork(zip);
            }
            if (type == null && zip.getEntry("META-INF/MANIFEST.MF") != null) {
                type = MediaType.application("java-archive");
            }
            if (type == null) {
                type = MediaType.APPLICATION_ZIP;
            }
View Full Code Here

    }

  protected Map<String, String> readZipArchive(InputStream inputStream) throws IOException {
    Map<String, String> data = new HashMap<String, String>();
    File tempFile = writeTemporaryArchiveFile(inputStream, "zip");
        ZipFile zip = new ZipFile(tempFile);
        Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        while (entries.hasMoreElements()) {
          ZipArchiveEntry entry = entries.nextElement();
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          IOUtils.copy(zip.getInputStream(entry), bos);
          data.put(entry.getName(), DigestUtils.md5Hex(bos.toByteArray()));
        }

        zip.close();
        tempFile.delete();
    return data;
  }
View Full Code Here

    return data;
  }

  protected String readArchiveText(InputStream inputStream) throws IOException {
      File tempFile = writeTemporaryArchiveFile(inputStream, "zip");
      ZipFile zip = new ZipFile(tempFile);
      zip.getEntry(UnpackerResource.TEXT_FILENAME);
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
        IOUtils.copy(zip.getInputStream(zip.getEntry(UnpackerResource.TEXT_FILENAME)), bos);

        zip.close();
        tempFile.delete();
    return bos.toString("UTF-8");
  }
View Full Code Here

        }
    }

    private static MediaType detectZipFormat(TikaInputStream tis) {
        try {
            ZipFile zip = new ZipFile(tis.getFile()); // TODO: hasFile()?
            try {
                MediaType type = detectOpenDocument(zip);
                if (type == null) {
                    type = detectOPCBased(zip, tis);
                }
                if (type == null) {
                    type = detectIWork(zip);
                }
                if (type == null) {
                    type = detectJar(zip);
                }
                if (type == null) {
                    type = detectKmz(zip);
                }
                if (type == null) {
                    type = detectIpa(zip);
                }
                if (type != null) {
                    return type;
                }
            } finally {
                // TODO: shouldn't we record the open
                // container so it can be later
                // reused...?
                // tis.setOpenContainer(zip);
                try {
                    zip.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        } catch (IOException e) {
View Full Code Here

TOP

Related Classes of org.apache.commons.compress.archivers.zip.ZipFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.