Skip to content

Commit 4a2aa9b

Browse files
committed
feat: add getters for information useful for manual removal of temporary files.
1 parent 3314b74 commit 4a2aa9b

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

src/main/java/edu/kit/datamanager/ro_crate/reader/ZipReader.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@
2121
* By default, it will be `./.tmp/ro-crate-java/zipReader/$UUID/`.
2222
* <p>
2323
* 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.
2531
*/
2632
public class ZipReader implements ReaderStrategy {
2733

2834
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;
3137

38+
/**
39+
* Crates a ZipReader with the default configuration as described in the class documentation.
40+
*/
3241
public ZipReader() {}
3342

3443
/**
@@ -44,15 +53,36 @@ public ZipReader() {}
4453
*/
4554
public ZipReader(Path folderPath, boolean shallAddUuidSubfolder) {
4655
if (shallAddUuidSubfolder) {
47-
this.tempFolder = folderPath.resolve(ID);
56+
this.temporaryFolder = folderPath.resolve(ID);
4857
} else {
49-
this.tempFolder = folderPath;
58+
this.temporaryFolder = folderPath;
5059
}
5160
}
5261

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+
5383
private void readCrate(String location) {
5484
try {
55-
File folder = tempFolder.toFile();
85+
File folder = temporaryFolder.toFile();
5686
// ensure the directory is clean
5787
if (folder.isDirectory()) {
5888
FileUtils.cleanDirectory(folder);
@@ -61,8 +91,8 @@ private void readCrate(String location) {
6191
}
6292
// extract
6393
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;
6696
}
6797
// register deletion on exit
6898
FileUtils.forceDeleteOnExit(folder);
@@ -73,12 +103,12 @@ private void readCrate(String location) {
73103

74104
@Override
75105
public ObjectNode readMetadataJson(String location) {
76-
if (!read) {
106+
if (!isExtracted) {
77107
this.readCrate(location);
78108
}
79109

80110
ObjectMapper objectMapper = MyObjectMapper.getMapper();
81-
File jsonMetadata = tempFolder.resolve("ro-crate-metadata.json").toFile();
111+
File jsonMetadata = temporaryFolder.resolve("ro-crate-metadata.json").toFile();
82112

83113
try {
84114
return objectMapper.readTree(jsonMetadata).deepCopy();
@@ -90,9 +120,9 @@ public ObjectNode readMetadataJson(String location) {
90120

91121
@Override
92122
public File readContent(String location) {
93-
if (!read) {
123+
if (!isExtracted) {
94124
this.readCrate(location);
95125
}
96-
return tempFolder.toFile();
126+
return temporaryFolder.toFile();
97127
}
98128
}

0 commit comments

Comments
 (0)