Example usage for java.nio.file FileSystems newFileSystem

List of usage examples for java.nio.file FileSystems newFileSystem

Introduction

In this page you can find the example usage for java.nio.file FileSystems newFileSystem.

Prototype

public static FileSystem newFileSystem(Path path, Map<String, ?> env) throws IOException 

Source Link

Document

Constructs a new FileSystem to access the contents of a file as a file system.

Usage

From source file:Test.java

public static void main(String[] args) throws Exception {
    Map<String, String> attributes = new HashMap<>();
    attributes.put("create", "true");
    URI zipFile = URI.create("jar:file:/home.zip");
    try (FileSystem zipFileSys = FileSystems.newFileSystem(zipFile, attributes);) {
        Path path = zipFileSys.getPath("docs");
        Files.createDirectory(path);
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(zipFileSys.getPath("/"));) {
            for (Path file : directoryStream) {
                System.out.println(file.getFileName());
            }/*from   w  ww . j  av a 2  s  .c om*/
        }
    }
}

From source file:Main.java

private static FileSystem createZipFileSystem(String zipFilename, boolean create) throws IOException {
    final Path path = Paths.get(zipFilename);
    final URI uri = URI.create("jar:file:" + path.toUri().getPath());

    final Map<String, String> env = new HashMap<>();
    if (create) {
        env.put("create", "true");
    }//from   w w  w  .  ja va 2s  .c o  m
    return FileSystems.newFileSystem(uri, env);
}

From source file:org.apdplat.superword.tools.JavaCodeAnalyzer.java

public static Map<String, AtomicInteger> parseZip(String zipFile) {
    LOGGER.info("?ZIP" + zipFile);
    Map<String, AtomicInteger> data = new HashMap<>();
    try (FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile),
            JavaCodeAnalyzer.class.getClassLoader())) {
        for (Path path : fs.getRootDirectories()) {
            LOGGER.info("?" + path);
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                @Override//ww w . j  a v  a 2  s . co  m
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    LOGGER.info("?" + file);
                    // ?
                    Path temp = Paths.get("target/java-source-code.txt");
                    Files.copy(file, temp, StandardCopyOption.REPLACE_EXISTING);
                    Map<String, AtomicInteger> r = parseFile(temp.toFile().getAbsolutePath());
                    r.keySet().forEach(k -> {
                        data.putIfAbsent(k, new AtomicInteger());
                        data.get(k).addAndGet(r.get(k).get());
                    });
                    r.clear();
                    return FileVisitResult.CONTINUE;
                }

            });
        }
    } catch (Exception e) {
        LOGGER.error("?ZIP", e);
    }
    return data;
}

From source file:net.formicary.remoterun.common.test.FileStreamerTest.java

@Test
public void testDirectory() throws URISyntaxException, IOException {
    try (FileSystem zipfs = FileSystems
            .newFileSystem(Paths.get(getClass().getResource("/example_directory.zip").toURI()), null);
            FileReceiver fileReceiver = new FileReceiver(tempDirectory)) {
        new Thread(fileReceiver).start();
        new FileStreamer(zipfs.getPath("/example"), new MyFileStreamerCallback(fileReceiver)).run();
        fileReceiver.waitUntilFinishedUninterruptably();
        Assert.assertTrue(fileReceiver.success(), fileReceiver.getFailureMessage());
    }//w w w .jav  a  2 s  . co m
}

From source file:net.formicary.remoterun.common.test.StreamToFileReceiver.java

@Test
public void testDirectory() throws URISyntaxException, IOException {
    try (FileSystem zipfs = FileSystems
            .newFileSystem(Paths.get(getClass().getResource("/example_directory.zip").toURI()), null);
            FileReceiver fileReceiver = new FileReceiver(tempDirectory)) {
        new Thread(fileReceiver).start();
        new FileStreamer(zipfs.getPath("/example"), new MyFileStreamerCallback(fileReceiver)).run();
        fileReceiver.waitUntilFinishedUninterruptably();
        Assert.assertTrue(fileReceiver.success(), fileReceiver.getFailureMessage());
    }//from   www .j a  va2  s .  c o  m
    Assert.assertTrue(Files.isDirectory(tempDirectory.resolve("example")));
    Assert.assertTrue(Files.isDirectory(tempDirectory.resolve("example/test/.git")));
    Assert.assertTrue(Files.isRegularFile(tempDirectory.resolve("example/test/hello.txt")));
}

From source file:notaql.performance.PerformanceTest.java

