Search in sources :

Example 6 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.

the class ZipUtils method isFolderExist.

/**
     * 判断在指定的zip目录下,指定的文件夹是否存在
     *
     * @param zipFile
     * @param pathName
     * @return
     */
public static boolean isFolderExist(File zipFile, String pathName) {
    ZipFile file = null;
    try {
        file = new ZipFile(zipFile);
        Enumeration<ZipArchiveEntry> en = file.getEntries();
        while (en.hasMoreElements()) {
            ZipArchiveEntry entry = en.nextElement();
            String name = entry.getName();
            if (name.startsWith(pathName)) {
                return true;
            }
        }
        return false;
    } catch (IOException e) {
    } finally {
        if (null != file) {
            try {
                file.close();
            } catch (IOException e) {
            }
        }
    }
    return false;
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException)

Example 7 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.

the class ZipUtils method isZipFile.

/**
     * <p>
     * isZipFile.
     * </p>
     *
     * @param zipFile a {@link File} object.
     * @return a boolean.
     */
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;
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) IOException(java.io.IOException)

Example 8 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.

the class ZipUtils method listZipEntries.

public static List<String> listZipEntries(File zipFile) {
    List<String> list = new ArrayList<String>();
    ZipFile zip;
    try {
        zip = new ZipFile(zipFile);
        Enumeration<ZipArchiveEntry> en = zip.getEntries();
        ZipArchiveEntry ze = null;
        while (en.hasMoreElements()) {
            ze = en.nextElement();
            String name = ze.getName();
            list.add(name);
        }
        if (null != zip)
            ZipFile.closeQuietly(zip);
    } catch (IOException e) {
    }
    return list;
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ArrayList(java.util.ArrayList) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException)

Example 9 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project buck by facebook.

the class ZipRuleIntegrationTest method shouldZipSources.

@Test
public void shouldZipSources() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "zip-rule", tmp);
    workspace.setUp();
    Path zip = workspace.buildAndReturnOutput("//example:ziptastic");
    // Make sure we have the right files and attributes.
    try (ZipFile zipFile = new ZipFile(zip.toFile())) {
        ZipArchiveEntry cake = zipFile.getEntry("cake.txt");
        assertThat(cake, Matchers.notNullValue());
        assertFalse(cake.isUnixSymlink());
        assertFalse(cake.isDirectory());
        ZipArchiveEntry beans = zipFile.getEntry("beans/");
        assertThat(beans, Matchers.notNullValue());
        assertFalse(beans.isUnixSymlink());
        assertTrue(beans.isDirectory());
        ZipArchiveEntry cheesy = zipFile.getEntry("beans/cheesy.txt");
        assertThat(cheesy, Matchers.notNullValue());
        assertFalse(cheesy.isUnixSymlink());
        assertFalse(cheesy.isDirectory());
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) Test(org.junit.Test)

Example 10 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project buck by facebook.

the class ZipStepTest method willRecurseIntoSubdirectories.

@Test
public void willRecurseIntoSubdirectories() throws IOException {
    Path parent = tmp.newFolder("zipstep");
    Path out = parent.resolve("output.zip");
    Path toZip = tmp.newFolder("zipdir");
    Files.createFile(toZip.resolve("file1.txt"));
    Files.createDirectories(toZip.resolve("child"));
    Files.createFile(toZip.resolve("child/file2.txt"));
    ZipStep step = new ZipStep(filesystem, Paths.get("zipstep/output.zip"), ImmutableSet.of(), false, ZipCompressionLevel.DEFAULT_COMPRESSION_LEVEL, Paths.get("zipdir"));
    assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode());
    // Make sure we have the right attributes.
    try (ZipFile zip = new ZipFile(out.toFile())) {
        ZipArchiveEntry entry = zip.getEntry("child/");
        assertNotEquals(entry.getUnixMode() & MoreFiles.S_IFDIR, 0);
    }
    try (Zip zip = new Zip(out, false)) {
        assertEquals(ImmutableSet.of("file1.txt", "child/file2.txt"), zip.getFileNames());
    }
}
Also used : Path(java.nio.file.Path) Zip(com.facebook.buck.testutil.Zip) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) Test(org.junit.Test)

Aggregations

ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)27 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)21 IOException (java.io.IOException)10 File (java.io.File)7 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 ZipInputStream (java.util.zip.ZipInputStream)6 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 BufferedInputStream (java.io.BufferedInputStream)4 FileNotFoundException (java.io.FileNotFoundException)4 OutputStream (java.io.OutputStream)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)2 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)1 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)1