Example usage for org.apache.commons.compress.archivers.zip ZipFile ZipFile

List of usage examples for org.apache.commons.compress.archivers.zip ZipFile ZipFile

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipFile ZipFile.

Prototype

public ZipFile(String name) throws IOException 

Source Link

Document

Opens the given file for reading, assuming "UTF8".

Usage

From source file:org.slc.sli.ingestion.landingzone.ZipFileUtil.java

/**
 * Get the entries in the ZIP file./*from   ww w  .  j  ava 2 s  .co m*/
 *
 * @param zipFileName  ZIP file name
 * @return the entries in the ZIP file
 */
public static Set<String> getZipFileEntries(String zipFileName) throws IOException {
    Enumeration<ZipArchiveEntry> zipFileEntries = null;
    Set<String> filesInZip = new HashSet<String>();

    ZipFile zf = null;

    if (zipFileName == null) {
        return null;
    }

    try {
        zf = new ZipFile(zipFileName);

        zipFileEntries = zf.getEntries();
        while (zipFileEntries.hasMoreElements()) {
            filesInZip.add(zipFileEntries.nextElement().getName());
        }

    } finally {
        if (zf != null) {
            ZipFile.closeQuietly(zf);
        }
    }

    return filesInZip;
}

From source file:org.slc.sli.ingestion.landingzone.ZipFileUtil.java

/**
 * Retrieves a name of the first .ctl file found in the zip archive.
 *
 * @param zipFile ZIP file to scan/*from   w w  w .  ja v  a 2s  . com*/
 * @return A filename representing the control file.
 * @throws IOException IO Exception
 */
public static String getControlFileName(File zipFile) throws IOException {
    ZipFile zf = null;

    try {
        zf = new ZipFile(zipFile);

        Enumeration<ZipArchiveEntry> zes = zf.getEntries();

        while (zes.hasMoreElements()) {
            ZipArchiveEntry entry = zes.nextElement();

            if (!entry.isDirectory() && entry.getName().endsWith(".ctl")) {
                return entry.getName();
            }
        }
    } finally {
        ZipFile.closeQuietly(zf);
    }

    return null;
}

From source file:org.springframework.boot.gradle.tasks.bundling.AbstractBootArchiveTests.java

@Test
public void allEntriesUseUnixPlatformAndUtf8NameEncoding() throws IOException {
    this.task.setMainClassName("com.example.Main");
    this.task.setMetadataCharset("UTF-8");
    File classpathFolder = this.temp.newFolder();
    File resource = new File(classpathFolder, "some-resource.xml");
    resource.getParentFile().mkdirs();/*w w  w.  j a  va 2 s  .  c om*/
    resource.createNewFile();
    this.task.classpath(classpathFolder);
    this.task.execute();
    File archivePath = this.task.getArchivePath();
    try (ZipFile zip = new ZipFile(archivePath)) {
        Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();
            assertThat(entry.getPlatform()).isEqualTo(ZipArchiveEntry.PLATFORM_UNIX);
            assertThat(entry.getGeneralPurposeBit().usesUTF8ForNames()).isTrue();
        }
    }
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void allEntriesUseUnixPlatformAndUtf8NameEncoding() throws IOException {
    this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
    File source = this.testJarFile.getFile();
    File dest = this.temporaryFolder.newFile("dest.jar");
    Repackager repackager = new Repackager(source);
    repackager.repackage(dest, NO_LIBRARIES);
    try (ZipFile zip = new ZipFile(dest)) {
        Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();
            assertThat(entry.getPlatform()).isEqualTo(ZipArchiveEntry.PLATFORM_UNIX);
            assertThat(entry.getGeneralPurposeBit().usesUTF8ForNames()).isTrue();
        }/*from  www . j a  v  a 2  s  . com*/
    }
}

From source file:org.springframework.ide.eclipse.boot.wizard.content.ZipFileCodeSet.java

