001/*
002 * (C) Copyright 2006-2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.common.utils;
020
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.IOException;
024import java.net.URI;
025import java.net.URISyntaxException;
026import java.net.URL;
027import java.nio.file.CopyOption;
028import java.nio.file.FileSystem;
029import java.nio.file.FileSystems;
030import java.nio.file.FileVisitResult;
031import java.nio.file.Files;
032import java.nio.file.Path;
033import java.nio.file.SimpleFileVisitor;
034import java.nio.file.attribute.BasicFileAttributes;
035import java.util.Collections;
036import java.util.jar.JarFile;
037import java.util.jar.Manifest;
038
039/**
040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
041 */
042public final class JarUtils {
043
044    // Utility class.
045    private JarUtils() {
046    }
047
048    public static Manifest getManifest(File file) {
049        try {
050            if (file.isDirectory()) {
051                return getDirectoryManifest(file);
052            } else {
053                return getJarManifest(file);
054            }
055        } catch (IOException ignored) {
056            return null;
057        }
058    }
059
060    public static Manifest getDirectoryManifest(File file) throws IOException {
061        try (FileInputStream fis = new FileInputStream(new File(file, "META-INF/MANIFEST.MF"));) {
062            return new Manifest(fis);
063        }
064    }
065
066    public static Manifest getJarManifest(File file) throws IOException {
067        try (JarFile jar = new JarFile(file)) {
068            return jar.getManifest();
069        }
070    }
071
072    public static Manifest getManifest(URL url) {
073        try (JarFile jarFile = new JarFile(new File(url.getFile()))) {
074            return jarFile.getManifest();
075        } catch (IOException e) {
076            return null;
077        }
078    }
079
080    /**
081     * Zips recursively as a jar the content of {@code source} to the {@code target} zip file.
082     *
083     * @since 9.3
084     */
085    public static Path zipDirectory(Path source, Path target, CopyOption... options) throws IOException {
086        if (!Files.isDirectory(source)) {
087            throw new IllegalArgumentException("Source argument must be a directory to zip");
088        }
089        // locate file system by using the syntax defined in java.net.JarURLConnection
090        URI uri = toJarURI(target);
091
092        try (FileSystem zipfs = FileSystems.newFileSystem(uri, Collections.singletonMap("create", "true"))) {
093            Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
094
095                @Override
096                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
097                    if (source.equals(dir)) {
098                        // don't process root element
099                        return FileVisitResult.CONTINUE;
100                    }
101                    return visitFile(dir, attrs);
102                }
103
104                @Override
105                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
106                    // retrieve the destination path in zip
107                    Path relativePath = source.relativize(file);
108                    Path pathInZipFile = zipfs.getPath(relativePath.toString());
109                    // copy a file into the zip file
110                    Files.copy(file, pathInZipFile, options);
111                    return FileVisitResult.CONTINUE;
112                }
113
114            });
115        }
116        return target;
117    }
118
119    /**
120     * Convert a {@link Path} to an {@link URI} with {@code jar} scheme.
121     *
122     * @since 9.10
123     */
124    public static URI toJarURI(Path path) {
125        try {
126            return new URI("jar", path.toUri().toString(), null);
127        } catch (URISyntaxException e) {
128            throw new IllegalArgumentException("Unable to create Jar URI", e);
129        }
130    }
131
132}