21
21
* By default, it will be `./.tmp/ro-crate-java/zipReader/$UUID/`.
22
22
* <p>
23
23
* NOTE: The resulting crate may refer to these temporary files. Therefore,
24
- * these files are only being deleted before the JVM exits.
24
+ * these files are only being deleted before the JVM exits. If you need to free
25
+ * space because your application is long-running or creates a lot of
26
+ * crates, you may use the getters to retrieve information which will help
27
+ * you to clean up manually. Keep in mind that crates may refer to this
28
+ * folder after extraction. Use RoCrateWriter to export it so some
29
+ * persistent location and possibly read it from there, if required. Or use
30
+ * the ZipWriter to write it back to its source.
25
31
*/
26
32
public class ZipReader implements ReaderStrategy {
27
33
28
34
protected final String ID = UUID .randomUUID ().toString ();
29
- protected Path tempFolder = Path .of (String .format ("./.tmp/ro-crate-java/zipReader/%s/" , ID ));
30
- protected boolean read = false ;
35
+ protected Path temporaryFolder = Path .of (String .format ("./.tmp/ro-crate-java/zipReader/%s/" , ID ));
36
+ protected boolean isExtracted = false ;
31
37
38
+ /**
39
+ * Crates a ZipReader with the default configuration as described in the class documentation.
40
+ */
32
41
public ZipReader () {}
33
42
34
43
/**
@@ -44,15 +53,36 @@ public ZipReader() {}
44
53
*/
45
54
public ZipReader (Path folderPath , boolean shallAddUuidSubfolder ) {
46
55
if (shallAddUuidSubfolder ) {
47
- this .tempFolder = folderPath .resolve (ID );
56
+ this .temporaryFolder = folderPath .resolve (ID );
48
57
} else {
49
- this .tempFolder = folderPath ;
58
+ this .temporaryFolder = folderPath ;
50
59
}
51
60
}
52
61
62
+ /**
63
+ * @return the identifier which may be used as the name for a subfolder in the temporary directory.
64
+ */
65
+ public String getID () {
66
+ return ID ;
67
+ }
68
+
69
+ /**
70
+ * @return the folder (considered temporary) where the zipped crate will be or has been extracted to.
71
+ */
72
+ public Path getTemporaryFolder () {
73
+ return temporaryFolder ;
74
+ }
75
+
76
+ /**
77
+ * @return whether the crate has already been extracted into the temporary folder.
78
+ */
79
+ public boolean isExtracted () {
80
+ return isExtracted ;
81
+ }
82
+
53
83
private void readCrate (String location ) {
54
84
try {
55
- File folder = tempFolder .toFile ();
85
+ File folder = temporaryFolder .toFile ();
56
86
// ensure the directory is clean
57
87
if (folder .isDirectory ()) {
58
88
FileUtils .cleanDirectory (folder );
@@ -61,8 +91,8 @@ private void readCrate(String location) {
61
91
}
62
92
// extract
63
93
try (ZipFile zf = new ZipFile (location )) {
64
- zf .extractAll (tempFolder .toAbsolutePath ().toString ());
65
- this .read = true ;
94
+ zf .extractAll (temporaryFolder .toAbsolutePath ().toString ());
95
+ this .isExtracted = true ;
66
96
}
67
97
// register deletion on exit
68
98
FileUtils .forceDeleteOnExit (folder );
@@ -73,12 +103,12 @@ private void readCrate(String location) {
73
103
74
104
@ Override
75
105
public ObjectNode readMetadataJson (String location ) {
76
- if (!read ) {
106
+ if (!isExtracted ) {
77
107
this .readCrate (location );
78
108
}
79
109
80
110
ObjectMapper objectMapper = MyObjectMapper .getMapper ();
81
- File jsonMetadata = tempFolder .resolve ("ro-crate-metadata.json" ).toFile ();
111
+ File jsonMetadata = temporaryFolder .resolve ("ro-crate-metadata.json" ).toFile ();
82
112
83
113
try {
84
114
return objectMapper .readTree (jsonMetadata ).deepCopy ();
@@ -90,9 +120,9 @@ public ObjectNode readMetadataJson(String location) {
90
120
91
121
@ Override
92
122
public File readContent (String location ) {
93
- if (!read ) {
123
+ if (!isExtracted ) {
94
124
this .readCrate (location );
95
125
}
96
- return tempFolder .toFile ();
126
+ return temporaryFolder .toFile ();
97
127
}
98
128
}
0 commit comments