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

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


    public void testFileEntryFromFile() 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(tmp[1], "foo");
            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


    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

    public void testJarMarker() throws IOException {
        File testArchive = File.createTempFile("jar-aostest", ".jar");
        testArchive.deleteOnExit();
        JarArchiveOutputStream out = null;
        ZipFile zf = null;
        try {

            out = new JarArchiveOutputStream(new FileOutputStream(testArchive));
            out.putArchiveEntry(new ZipArchiveEntry("foo/"));
            out.closeArchiveEntry();
            out.putArchiveEntry(new ZipArchiveEntry("bar/"));
            out.closeArchiveEntry();
            out.finish();
            out.close();
            out = null;

            zf = new ZipFile(testArchive);
            ZipArchiveEntry ze = zf.getEntry("foo/");
            assertNotNull(ze);
            ZipExtraField[] fes = ze.getExtraFields();
            assertEquals(1, fes.length);
            assertTrue(fes[0] instanceof JarMarker);

            ze = zf.getEntry("bar/");
            assertNotNull(ze);
            fes = ze.getExtraFields();
            assertEquals(0, fes.length);
        } finally {
            if (out != null) {
View Full Code Here

     * Test case for
     * <a href="https://issues.apache.org/jira/browse/COMPRESS-93"
     * >COMPRESS-93</a>.
     */
    public void testSupportedCompressionMethod() throws IOException {
        ZipFile bla = new ZipFile(getFile("bla.zip"));
        assertTrue(bla.canReadEntryData(bla.getEntry("test1.xml")));
        bla.close();

        ZipFile moby = new ZipFile(getFile("moby.zip"));
        assertFalse(moby.canReadEntryData(moby.getEntry("README")));
        moby.close();
    }
View Full Code Here

    List<String> fileNames = CollectUtils.newArrayList();
    String dest = destination;
    if (!destination.endsWith(File.separator)) {
      dest = destination + File.separator;
    }
    ZipFile file;
    try {
      file = null;
      if (null == encoding) file = new ZipFile(zipFile);
      else file = new ZipFile(zipFile, encoding);
      @SuppressWarnings("unchecked")
      Enumeration<ZipArchiveEntry> en = file.getEntries();
      ZipArchiveEntry ze = null;
      while (en.hasMoreElements()) {
        ze = en.nextElement();
        File f = new File(dest, ze.getName());
        if (ze.isDirectory()) {
          f.mkdirs();
          continue;
        } else {
          f.getParentFile().mkdirs();
          InputStream is = file.getInputStream(ze);
          OutputStream os = new FileOutputStream(f);
          IOUtils.copy(is, os);
          is.close();
          os.close();
          fileNames.add(f.getAbsolutePath());
        }
      }
      file.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return fileNames;
  }
View Full Code Here

    return fileNames;
  }

  public static boolean isZipFile(File zipFile) {
    try {
      ZipFile zf = new ZipFile(zipFile);
      boolean isZip = zf.getEntries().hasMoreElements();
      zf.close();
      return isZip;
    } catch (IOException e) {
      return false;
    }
  }
View Full Code Here

        String extension = FilenameUtils.getExtension(file);
        if(loadEntriesWithinZipArchives && (
                "zip".equalsIgnoreCase(extension) ||
                "jar".equalsIgnoreCase(extension))){
            log.info("  - processing {}-archive entries:",extension);
            ZipFile zipArchive;
            try {
                zipArchive = new ZipFile(file);
            } catch (IOException e) {
                zipArchive = null;
                setResourceState(file, ResourceState.ERROR,e);
            }
            if(zipArchive != null){
                boolean isError = false;
                Enumeration<ZipArchiveEntry> entries = zipArchive.getEntries();
                while(entries.hasMoreElements()){
                    ZipArchiveEntry entry = entries.nextElement();
                    if(!entry.isDirectory()){
                        String entryName = entry.getName();
                        log.info("     o loading entry '{}'", entryName);
                        try {
                            ResourceState state = resourceImporter.importResource(
                                zipArchive.getInputStream(entry),
                                FilenameUtils.getName(entryName));
                            if(state == ResourceState.ERROR){
                                isError = true;
                            }
                        } catch (IOException e) {
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

        super(ArchiveFormat.ZIP);
    }

    @Override
    protected ArchiveInputStream createArchiveInputStream(File archive) throws IOException {
        return new ZipFileArchiveInputStream(new ZipFile(archive));
    }
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.