@Override
public <T> T each(Processor<T> processor) throws Exception {
    T result = null;/*from w  w  w. j a va 2  s . c om*/
    ZipFile zip = new ZipFile(zipDownload.getFile());
    try {
        Enumeration<ZipArchiveEntry> iter = zip.getEntries();
        while (iter.hasMoreElements() && result == null) {
            ZipArchiveEntry el = iter.nextElement();
            Path zipPath = new Path(el.getName());
            if (root.isPrefixOf(zipPath)) {
                String key = zipPath.removeFirstSegments(root.segmentCount()).toString();
                if ("".equals(key)) {
                    //path maches exactly, this means we hit the root of the
                    // code set. Do not store it because the root of a codeset
                    // is not actually an element of the codeset!
                } else {
                    CodeSetEntry cse = csEntry(zip, el);
                    result = processor.doit(cse);
                    if (result != null) {
                        //Bail out early when result found
                        return result;
                    }
                }
            }
        }
        return result;
    } finally {
        try {
            zip.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.springframework.ide.eclipse.boot.wizard.content.ZipFileCodeSet.java

@Override
public <T> T readFileEntry(String path, Processor<T> processor) throws Exception {
    ZipFile zip = new ZipFile(zipDownload.getFile());
    try {//ww w .  ja v  a2 s.  c o m
        String entryName = root.append(path).toString();
        ZipArchiveEntry entry = zip.getEntry(entryName);
        return processor.doit(entry == null ? null : csEntry(zip, entry));
    } finally {
        try {
            zip.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.xwiki.contrib.maven.PackageExtensionsMojo.java

private Collection<String> getJarsIncludedInWar(Artifact web) throws IOException, MojoExecutionException {
    if (web == null) {
        return Collections.emptyList();
    }/*from w w  w  .j av  a 2 s  . c o  m*/

    getLog().info(String.format("Excluding Base WAR [%s:%s].", web.getGroupId(), web.getArtifactId()));

    Collection<String> jars = new ArrayList<>();

    // TODO: replace this by a a method which look to the POM of the WAR
    // Open the war and list all the jars
    ZipFile zipFile = new ZipFile(web.getFile());
    Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
    while (entries.hasMoreElements()) {
        String entryName = entries.nextElement().getName();
        if (entryName.startsWith(JAR_DIRECTORY) && entryName.endsWith(".jar")) {
            jars.add(entryName.substring(JAR_DIRECTORY.length()));
        }
    }
    zipFile.close();

    return jars;
}

From source file:org.xwiki.extension.xar.internal.handler.packager.XarFile.java

public XarFile(File file, Collection<XarEntry> entries) throws IOException {
    this.zipFile = new ZipFile(file);

    for (XarEntry xarEntry : entries) {
        this.entries.put(xarEntry, xarEntry);
    }/*from www . j ava 2  s  .  c o m*/
}

From source file:org.xwiki.filemanager.internal.job.PackJobTest.java

@Test
public void pack() throws Exception {
    Folder projects = mockFolder("Projects", "Pr\u00F4j\u00EA\u00E7\u021B\u0219", null,
            Arrays.asList("Concerto", "Resilience"), Arrays.asList("key.pub"));
    File key = mockFile("key.pub", "Projects");
    when(fileSystem.canView(key.getReference())).thenReturn(false);

    mockFolder("Concerto", "Projects", Collections.<String>emptyList(), Arrays.asList("pom.xml"));
    File pom = mockFile("pom.xml", "m&y p?o#m.x=m$l", "Concerto");
    setFileContent(pom, "foo");

    Folder resilience = mockFolder("Resilience", "Projects", Arrays.asList("src"), Arrays.asList("build.xml"));
    when(fileSystem.canView(resilience.getReference())).thenReturn(false);
    mockFolder("src", "Resilience");
    mockFile("build.xml");

    File readme = mockFile("readme.txt", "r\u00E9\u00E0dm\u00E8.txt");
    setFileContent(readme, "blah");

    PackRequest request = new PackRequest();
    request.setPaths(Arrays.asList(new Path(projects.getReference()), new Path(null, readme.getReference())));
    request.setOutputFileReference(// w w w  .  j a v  a2s.c o  m
            new AttachmentReference("out.zip", new DocumentReference("wiki", "Space", "Page")));

    PackJob job = (PackJob) execute(request);

    ZipFile zip = new ZipFile(
            new java.io.File(testFolder.getRoot(), "temp/filemanager/wiki/Space/Page/out.zip"));
    List<String> folders = new ArrayList<String>();
    Map<String, String> files = new HashMap<String, String>();
    Enumeration<ZipArchiveEntry> entries = zip.getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            folders.add(entry.getName());
        } else if (zip.canReadEntryData(entry)) {
            StringWriter writer = new StringWriter();
            IOUtils.copy(zip.getInputStream(entry), writer);
            files.put(entry.getName(), writer.toString());
        }
    }
    zip.close();

    assertEquals(Arrays.asList(projects.getName() + '/', projects.getName() + "/Concerto/"), folders);
    assertEquals(2, files.size());
    assertEquals("blah", files.get(readme.getName()));
    assertEquals("foo", files.get(projects.getName() + "/Concerto/" + pom.getName()));
    assertEquals(("blah" + "foo").getBytes().length, job.getStatus().getBytesWritten());
    assertTrue(job.getStatus().getOutputFileSize() > 0);
}

From source file:org.xwiki.filter.test.internal.ZIPFileAssertComparator.java

private static Map<String, byte[]> unzip(File filename) throws IOException {
    Map<String, byte[]> zipContent = new HashMap<String, byte[]>();

    ZipFile zipFile = new ZipFile(filename);

    try {//  w w  w .  j  a va 2s  . c om
        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();

            InputStream inputStream = zipFile.getInputStream(entry);
            try {
                zipContent.put(entry.getName(), IOUtils.toByteArray(inputStream));
            } finally {
                inputStream.close();
            }
        }
    } finally {
        zipFile.close();
    }

    return zipContent;
}