S
S
swcalc2018-09-21 13:29:07
Java
swcalc, 2018-09-21 13:29:07

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;
    }
    
}

Above is a class that describes the main methods and would like to separate the creation of a directory and initialization of the store. But the problem is that if I save the generated FileSystem to a global variable, then either I get an InvocationTargetException (the code is given above), if I fix it on storage = FileSystems.newFileSystem();, then there are no errors, but later I cannot create a folder or file.
If you create a directory immediately after generating the FileSystem, then everything is OK.
How can I generate it once and then use it in other methods???
// EDIT
All due to the fact that the FileSystem is closed, after the selectStorage method is executed, then the object saves only read-only properties, the isOpen() method returns false, I could not find any information on how to open it =(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
drKred, 2018-09-21
@drKred

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 question

Ask a Question

731 491 924 answers to any question