Example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry getUnixMode

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

Introduction

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

Prototype

public int getUnixMode() 

Source Link

Document

Unix permission.

Usage

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);/*  ww w  .ja 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 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  ww  w. j  ava  2 s  . co  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();//w  w w. j ava2  s  .  co  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.everit.osgi.dev.maven.util.FileManager.java

private static void setPermissionsOnFile(final File file, final ZipArchiveEntry entry) throws IOException {
    if (entry.getPlatform() == ZipArchiveEntry.PLATFORM_FAT) {
        return;/* ww  w  . j  a v  a 2s. co m*/
    }
    int unixPermissions = entry.getUnixMode();

    Set<PosixFilePermission> perms = new HashSet<>();

    if ((unixPermissions & OWNER_EXECUTE_BITMASK) > 0) {
        perms.add(PosixFilePermission.OWNER_EXECUTE);
    }

    if ((unixPermissions & GROUP_EXECUTE_BITMASK) > 0) {
        perms.add(PosixFilePermission.GROUP_EXECUTE);
    }

    if ((unixPermissions & OTHERS_EXECUTE_BITMASK) > 0) {
        perms.add(PosixFilePermission.OTHERS_EXECUTE);
    }

    if ((unixPermissions & OWNER_READ_BITMASK) > 0) {
        perms.add(PosixFilePermission.OWNER_READ);
    }

    if ((unixPermissions & GROUP_READ_BITMASK) > 0) {
        perms.add(PosixFilePermission.GROUP_READ);
    }

    if ((unixPermissions & OTHERS_READ_BITMASK) > 0) {
        perms.add(PosixFilePermission.OTHERS_READ);
    }

    if ((unixPermissions & OWNER_WRITE_BITMASK) > 0) {
        perms.add(PosixFilePermission.OWNER_WRITE);
    }

    if ((unixPermissions & GROUP_WRITE_BITMASK) > 0) {
        perms.add(PosixFilePermission.GROUP_WRITE);
    }

    if ((unixPermissions & OTHERS_WRITE_BITMASK) > 0) {
        perms.add(PosixFilePermission.OTHERS_WRITE);
    }

    Path path = file.toPath();
    if (path.getFileSystem().supportedFileAttributeViews().contains("posix")) {
        Files.setPosixFilePermissions(path, perms);
    } else {
        setPermissionsOnFileInNonPosixSystem(file, perms);
    }
}

From source file:org.jboss.as.forge.util.Files.java

public static boolean extractAppServer(final String zipPath, final File target, final boolean overwrite)
        throws IOException {
    if (target.exists() && !overwrite) {
        throw new IllegalStateException(Messages.INSTANCE.getMessage("files.not.empty.directory"));
    }//from w  w w  . java2  s .  com
    // Create a temporary directory
    final File tmpDir = new File(getTempDirectory(), "jboss-as-" + zipPath.hashCode());
    if (tmpDir.exists()) {
        deleteRecursively(tmpDir);
    }
    try {
        final byte buff[] = new byte[1024];
        ZipFile file = null;
        try {
            file = new ZipFile(zipPath);
            final Enumeration<ZipArchiveEntry> entries = file.getEntries();
            while (entries.hasMoreElements()) {
                final ZipArchiveEntry entry = entries.nextElement();
                // Create the extraction target
                final File extractTarget = new File(tmpDir, entry.getName());
                if (entry.isDirectory()) {
                    extractTarget.mkdirs();
                } else {
                    final File parent = new File(extractTarget.getParent());
                    parent.mkdirs();
                    final BufferedInputStream in = new BufferedInputStream(file.getInputStream(entry));
                    try {
                        final BufferedOutputStream out = new BufferedOutputStream(
                                new FileOutputStream(extractTarget));
                        try {
                            int read;
                            while ((read = in.read(buff)) != -1) {
                                out.write(buff, 0, read);
                            }
                        } finally {
                            Streams.safeClose(out);
                        }
                    } finally {
                        Streams.safeClose(in);
                    }
                    // Set the file permissions
                    if (entry.getUnixMode() > 0) {
                        setPermissions(extractTarget, FilePermissions.of(entry.getUnixMode()));
                    }
                }
            }
        } catch (IOException e) {
            throw new IOException(Messages.INSTANCE.getMessage("files.extraction.error", file), e);
        } finally {
            ZipFile.closeQuietly(file);
            // Streams.safeClose(file);
        }
        // If the target exists, remove then rename
        if (target.exists()) {
            deleteRecursively(target);
        }
        // First child should be a directory and there should only be one child
        final File[] children = tmpDir.listFiles();
        if (children != null && children.length == 1) {
            return moveDirectory(children[0], target);
        }
        return moveDirectory(tmpDir, target);

    } finally {
        deleteRecursively(tmpDir);
    }
}

From source file:org.jboss.qa.jenkins.test.executor.utils.unpack.UnZipper.java

@Override
public void unpack(File archive, File destination) throws IOException {
    try (ZipFile zip = new ZipFile(archive)) {
        final Set<ZipArchiveEntry> entries = new HashSet<>(Collections.list(zip.getEntries()));

        if (ignoreRootFolders) {
            pathSegmentsToTrim = countRootFolders(entries);
        }//from ww w.j a  v a2s.c o m

        for (ZipArchiveEntry entry : entries) {
            if (entry.isDirectory()) {
                continue;
            }
            final String zipPath = trimPathSegments(entry.getName(), pathSegmentsToTrim);
            final File file = new File(destination, zipPath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs(); // Create parent folders if not exist
            }

            try (InputStream is = zip.getInputStream(entry); OutputStream fos = new FileOutputStream(file)) {
                IOUtils.copy(is, fos);
                // check for user-executable bit on entry and apply to file
                if ((entry.getUnixMode() & 0100) != 0) {
                    file.setExecutable(true);
                }
            }
            file.setLastModified(entry.getTime());
        }
    }
}

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

/**
 * Create a CodeSetEntry that wraps a ZipEntry
 *//*from  ww  w .  java2  s . c om*/
private CodeSetEntry csEntry(final ZipFile zip, final ZipArchiveEntry e) {
    IPath zipPath = new Path(e.getName()); //path relative to zip file
    Assert.isTrue(root.isPrefixOf(zipPath));
    final IPath csPath = zipPath.removeFirstSegments(root.segmentCount());
    return new CodeSetEntry() {
        @Override
        public IPath getPath() {
            return csPath;
        }

        @Override
        public String toString() {
            return getPath() + " in " + zipDownload;
        }

        @Override
        public boolean isDirectory() {
            return e.isDirectory();
        }

        @Override
        public int getUnixMode() {
            return e.getUnixMode();
        }

        @Override
        public InputStream getData() throws IOException {
            return zip.getInputStream(e);
        }
    };
}