sundar@1372: /* sundar@1372: * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. sundar@1372: * sundar@1372: * Redistribution and use in source and binary forms, with or without sundar@1372: * modification, are permitted provided that the following conditions sundar@1372: * are met: sundar@1372: * sundar@1372: * - Redistributions of source code must retain the above copyright sundar@1372: * notice, this list of conditions and the following disclaimer. sundar@1372: * sundar@1372: * - Redistributions in binary form must reproduce the above copyright sundar@1372: * notice, this list of conditions and the following disclaimer in the sundar@1372: * documentation and/or other materials provided with the distribution. sundar@1372: * sundar@1372: * - Neither the name of Oracle nor the names of its sundar@1372: * contributors may be used to endorse or promote products derived sundar@1372: * from this software without specific prior written permission. sundar@1372: * sundar@1372: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS sundar@1372: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, sundar@1372: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sundar@1372: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR sundar@1372: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, sundar@1372: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, sundar@1372: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR sundar@1372: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF sundar@1372: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING sundar@1372: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS sundar@1372: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. sundar@1372: */ sundar@1372: sundar@1372: /* sundar@1372: * Simple unzip tool using #nashorn and #java sundar@1372: * zip fs file system interface. sundar@1372: */ sundar@1372: sundar@1372: if (arguments.length == 0) { sundar@1372: print("Usage: jjs zipfs.js -- <.zip/.jar file> [out dir]"); sundar@1372: exit(1); sundar@1372: } sundar@1372: sundar@1372: var File = Java.type("java.io.File"); sundar@1372: // output directory where zip is extracted sundar@1372: var outDir = arguments[1]; sundar@1372: if (!outDir) { sundar@1372: outDir = "."; sundar@1372: } else { sundar@1372: if (! new File(outDir).isDirectory()) { sundar@1372: print(outDir + " directory does not exist!"); sundar@1372: exit(1); sundar@1372: } sundar@1372: } sundar@1372: sundar@1372: var Files = Java.type("java.nio.file.Files"); sundar@1372: var FileSystems = Java.type("java.nio.file.FileSystems"); sundar@1372: var Paths = Java.type("java.nio.file.Paths"); sundar@1372: sundar@1372: var zipfile = Paths.get(arguments[0]) sundar@1372: var fs = FileSystems.newFileSystem(zipfile, null); sundar@1372: var root = fs.rootDirectories[0]; sundar@1372: sundar@1372: // walk root and handle each Path sundar@1372: Files.walk(root).forEach( sundar@1372: function(p) { sundar@1372: var outPath = outDir + sundar@1372: p.toString().replace('/', File.separatorChar); sundar@1372: print(outPath); sundar@1372: if (Files.isDirectory(p)) { sundar@1372: // create directories as needed sundar@1372: new File(outPath).mkdirs(); sundar@1372: } else { sundar@1372: // copy a 'file' resource sundar@1372: Files.copy(p, new File(outPath).toPath()); sundar@1372: } sundar@1372: } sundar@1372: ); sundar@1372: sundar@1372: // done sundar@1372: fs.close();