@Test public void testZipDirectory() throws Exception { Path source = resourcesRoot.resolve("TestJarUtils"); Path target = resourcesRoot.resolve(BUILT_JAR_NAME); target = JarUtils.zipDirectory(source, target); Manifest manifest = JarUtils.getManifest(target.toFile()); assertNotNull(manifest); Attributes mainAttributes = manifest.getMainAttributes(); assertNotNull(mainAttributes); assertEquals("1.0", mainAttributes.getValue("Manifest-Version")); assertEquals("Nuxeo JarUtils test", mainAttributes.getValue("Bundle-Name")); assertEquals("2", mainAttributes.getValue("Bundle-ManifestVersion")); assertEquals("org.nuxeo.common.jarutils.test;singleton:=true", mainAttributes.getValue("Bundle-SymbolicName")); }
public static Path zipDirectory(Path source, Path target, CopyOption... options) throws IOException { if (!Files.isDirectory(source)) { throw new IllegalArgumentException("Source argument must be a directory to zip"); } URI uri = toJarURI(target); try (FileSystem zipfs = FileSystems.newFileSystem(uri, Collections.singletonMap("create", "true"))) { Files.walkFileTree(source, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (source.equals(dir)) { return FileVisitResult.CONTINUE; } return visitFile(dir, attrs); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path relativePath = source.relativize(file); Path pathInZipFile = zipfs.getPath(relativePath.toString()); Files.copy(file, pathInZipFile, options); return FileVisitResult.CONTINUE; } }); } return target; }
JarUtils { public static Path zipDirectory(Path source, Path target, CopyOption... options) throws IOException { if (!Files.isDirectory(source)) { throw new IllegalArgumentException("Source argument must be a directory to zip"); } URI uri = toJarURI(target); try (FileSystem zipfs = FileSystems.newFileSystem(uri, Collections.singletonMap("create", "true"))) { Files.walkFileTree(source, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (source.equals(dir)) { return FileVisitResult.CONTINUE; } return visitFile(dir, attrs); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path relativePath = source.relativize(file); Path pathInZipFile = zipfs.getPath(relativePath.toString()); Files.copy(file, pathInZipFile, options); return FileVisitResult.CONTINUE; } }); } return target; } }
JarUtils { public static Path zipDirectory(Path source, Path target, CopyOption... options) throws IOException { if (!Files.isDirectory(source)) { throw new IllegalArgumentException("Source argument must be a directory to zip"); } URI uri = toJarURI(target); try (FileSystem zipfs = FileSystems.newFileSystem(uri, Collections.singletonMap("create", "true"))) { Files.walkFileTree(source, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (source.equals(dir)) { return FileVisitResult.CONTINUE; } return visitFile(dir, attrs); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path relativePath = source.relativize(file); Path pathInZipFile = zipfs.getPath(relativePath.toString()); Files.copy(file, pathInZipFile, options); return FileVisitResult.CONTINUE; } }); } return target; } private  JarUtils(); }
JarUtils { public static Path zipDirectory(Path source, Path target, CopyOption... options) throws IOException { if (!Files.isDirectory(source)) { throw new IllegalArgumentException("Source argument must be a directory to zip"); } URI uri = toJarURI(target); try (FileSystem zipfs = FileSystems.newFileSystem(uri, Collections.singletonMap("create", "true"))) { Files.walkFileTree(source, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (source.equals(dir)) { return FileVisitResult.CONTINUE; } return visitFile(dir, attrs); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path relativePath = source.relativize(file); Path pathInZipFile = zipfs.getPath(relativePath.toString()); Files.copy(file, pathInZipFile, options); return FileVisitResult.CONTINUE; } }); } return target; } private  JarUtils(); static Manifest getManifest(File file); static Manifest getDirectoryManifest(File file); static Manifest getJarManifest(File file); static Manifest getManifest(URL url); static Path zipDirectory(Path source, Path target, CopyOption... options); static URI toJarURI(Path path); }
JarUtils { public static Path zipDirectory(Path source, Path target, CopyOption... options) throws IOException { if (!Files.isDirectory(source)) { throw new IllegalArgumentException("Source argument must be a directory to zip"); } URI uri = toJarURI(target); try (FileSystem zipfs = FileSystems.newFileSystem(uri, Collections.singletonMap("create", "true"))) { Files.walkFileTree(source, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (source.equals(dir)) { return FileVisitResult.CONTINUE; } return visitFile(dir, attrs); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path relativePath = source.relativize(file); Path pathInZipFile = zipfs.getPath(relativePath.toString()); Files.copy(file, pathInZipFile, options); return FileVisitResult.CONTINUE; } }); } return target; } private  JarUtils(); static Manifest getManifest(File file); static Manifest getDirectoryManifest(File file); static Manifest getJarManifest(File file); static Manifest getManifest(URL url); static Path zipDirectory(Path source, Path target, CopyOption... options); static URI toJarURI(Path path);  }