Answer the question
In order to leave comments, you need to log in
How to use ZipFileSystem in funcA if generated in funcB?
Hello, I'm using FileSystem to manage an archive that stores everything necessary for the application to work. Archive - a database with which the user works.
public class Manager {
private FileSystem storage = null;
public String createStorage(String path, String name) {
try {
InputStream in = Manager.class.getResourceAsStream("storage.bmd");
File fileDestination = new File(path + "/" + name + ".bmd");
if (Files.notExists(fileDestination.toPath())) Files.copy(in, fileDestination.toPath());
return fileDestination.toString();
} catch (IOException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public boolean selectStorage(String path) {
Map<String,String> env = new HashMap();
env.put("create","true");
URI uri = URI.create("jar:file:" + path);
try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
storage = fs;
return true;
} catch (IOException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
public boolean createCatalog(String path) {
if (storage == null) return false;
Path dest = storage.getPath(path);
System.out.println(dest);
if (Files.notExists(dest)) {
try {
Files.createDirectories(dest);
return true;
} catch (IOException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
}
return false;
}
}
Answer the question
In order to leave comments, you need to log in
Hey! storage = fs; you are copying the link that will close after the selectStorage method is executed, because there is a try-with-resources block, manage its life cycle yourself something like this
try {
storage = FileSystems. newFileSystem (uri, env);
return true;
} catch (IOException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
+ make a public method that can be closed from outside
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question