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.betaconceptframework.astroboa.engine.jcr.io.ContentSourceExtractor.java

public InputStream extractStream(URI contentSource) throws Exception {
    dispose();/*from ww  w .  j a  v a 2s.  c om*/

    String filename = contentSource.toURL().getFile();

    if (StringUtils.isBlank(filename)) {
        throw new Exception("No file name from URL " + contentSource.toString());
    }

    if (filename.endsWith(".zip")) {
        tmpZip = File.createTempFile("rep", ".zip");

        FileUtils.copyURLToFile(contentSource.toURL(), tmpZip);

        zipFile = new ZipFile(tmpZip);

        Enumeration entries = zipFile.getEntries();

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

            if (entry.getName() != null && entry.getName().endsWith(".xml")) {
                return zipFile.getInputStream(entry);
            }
        }

        return null;
    } else if (filename.endsWith(".xml")) {
        return contentSource.toURL().openStream();
    } else {
        throw new Exception("Unsupported file extension " + filename);
    }
}

From source file:org.cloudfoundry.util.ResourceMatchingUtils.java

private static Flux<ArtifactMetadata> getArtifactMetadataFromZip(Path application) {
    List<ArtifactMetadata> artifactMetadatas = new ArrayList<>();

    try (ZipFile zipFile = new ZipFile(application.toFile())) {
        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();

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

            if (!entry.isDirectory()) {
                try (InputStream in = zipFile.getInputStream(entry)) {
                    String hash = FileUtils.hash(in);
                    String path = entry.getName();
                    String permissions = FileUtils.permissions(entry.getUnixMode());
                    int size = (int) entry.getSize();

                    artifactMetadatas.add(new ArtifactMetadata(hash, path, permissions, size));
                }/*from www .  j  a  v a2  s . co  m*/

            }
        }
    } catch (IOException e) {
        throw Exceptions.propagate(e);
    }

    return Flux.fromIterable(artifactMetadatas);
}

From source file:org.codehaus.mojo.unix.maven.zip.ZipPackageTest.java