public static void runTests() throws URISyntaxException, IOException {
    URI basePath = NotaQL.class.getResource(BASE_PATH).toURI();

    Map<String, String> env = new HashMap<>();
    env.put("create", "true");
    FileSystem zipfs = FileSystems.newFileSystem(basePath, env);

    final List<Path> tests = Files.list(Paths.get(basePath)).filter(p -> p.toString().endsWith(".json"))
            .sorted((a, b) -> a.toString().compareTo(b.toString())).collect(Collectors.toList());

    List<String> csv = new LinkedList<>();

    for (Path test : tests) {
        if (testCase != null && !test.getFileName().toString().equals(testCase))
            continue;

        final List<JSONObject> transformations = readArray(test);

        csv.add(test.getFileName().toString());

        int i = 1;

        for (JSONObject transformation : transformations) {
            final String name = transformation.getString("name");
            final String notaqlTransformation = composeTransformation(transformation);

            if (!(inEngine == null && outEngine == null
                    || inEngine != null//from   w w  w  .j  a v  a  2  s.  c o m
                            && transformation.getJSONObject("IN-ENGINE").getString("engine").equals(inEngine)
                    || outEngine != null && transformation.getJSONObject("OUT-ENGINE").getString("engine")
                            .equals(outEngine)))
                continue;

            System.out.println("Evaluation test (" + i++ + "/" + transformations.size() + "): "
                    + test.getFileName() + ": " + name);

            List<Duration> durations = new LinkedList<>();

            for (int j = 0; j < RUNS; j++) {
                clean(transformation);

                final Instant startTime = Instant.now();

                NotaQL.evaluate(notaqlTransformation);

                final Instant endTime = Instant.now();

                final Duration duration = Duration.between(startTime, endTime);

                durations.add(duration);

                System.out.println("Testrun(" + j + ") " + name + " took: " + duration);
            }

            System.out.println("!!=================================================================!!");

            System.out.println("Test " + name + " took: "
                    + durations.stream().map(Duration::toString).collect(Collectors.joining(", ")));

            System.out.println("Test " + name + " took millis: " + durations.stream()
                    .map(d -> Long.toString(d.toMillis())).collect(Collectors.joining(", ")));

            System.out.println("Test " + name + " took on average (millis): "
                    + durations.stream().mapToLong(Duration::toMillis).average());

            System.out.println("Test " + name + " took on average (ignoring first - millis): "
                    + durations.stream().skip(1).mapToLong(Duration::toMillis).average());

            System.out.println("!!=================================================================!!");

            csv.add(name);
            csv.add(durations.stream().map(d -> Long.toString(d.toMillis())).collect(Collectors.joining(",")));
        }

    }

    System.out.println(csv.stream().collect(Collectors.joining("\n")));
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemProviderIntegrationTest.java

@Test
public void testGetFsViaNioApi() throws IOException {
    URI fsUri = CryptoFileSystemUris.createUri(tmpPath);
    FileSystem fs = FileSystems.newFileSystem(fsUri,
            cryptoFileSystemProperties().withPassphrase("asd").build());
    Assert.assertTrue(fs instanceof CryptoFileSystemImpl);
    Assert.assertTrue(Files.exists(tmpPath.resolve("masterkey.cryptomator")));
    FileSystem fs2 = FileSystems.getFileSystem(fsUri);
    Assert.assertSame(fs, fs2);/*from w  w w . j  a va  2  s.  co  m*/
}

From source file:org.apdplat.superword.extract.PhraseExtractor.java

public static Set<String> parseZip(String zipFile) {
    Set<String> data = new HashSet<>();
    LOGGER.info("?ZIP" + zipFile);
    try (FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile),
            PhraseExtractor.class.getClassLoader())) {
        for (Path path : fs.getRootDirectories()) {
            LOGGER.info("?" + path);
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                @Override//from w ww  .  ja  v  a2 s . c  om
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    LOGGER.info("?" + file);
                    // ?
                    Path temp = Paths.get("target/origin-html-temp.txt");
                    Files.copy(file, temp, StandardCopyOption.REPLACE_EXISTING);
                    data.addAll(parseFile(temp.toFile().getAbsolutePath()));
                    return FileVisitResult.CONTINUE;
                }

            });
        }
    } catch (Exception e) {
        LOGGER.error("?", e);
    }
    return data;
}

From source file:org.ballerinalang.repository.fs.ClasspathPackageRepository.java

private static void initFS(URI uri) throws IOException {
    if (JAR_URI_SCHEME.equals(uri.getScheme())) {
        Map<String, String> env = new HashMap<>();
        env.put("create", "true");
        try {/*from   w w w.  ja v a2 s.com*/
            FileSystems.newFileSystem(uri, env);
        } catch (FileSystemAlreadyExistsException ignore) {
        }
    }
}

From source file:org.apdplat.superword.extract.DefinitionExtractor.java

public static Set<Word> parseZip(String zipFile) {
    Set<Word> data = new HashSet<>();
    LOGGER.info("?ZIP" + zipFile);
    try (FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile), WordClassifier.class.getClassLoader())) {
        for (Path path : fs.getRootDirectories()) {
            LOGGER.info("?" + path);
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                @Override//w w w .  j  a v a 2  s  .co m
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    LOGGER.info("?" + file);
                    // ?
                    Path temp = Paths.get("target/origin-html-temp.txt");
                    Files.copy(file, temp, StandardCopyOption.REPLACE_EXISTING);
                    data.addAll(parseFile(temp.toFile().getAbsolutePath()));
                    return FileVisitResult.CONTINUE;
                }

            });
        }
    } catch (Exception e) {
        LOGGER.error("?", e);
    }
    return data;
}