Blame openjdk/nashorn/samples/unzip.js

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