-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ZipResourceAccessor.java
71 lines (59 loc) · 2.33 KB
/
ZipResourceAccessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package liquibase.resource;
import liquibase.Scope;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.nio.file.*;
import java.util.Collections;
import java.util.List;
public class ZipResourceAccessor extends AbstractPathResourceAccessor {
private FileSystem fileSystem;
/**
* Creates a FileSystemResourceAccessor with the given directories/files as the roots.
*/
public ZipResourceAccessor(File file) throws FileNotFoundException {
this(file.toPath());
}
public ZipResourceAccessor(Path file) throws FileNotFoundException {
if (file == null) {
throw new IllegalArgumentException("File must not be null");
}
Scope.getCurrentScope().getLog(getClass()).fine("Creating resourceAccessor for file " + file);
if (!Files.exists(file)) {
throw new FileNotFoundException("Non-existent file: " + file.toAbsolutePath());
}
if (!Files.isRegularFile(file)) {
throw new IllegalArgumentException("Not a regular file: " + file.toAbsolutePath());
}
String lowerCaseName = file.toString().toLowerCase();
if (!(lowerCaseName.endsWith(".jar") || lowerCaseName.endsWith(".zip"))) {
throw new IllegalArgumentException("Not a jar or zip file: " + file.toAbsolutePath());
}
URI finalUri = URI.create("jar:" + file.toUri());
try {
try {
this.fileSystem = FileSystems.getFileSystem(finalUri);
} catch (FileSystemNotFoundException e) {
this.fileSystem = FileSystems.newFileSystem(finalUri, Collections.emptyMap(), Scope.getCurrentScope().getClassLoader());
}
} catch (Throwable e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
@Override
public void close() throws Exception {
//can't close the filesystem because they often get reused and/or are being used by other things
}
@Override
protected Path getRootPath() {
return this.fileSystem.getPath("/");
}
@Override
protected Resource createResource(Path file, String pathToAdd) {
return new PathResource(pathToAdd, file);
}
@Override
public List<String> describeLocations() {
return Collections.singletonList(fileSystem.toString());
}
}