private static FileSystem getFileSystem(URI uri) throws IOException { try { return FileSystems.getFileSystem(uri); } catch (FileSystemNotFoundException e) { return FileSystems.newFileSystem(uri, Collections.<String, String>emptyMap()); } }
private void initFileSystem(URI uri) throws IOException { try { FileSystems.getFileSystem(uri); } catch (FileSystemNotFoundException e) { FileSystems.newFileSystem(uri, Collections.emptyMap()); } catch (IllegalArgumentException e) { FileSystems.getDefault(); } }
private static void updateZip(File zip, File entry, String entryName) throws IOException { Map<String, String> env = new HashMap<>(); String uriPath = "jar:" + zip.toURI().toString(); URI uri = URI.create(uriPath); try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { fs.provider().checkAccess(fs.getPath(entryName), AccessMode.READ); Path target = fs.getPath(entryName); Path source = entry.toPath(); Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw e; } }
@Override public FileSystem load(@Nonnull URI key) throws Exception { try { return FileSystems.getFileSystem(key); } catch (FileSystemNotFoundException e) { try { return FileSystems.newFileSystem(key, Collections.emptyMap()); } catch (FileSystemAlreadyExistsException f) { return FileSystems.getFileSystem(key); } } } });
public JrtfsCodeBase(ICodeBaseLocator codeBaseLocator, @Nonnull String fileName) { super(codeBaseLocator); this.fileName = fileName; URL url; try { url = Paths.get(fileName).toUri().toURL(); URLClassLoader loader = new URLClassLoader(new URL[] { url }); fs = FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap(), loader); root = fs.getPath("modules"); packageToModuleMap = createPackageToModuleMap(fs); } catch (IOException e) { e.printStackTrace(); } }
private ImmutableList<ProfileFileElement> loadProfileFiles(Multimap<Path, String> pathsToAttempt) throws IOException { ImmutableList.Builder<ProfileFileElement> result = ImmutableList.builder(); try (Closer closer = Closer.create()) { for (Map.Entry<Path, Collection<String>> entry : pathsToAttempt.asMap().entrySet()) { Path base = entry.getKey(); if (Files.isRegularFile(base)) { FileSystem sourceFs = FileSystems.newFileSystem(base, getClass().getClassLoader()); closer.register(sourceFs); base = getOnlyElement(sourceFs.getRootDirectories()); } for (String path : entry.getValue()) { ProfileFileElement element = loadProfileFile(base, path); if (element != null) result.add(element); } } } return result.build(); }
public Schema load() throws IOException { if (sources.isEmpty()) { throw new IllegalStateException("No sources added."); } try (Closer closer = Closer.create()) { // Map the physical path to the file system root. For regular directories the key and the // value are equal. For ZIP files the key is the path to the .zip, and the value is the root // of the file system within it. Map<Path, Path> directories = new LinkedHashMap<>(); for (Path source : sources) { if (Files.isRegularFile(source)) { FileSystem sourceFs = FileSystems.newFileSystem(source, getClass().getClassLoader()); closer.register(sourceFs); directories.put(source, getOnlyElement(sourceFs.getRootDirectories())); } else { directories.put(source, source); } } return loadFromDirectories(directories); } }
/** Returns a reference-counted Jar FileSystem, possibly one that was previously returned. */ private static FileSystem getJarFs(Path jarFile) throws IOException { Path key = jarFile.toAbsolutePath(); synchronized (ZIP_FILESYSTEMS) { FsWrapper fs = ZIP_FILESYSTEMS.get(key); if (fs == null) { fs = new FsWrapper(FileSystems.newFileSystem(key, null), key); fs.incrRefCount(); ZIP_FILESYSTEMS.put(key, fs); } else { fs.incrRefCount(); } return fs; } }
@Override public FileSystem newFileSystem(Path path, Map<String, ?> env) throws IOException { JimfsPath checkedPath = checkPath(path); checkNotNull(env); URI pathUri = checkedPath.toUri(); URI jarUri = URI.create("jar:" + pathUri); try { // pass the new jar:jimfs://... URI to be handled by ZipFileSystemProvider return FileSystems.newFileSystem(jarUri, env); } catch (Exception e) { // if any exception occurred, assume the file wasn't a zip file and that we don't support // viewing it as a file system throw new UnsupportedOperationException(e); } }
protected byte[] loadChunkZip(Vector3i chunkPos) { byte[] chunkData = null; Vector3i chunkZipPos = storagePathProvider.getChunkZipPosition(chunkPos); Path chunkPath = storagePathProvider.getChunkZipPath(chunkZipPos); if (Files.isRegularFile(chunkPath)) { try (FileSystem chunkZip = FileSystems.newFileSystem(chunkPath, null)) { Path targetChunk = chunkZip.getPath(storagePathProvider.getChunkFilename(chunkPos)); if (Files.isRegularFile(targetChunk)) { chunkData = Files.readAllBytes(targetChunk); } } catch (IOException e) { logger.error("Failed to load chunk zip {}", chunkPath, e); } } return chunkData; }
URI archiveAbsoluteURI = URI.create( "jar:file:" + destinationZip.toURI().getRawPath() ); try ( FileSystem zipFs = FileSystems.newFileSystem( archiveAbsoluteURI, env ) )
@SuppressWarnings("ConstantConditions") public static Path getResourceRoot() throws IOException, URISyntaxException { final URI uri = GeneratorTest.class.getClassLoader().getResource("product.avro").toURI(); if ("jar".equals(uri.getScheme())) { final FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap(), null); return fileSystem.getPath("path/to/folder/inside/jar").getParent(); } else { return Paths.get(uri).getParent(); } } }
static void createJar(final URI jarURI, final File workDir, final File f) throws Exception { final Map<String, String> env = new HashMap<>(); env.put("create", "true"); final URI uri = URI.create("jar:file://" + jarURI.getRawPath()); try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) { final Path path = zipfs.getPath(workDir.toPath().relativize(f.toPath()).toString()); if (path.getParent() != null) { Files.createDirectories(path.getParent()); } Files.copy(f.toPath(), path, StandardCopyOption.REPLACE_EXISTING ); } }
private void addJarManifest(String uberJar, MavenProject project, String mainClass) throws IOException { Path path = Paths.get(uberJar); URI uri = URI.create("jar:" + path.toUri()); String user = System.getProperty("user.name"); String manifestContent = IO.load("manifest-template.mf") .replace("$user", user) .replace("$java", Msc.javaVersion()) .replace("$name", project.getName()) .replace("$version", project.getVersion()) .replace("$groupId", project.getGroupId()) .replace("$organization", project.getOrganization() != null ? U.or(project.getOrganization().getName(), "?") : "?") .replace("$url", U.or(project.getUrl(), "?")) .replace("$main", U.safe(mainClass)); try (FileSystem fs = FileSystems.newFileSystem(uri, U.map())) { Path manifest = fs.getPath("META-INF/MANIFEST.MF"); try (Writer writer = Files.newBufferedWriter(manifest, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) { writer.write(manifestContent); } } }
@BeforeClass public void setUp() throws URISyntaxException, ConfigStoreCreationException, IOException { Path path = Paths.get(this.getClass().getClassLoader().getResource("zipStoreTest.zip").getPath()); FileSystem fs = FileSystems.newFileSystem(path, null); this.store = new ZipFileConfigStore((ZipFileSystem) fs, path.toUri(), this.version, "_CONFIG_STORE"); }
private static void verifyContent( Path destination ) throws IOException { URI uri = URI.create("jar:file:" + destination.toAbsolutePath().toUri().getRawPath() ); try ( FileSystem fs = FileSystems.newFileSystem( uri, Collections.emptyMap() ) ) { List<String> fileA = Files.readAllLines( fs.getPath( "logs/a.txt" ) ); assertEquals( 1, fileA.size() ); assertEquals( "file a", fileA.get( 0 ) ); List<String> fileB = Files.readAllLines( fs.getPath( "logs/b.txt" ) ); assertEquals( 1, fileB.size() ); assertEquals( "file b", fileB.get( 0 ) ); } }
@Test public void testNewFileSystem() throws Exception { Map<String, String> env = new HashMap<>(); FileSystems.newFileSystem(URI.create("gs://bucket/path/to/file"), env); }
private Library loadLibrary(final URI lib) { return Try.apply(() -> FileSystems.newFileSystem(lib, Maps.newHashMap())) .map(it -> Try.apply(() -> new Library(it)).get()) .recover(x -> Try.apply(() -> new Library(Paths.get(lib))).get()) .get(); }
try ( FileSystem fs = FileSystems.newFileSystem( uri, Collections.emptyMap() ) )
final URI uri = URI.create( "jar:file:" + report.toUri().getRawPath() ); try ( FileSystem fs = FileSystems.newFileSystem( uri, Collections.emptyMap() ) )