@SuppressWarnings("OctalInteger")
public void testBasic() throws Exception {
    File zip1 = testUtil.getTestFile("src/test/resources/zip/zip-1");
    File zip = testUtil.getTestFile("target/zip/zip-1/test.zip");
    if (!zip.getParentFile().isDirectory()) {
        assertTrue(zip.getParentFile().mkdirs());
    }//from w w w .ja  va  2  s  . com

    LocalFs basedir = new LocalFs(zip1);

    ZipUnixPackage zipPackage = new ZipUnixPackage(new SystemStreamLog());

    zipPackage.beforeAssembly(EMPTY.mode(UnixFileMode._0755), timestamp);

    assertTrue(basedir.isDirectory());

    // Git set the timestamp of file objects
    assertTrue(new File(zip1, "dirs").setLastModified(dirsTimestamp.toDateTime().getMillis()));
    assertTrue(new File(zip1, "dirs/bar.txt").setLastModified(dirsBarTxtTimestamp.toDateTime().getMillis()));
    assertTrue(new File(zip1, "file").setLastModified(fileTimestamp.toDateTime().getMillis()));
    assertTrue(new File(zip1, "file/foo.txt").setLastModified(fileFooTxtTimestamp.toDateTime().getMillis()));

    Replacer replacer = new Replacer("@bar@", "awesome");

    new CreateDirectoriesOperation(timestamp, new String[] { "/opt/hudson" }, EMPTY).perform(zipPackage);

    new CopyDirectoryOperation(basedir, relativePath(""), List.<String>nil(), List.<String>nil(),
            Option.<P2<String, String>>none(), EMPTY, EMPTY).perform(zipPackage);

    new CopyFileOperation(EMPTY, basedir.resolve("file/foo.txt"), relativePath("/file/foo.txt"))
            .perform(zipPackage);

    new SymlinkOperation(relativePath("/var/log/hudson"), "/var/opt/hudson/log", Option.<String>none(),
            Option.<String>none()).perform(zipPackage);

    new FilterFilesOperation(single("dirs/**"), List.<String>nil(), single(replacer), LineEnding.unix)
            .perform(zipPackage);

    UnixFileMode fileMode = UnixFileMode.fromInt(0600);
    UnixFileMode dirMode = _0777;

    FileAttributes fileAttributes = new FileAttributes("root", "root", fileMode);
    FileAttributes directoryAttributes = new FileAttributes("root", "root", dirMode);
    new SetAttributesOperation(BASE, single("**/*"), List.<String>nil(), some(fileAttributes),
            some(directoryAttributes)).perform(zipPackage);

    zipPackage.prepare(SINGLE).packageToFile(zip);

    ZipFile file = new ZipFile(zip);
    Enumeration<ZipArchiveEntry> enumeration = file.getEntriesInPhysicalOrder();
    assertDirectory(enumeration.nextElement(), "./dirs/", dirsTimestamp);
    // Is it really correct that filtered files should retain the old timestamp?
    assertFile(file, enumeration.nextElement(), "./dirs/bar.txt", 8, dirsBarTxtTimestamp, "awesome\n",
            fileMode);
    assertDirectory(enumeration.nextElement(), "./file/", fileTimestamp);
    assertFile(file, enumeration.nextElement(), "./file/foo.txt", 6, fileFooTxtTimestamp, "@foo@\n", fileMode);
    assertDirectory(enumeration.nextElement(), "./opt/", timestamp);
    assertDirectory(enumeration.nextElement(), "./opt/hudson/", timestamp);
    assertFalse(enumeration.hasMoreElements());
    file.close();
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testImplicitPermissions() throws IOException {
    File zipFile = getTestFile("target/output/zip-with-implicit-dirmode.zip");

    ZipArchiver archiver = getZipArchiver(zipFile);

    archiver.setDefaultDirectoryMode(0777);
    archiver.setDirectoryMode(0641);/*from  ww  w  .j a v  a  2  s.co m*/
    archiver.setFileMode(0222);
    archiver.addFile(new File("pom.xml"), "fizz/buzz/pom.xml");
    archiver.setDefaultDirectoryMode(0530);
    archiver.setDirectoryMode(-1); // Not forced mode
    archiver.setFileMode(0111);
    archiver.addFile(new File("pom.xml"), "fazz/bazz/pam.xml");
    archiver.createArchive();

    assertTrue(zipFile.exists());
    ZipFile zf = new ZipFile(zipFile);
    ZipArchiveEntry fizz = zf.getEntry("fizz/");
    assertEquals(040641, fizz.getUnixMode());
    ZipArchiveEntry pom = zf.getEntry("fizz/buzz/pom.xml");
    assertEquals(0100222, pom.getUnixMode());

    ZipArchiveEntry fazz = zf.getEntry("fazz/");
    assertEquals(040530, fazz.getUnixMode());
    ZipArchiveEntry pam = zf.getEntry("fazz/bazz/pam.xml");
    assertEquals(0100111, pam.getUnixMode());
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testOverddidenPermissions() throws IOException {
    File zipFile = getTestFile("target/output/zip-with-overriden-modes.zip");

    ZipArchiver archiver = getZipArchiver(zipFile);
    archiver.setDefaultDirectoryMode(0777);
    archiver.setDirectoryMode(0641);/* w  w  w  . ja v  a 2s .  c om*/
    archiver.setFileMode(0777);
    archiver.addDirectory(new File("src/test/resources/symlinks/src"));
    archiver.createArchive();

    assertTrue(zipFile.exists());
    ZipFile zf = new ZipFile(zipFile);
    ZipArchiveEntry fizz = zf.getEntry("symDir");
    assertTrue(fizz.isUnixSymlink());
    ZipArchiveEntry symR = zf.getEntry("symR");
    assertTrue(symR.isUnixSymlink());
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testCreateArchiveWithDetectedModes() throws Exception {

    String[] executablePaths = { "path/to/executable", "path/to/executable.bat" };

    String[] confPaths = { "path/to/etc/file", "path/to/etc/file2" };

    String[] logPaths = { "path/to/logs/log.txt" };

    int exeMode = 0777;
    int confMode = 0600;
    int logMode = 0640;

    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        StackTraceElement e = new Throwable().getStackTrace()[0];
        System.out//from w  w w .j a v a  2s  . c  o  m
                .println("Cannot execute test: " + e.getMethodName() + " on " + System.getProperty("os.name"));
        return;
    }

    File tmpDir = null;
    try {
        tmpDir = File.createTempFile("zip-with-chmod.", ".dir");
        tmpDir.delete();

        tmpDir.mkdirs();

        for (String executablePath : executablePaths) {
            writeFile(tmpDir, executablePath, exeMode);
        }

        for (String confPath : confPaths) {
            writeFile(tmpDir, confPath, confMode);
        }

        for (String logPath : logPaths) {
            writeFile(tmpDir, logPath, logMode);
        }

        {
            Map<String, PlexusIoResourceAttributes> attributesByPath = PlexusIoResourceAttributeUtils
                    .getFileAttributesByPath(tmpDir);
            for (String path : executablePaths) {
                PlexusIoResourceAttributes attrs = attributesByPath.get(path);
                if (attrs == null) {
                    attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
                }

                assertNotNull(attrs);
                assertEquals("Wrong mode for: " + path + "; expected: " + exeMode, exeMode,
                        attrs.getOctalMode());
            }

            for (String path : confPaths) {
                PlexusIoResourceAttributes attrs = attributesByPath.get(path);
                if (attrs == null) {
                    attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
                }

                assertNotNull(attrs);
                assertEquals("Wrong mode for: " + path + "; expected: " + confMode, confMode,
                        attrs.getOctalMode());
            }

            for (String path : logPaths) {
                PlexusIoResourceAttributes attrs = attributesByPath.get(path);
                if (attrs == null) {
                    attrs = attributesByPath.get(new File(tmpDir, path).getAbsolutePath());
                }

                assertNotNull(attrs);
                assertEquals("Wrong mode for: " + path + "; expected: " + logMode, logMode,
                        attrs.getOctalMode());
            }
        }

        File zipFile = getTestFile("target/output/zip-with-modes.zip");

        ZipArchiver archiver = getZipArchiver(zipFile);

        archiver.addDirectory(tmpDir);
        archiver.createArchive();

        assertTrue(zipFile.exists());

        File zipFile2 = getTestFile("target/output/zip-with-modes-L2.zip");

        archiver = getZipArchiver();
        archiver.setDestFile(zipFile2);

        archiver.addArchivedFileSet(zipFile);
        archiver.createArchive();

        ZipFile zf = new ZipFile(zipFile2);

        for (String path : executablePaths) {
            ZipArchiveEntry ze = zf.getEntry(path);

            int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

            assertEquals("Wrong mode for: " + path + "; expected: " + exeMode, exeMode, mode);
        }

        for (String path : confPaths) {
            ZipArchiveEntry ze = zf.getEntry(path);

            int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

            assertEquals("Wrong mode for: " + path + "; expected: " + confMode, confMode, mode);
        }

        for (String path : logPaths) {
            ZipArchiveEntry ze = zf.getEntry(path);

            int mode = ze.getUnixMode() & UnixStat.PERM_MASK;

            assertEquals("Wrong mode for: " + path + "; expected: " + logMode, logMode, mode);
        }
    } finally {
        if (tmpDir != null && tmpDir.exists()) {
            try {
                FileUtils.forceDelete(tmpDir);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

private void createArchive(ZipArchiver archiver) throws ArchiverException, IOException {
    archiver.createArchive();/*from w  ww  . ja  v  a2 s. c o  m*/

    ZipFile zf = new ZipFile(archiver.getDestFile());

    Enumeration e = zf.getEntries();

    while (e.hasMoreElements()) {
        ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
        if (ze.isDirectory()) {
            if (ze.getName().startsWith("worldwritable")) {
                fileModeAssert(0777, UnixStat.PERM_MASK & ze.getUnixMode());
            } else if (ze.getName().startsWith("groupwritable")) {
                fileModeAssert(0070, UnixStat.PERM_MASK & ze.getUnixMode());
            } else {
                fileModeAssert(0500, UnixStat.PERM_MASK & ze.getUnixMode());
            }
        } else {
            if (ze.getName().equals("one.txt")) {
                fileModeAssert(0640, UnixStat.PERM_MASK & ze.getUnixMode());
            } else if (ze.getName().equals("two.txt")) {
                fileModeAssert(0664, UnixStat.PERM_MASK & ze.getUnixMode());
            } else if (ze.isUnixSymlink()) {
                //         assertEquals( ze.getName(), 0500, UnixStat.PERM_MASK & ze.getUnixMode() );
            } else {
                fileModeAssert(0400, UnixStat.PERM_MASK & ze.getUnixMode());
            }
        }

    }
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testSymlinkArchivedFileSet() throws Exception {
    final File zipFile = getTestFile("src/test/resources/symlinks/symlinks.zip");
    final File zipFile2 = getTestFile("target/output/pasymlinks-archivedFileset.zip");
    final ZipArchiver zipArchiver = getZipArchiver(zipFile2);
    zipArchiver.addArchivedFileSet(zipFile);
    zipArchiver.createArchive();/*w w  w  .j a v a 2 s  .c  o  m*/

    final ZipFile cmp1 = new ZipFile(zipFile);
    final ZipFile cmp2 = new ZipFile(zipFile2);
    ArchiveFileComparator.assertEquals(cmp1, cmp2, "");
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testCreateResourceCollection() throws Exception {
    final File srcDir = new File("src");
    final File zipFile = new File("target/output/src.zip");
    ZipArchiver zipArchiver = getZipArchiver(zipFile);
    zipArchiver.addDirectory(srcDir, null, FileUtils.getDefaultExcludes());
    zipArchiver.setEncoding("UTF-8");
    FileUtils.removePath(zipFile.getPath());
    zipArchiver.createArchive();// w  ww.  j  av a  2s  .c  o  m

    final File zipFile2 = new File("target/output/src2.zip");
    ZipArchiver zipArchiver2 = getZipArchiver(zipFile2);
    zipArchiver2.addArchivedFileSet(zipFile, "prfx/");
    zipArchiver2.setEncoding("UTF-8");
    FileUtils.removePath(zipFile2.getPath());
    zipArchiver2.createArchive();

    final ZipFile cmp1 = new ZipFile(zipFile);
    final ZipFile cmp2 = new ZipFile(zipFile2);
    ArchiveFileComparator.assertEquals(cmp1, cmp2, "prfx/");
    cmp1.close();
    cmp2.close();
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testForcedFileModes() throws IOException {
    File step1file = new File("target/output/forced-file-mode.zip");
    {/*  www .java 2s . c o  m*/
        final ZipArchiver zipArchiver = getZipArchiver(step1file);
        zipArchiver.setFileMode(0077);
        zipArchiver.setDirectoryMode(0007);
        PlexusIoResourceAttributes attrs = new SimpleResourceAttributes(123, "fred", 22, "filntstones", 0111);
        PlexusIoResource resource = ResourceFactory
                .createResource(new File("src/test/resources/folders/File.txt"), "Test.txt", null, attrs);
        zipArchiver.addResource(resource, "Test2.txt", 0707);
        PlexusIoFileResourceCollection files = new PlexusIoFileResourceCollection();
        files.setBaseDir(new File("src/test/resources/folders"));
        files.setPrefix("sixsixsix/");
        zipArchiver.addResources(files);

        zipArchiver.createArchive();

        ZipFile zf = new ZipFile(step1file);
        fileModeAssert(040007, zf.getEntry("sixsixsix/a/").getUnixMode());
        fileModeAssert(0100077, zf.getEntry("sixsixsix/b/FileInB.txt").getUnixMode());
        fileModeAssert(0100707, zf.getEntry("Test2.txt").getUnixMode());
        zf.close();
    }

    File Step2file = new File("target/output/forced-file-mode-from-zip.zip");
    {
        final ZipArchiver za2 = getZipArchiver(Step2file);
        za2.setFileMode(0666);
        za2.setDirectoryMode(0676);

        PlexusIoZipFileResourceCollection zipSrc = new PlexusIoZipFileResourceCollection();
        zipSrc.setFile(step1file);
        zipSrc.setPrefix("zz/");
        za2.addResources(zipSrc);
        za2.createArchive();
        ZipFile zf = new ZipFile(Step2file);
        fileModeAssert(040676, zf.getEntry("zz/sixsixsix/a/").getUnixMode());
        fileModeAssert(0100666, zf.getEntry("zz/Test2.txt").getUnixMode());
        zf.close();
    }

    File step3file = new File("target/output/forced-file-mode-from-zip2.zip");
    {
        final ZipArchiver za2 = getZipArchiver(step3file);
        za2.setFileMode(0666);
        za2.setDirectoryMode(0676);

        PlexusArchiverZipFileResourceCollection zipSrc = new PlexusArchiverZipFileResourceCollection();
        zipSrc.setFile(step1file);
        zipSrc.setPrefix("zz/");
        za2.addResources(zipSrc);
        za2.createArchive();
        ZipFile zf = new ZipFile(Step2file);
        fileModeAssert(040676, zf.getEntry("zz/sixsixsix/a/").getUnixMode());
        fileModeAssert(0100666, zf.getEntry("zz/Test2.txt").getUnixMode());
        zf.close();
    }
}