Search in sources :

Example 1 with EOLFixingFileOutputStream

use of org.jboss.fuse.patch.management.io.EOLFixingFileOutputStream in project fuse-karaf by jboss-fuse.

the class GitPatchManagementServiceImpl method unpack.

/**
 * Unpacks a ZIP file to targetDirectory
 * @param zipFile
 * @param targetDirectory
 * @param skipInitialDirectories how many levels of a path to skip when unpacking (like skipping base directory inside ZIP)
 * @throws IOException
 */
public static void unpack(File zipFile, File targetDirectory, int skipInitialDirectories) throws IOException {
    try (ZipFile zf = new ZipFile(zipFile)) {
        for (Enumeration<ZipArchiveEntry> e = zf.getEntries(); e.hasMoreElements(); ) {
            ZipArchiveEntry entry = e.nextElement();
            String name = entry.getName();
            int skip = skipInitialDirectories;
            while (skip-- > 0) {
                name = name.substring(name.indexOf('/') + 1);
            }
            if (entry.isDirectory()) {
                mkdirs(new File(targetDirectory, name));
            } else /*if (!entry.isUnixSymlink())*/
            {
                File file = new File(targetDirectory, name);
                mkdirs(file.getParentFile());
                FileOutputStream output = new EOLFixingFileOutputStream(targetDirectory, file);
                IOUtils.copyLarge(zf.getInputStream(entry), output);
                closeQuietly(output);
                if (Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class) != null) {
                    Files.setPosixFilePermissions(file.toPath(), getPermissionsFromUnixMode(file, entry.getUnixMode()));
                }
            }
        }
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) EOLFixingFileOutputStream(org.jboss.fuse.patch.management.io.EOLFixingFileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) EOLFixingFileOutputStream(org.jboss.fuse.patch.management.io.EOLFixingFileOutputStream) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 2 with EOLFixingFileOutputStream

use of org.jboss.fuse.patch.management.io.EOLFixingFileOutputStream in project fuse-karaf by jboss-fuse.

the class GitPatchManagementServiceImpl method unzipKarafInstanceJar.

/**
 * Unzips <code>bin</code> and <code>etc</code> from org.apache.karaf.instance.core.
 * @param artifact
 * @param targetDirectory
 * @throws IOException
 */
private void unzipKarafInstanceJar(File artifact, File targetDirectory) throws IOException {
    String prefix = "org/apache/karaf/instance/resources/";
    try (ZipFile zf = new ZipFile(artifact)) {
        for (Enumeration<ZipArchiveEntry> e = zf.getEntries(); e.hasMoreElements(); ) {
            ZipArchiveEntry entry = e.nextElement();
            String name = entry.getName();
            if (!name.startsWith(prefix)) {
                continue;
            }
            name = name.substring(prefix.length());
            if (!name.startsWith("bin") && !name.startsWith("etc")) {
                continue;
            }
            // flags from karaf.instance.core
            // see: org.apache.karaf.instance.core.internal.InstanceServiceImpl.createInstance()
            boolean windows = System.getProperty("os.name").startsWith("Win");
            boolean cygwin = windows && new File(System.getProperty("karaf.home"), "bin/instance").exists();
            if (!entry.isDirectory() && !entry.isUnixSymlink()) {
                if (windows && !cygwin) {
                    if (name.startsWith("bin/") && !name.endsWith(".bat")) {
                        continue;
                    }
                } else {
                    if (name.startsWith("bin/") && name.endsWith(".bat")) {
                        continue;
                    }
                }
                File file = new File(targetDirectory, name);
                Utils.mkdirs(file.getParentFile());
                FileOutputStream output = new EOLFixingFileOutputStream(targetDirectory, file);
                IOUtils.copyLarge(zf.getInputStream(entry), output);
                Utils.closeQuietly(output);
                if (Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class) != null) {
                    if (name.startsWith("bin/") && !name.endsWith(".bat")) {
                        Files.setPosixFilePermissions(file.toPath(), getPermissionsFromUnixMode(file, 0775));
                    }
                }
            }
        }
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) EOLFixingFileOutputStream(org.jboss.fuse.patch.management.io.EOLFixingFileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) EOLFixingFileOutputStream(org.jboss.fuse.patch.management.io.EOLFixingFileOutputStream) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Aggregations

File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)2 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)2 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)2 EOLFixingFileOutputStream (org.jboss.fuse.patch.management.io.EOLFixingFileOutputStream)2