samples/unzip.js

Fri, 05 Jun 2015 12:38:53 +0200

author
mhaupt
date
Fri, 05 Jun 2015 12:38:53 +0200
changeset 1398
2f1b9f4daec1
parent 1372
644d9b9c97ed
permissions
-rw-r--r--

8080087: Nashorn $ENV.PWD is originally undefined
Summary: On Windows, the PWD environment variable does not exist and cannot be imported in scripting mode, so it is set explicitly.
Reviewed-by: lagergren, sundar

sundar@1372 1 /*
sundar@1372 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
sundar@1372 3 *
sundar@1372 4 * Redistribution and use in source and binary forms, with or without
sundar@1372 5 * modification, are permitted provided that the following conditions
sundar@1372 6 * are met:
sundar@1372 7 *
sundar@1372 8 * - Redistributions of source code must retain the above copyright
sundar@1372 9 * notice, this list of conditions and the following disclaimer.
sundar@1372 10 *
sundar@1372 11 * - Redistributions in binary form must reproduce the above copyright
sundar@1372 12 * notice, this list of conditions and the following disclaimer in the
sundar@1372 13 * documentation and/or other materials provided with the distribution.
sundar@1372 14 *
sundar@1372 15 * - Neither the name of Oracle nor the names of its
sundar@1372 16 * contributors may be used to endorse or promote products derived
sundar@1372 17 * from this software without specific prior written permission.
sundar@1372 18 *
sundar@1372 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
sundar@1372 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
sundar@1372 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sundar@1372 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
sundar@1372 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
sundar@1372 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
sundar@1372 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
sundar@1372 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
sundar@1372 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
sundar@1372 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
sundar@1372 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sundar@1372 30 */
sundar@1372 31
sundar@1372 32 /*
sundar@1372 33 * Simple unzip tool using #nashorn and #java
sundar@1372 34 * zip fs file system interface.
sundar@1372 35 */
sundar@1372 36
sundar@1372 37 if (arguments.length == 0) {
sundar@1372 38 print("Usage: jjs zipfs.js -- <.zip/.jar file> [out dir]");
sundar@1372 39 exit(1);
sundar@1372 40 }
sundar@1372 41
sundar@1372 42 var File = Java.type("java.io.File");
sundar@1372 43 // output directory where zip is extracted
sundar@1372 44 var outDir = arguments[1];
sundar@1372 45 if (!outDir) {
sundar@1372 46 outDir = ".";
sundar@1372 47 } else {
sundar@1372 48 if (! new File(outDir).isDirectory()) {
sundar@1372 49 print(outDir + " directory does not exist!");
sundar@1372 50 exit(1);
sundar@1372 51 }
sundar@1372 52 }
sundar@1372 53
sundar@1372 54 var Files = Java.type("java.nio.file.Files");
sundar@1372 55 var FileSystems = Java.type("java.nio.file.FileSystems");
sundar@1372 56 var Paths = Java.type("java.nio.file.Paths");
sundar@1372 57
sundar@1372 58 var zipfile = Paths.get(arguments[0])
sundar@1372 59 var fs = FileSystems.newFileSystem(zipfile, null);
sundar@1372 60 var root = fs.rootDirectories[0];
sundar@1372 61
sundar@1372 62 // walk root and handle each Path
sundar@1372 63 Files.walk(root).forEach(
sundar@1372 64 function(p) {
sundar@1372 65 var outPath = outDir +
sundar@1372 66 p.toString().replace('/', File.separatorChar);
sundar@1372 67 print(outPath);
sundar@1372 68 if (Files.isDirectory(p)) {
sundar@1372 69 // create directories as needed
sundar@1372 70 new File(outPath).mkdirs();
sundar@1372 71 } else {
sundar@1372 72 // copy a 'file' resource
sundar@1372 73 Files.copy(p, new File(outPath).toPath());
sundar@1372 74 }
sundar@1372 75 }
sundar@1372 76 );
sundar@1372 77
sundar@1372 78 // done
sundar@1372 79 fs.close();

mercurial