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        FileInputStream fis = null;
062        try {
063            fis = new FileInputStream(new File(file, "META-INF/MANIFEST.MF"));
064            return new Manifest(fis);
065        } finally {
066            if (fis != null) {
067                fis.close();
068            }
069        }
070    }
071
072    public static Manifest getJarManifest(File file) throws IOException {
073        JarFile jar = null;
074        try {
075            jar = new JarFile(file);
076            return jar.getManifest();
077        } finally {
078            if (jar != null) {
079                jar.close();
080            }
081        }
082    }
083
084    public static Manifest getManifest(URL url) {
085        try {
086            return new JarFile(new File(url.getFile())).getManifest();
087        } catch (IOException e) {
088            return null;
089        }
090    }
091
092    /**
093     * Zips recursively as a jar the content of {@code source} to the {@code target} zip file.
094     *
095     * @since 9.3
096     */
097    public static Path zipDirectory(Path source, Path target, CopyOption... options) throws IOException {
098        if (!Files.isDirectory(source)) {
099            throw new IllegalArgumentException("Source argument must be a directory to zip");
100        }
101        // locate file system by using the syntax defined in java.net.JarURLConnection
102        URI uri = toJarURI(target);
103
104        try (FileSystem zipfs = FileSystems.newFileSystem(uri, Collections.singletonMap("create", "true"))) {
105            Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
106
107                @Override
108                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
109                    if (source.equals(dir)) {
110                        // don't process root element
111                        return FileVisitResult.CONTINUE;
112                    }
113                    return visitFile(dir, attrs);
114                }
115
116                @Override
117                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
118                    // retrieve the destination path in zip
119                    Path relativePath = source.relativize(file);
120                    Path pathInZipFile = zipfs.getPath(relativePath.toString());
121                    // copy a file into the zip file
122                    Files.copy(file, pathInZipFile, options);
123                    return FileVisitResult.CONTINUE;
124                }
125
126            });
127        }
128        return target;
129    }
130
131    /**
132     * Convert a {@link Path} to an {@link URI} with {@code jar} scheme.
133     *
134     * @since 9.10
135     */
136    public static URI toJarURI(Path path) {
137        try {
138            return new URI("jar", path.toUri().toString(), null);
139        } catch (URISyntaxException e) {
140            throw new IllegalArgumentException("Unable to create Jar URI", e);
141        }
142    }
143
144}