Skip to content

Commit 3314b74

Browse files
committed
feat: better (customizable) default path for ZipReader
1 parent f6c0b48 commit 3314b74

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

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

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,64 @@
88
import java.io.File;
99
import java.io.IOException;
1010
import java.util.UUID;
11+
import java.nio.file.Path;
1112

1213
import net.lingala.zip4j.ZipFile;
1314
import org.apache.commons.io.FileUtils;
1415

1516
/**
16-
* Implementation of the reader strategy, providing a way of reading crates from
17-
* a zip archive.
17+
* A ReaderStrategy implementation which reads from ZipFiles.
18+
* <p>
19+
* May be used as a dependency for RoCrateReader. It will unzip
20+
* the ZipFile in a path relative to the directory this application runs in.
21+
* By default, it will be `./.tmp/ro-crate-java/zipReader/$UUID/`.
22+
* <p>
23+
* NOTE: The resulting crate may refer to these temporary files. Therefore,
24+
* these files are only being deleted before the JVM exits.
1825
*/
1926
public class ZipReader implements ReaderStrategy {
2027

21-
private String uuid = UUID.randomUUID().toString();
28+
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;
2231

23-
private String tempFolder = "./temp/" + uuid + "/";
32+
public ZipReader() {}
2433

25-
private boolean read = false;
34+
/**
35+
* Creates a ZipReader which will extract the contents temporary
36+
* to the given location instead of the default location.
37+
*
38+
* @param folderPath the custom directory to extract
39+
* content to for temporary access.
40+
* @param shallAddUuidSubfolder if true, the reader will extract
41+
* into subdirectories of the given
42+
* directory. These subdirectories
43+
* will have UUIDs as their names.
44+
*/
45+
public ZipReader(Path folderPath, boolean shallAddUuidSubfolder) {
46+
if (shallAddUuidSubfolder) {
47+
this.tempFolder = folderPath.resolve(ID);
48+
} else {
49+
this.tempFolder = folderPath;
50+
}
51+
}
2652

2753
private void readCrate(String location) {
2854
try {
29-
File temp = new File(tempFolder);
30-
if (temp.exists()) {
31-
FileUtils.cleanDirectory(new File(tempFolder));
55+
File folder = tempFolder.toFile();
56+
// ensure the directory is clean
57+
if (folder.isDirectory()) {
58+
FileUtils.cleanDirectory(folder);
59+
} else if (folder.isFile()) {
60+
FileUtils.delete(folder);
3261
}
62+
// extract
3363
try (ZipFile zf = new ZipFile(location)) {
34-
zf.extractAll(tempFolder);
64+
zf.extractAll(tempFolder.toAbsolutePath().toString());
65+
this.read = true;
3566
}
36-
FileUtils.forceDeleteOnExit(new File(tempFolder));
37-
this.read = true;
67+
// register deletion on exit
68+
FileUtils.forceDeleteOnExit(folder);
3869
} catch (IOException e) {
3970
e.printStackTrace();
4071
}
@@ -47,7 +78,7 @@ public ObjectNode readMetadataJson(String location) {
4778
}
4879

4980
ObjectMapper objectMapper = MyObjectMapper.getMapper();
50-
File jsonMetadata = new File(tempFolder + "ro-crate-metadata.json");
81+
File jsonMetadata = tempFolder.resolve("ro-crate-metadata.json").toFile();
5182

5283
try {
5384
return objectMapper.readTree(jsonMetadata).deepCopy();
@@ -62,6 +93,6 @@ public File readContent(String location) {
6293
if (!read) {
6394
this.readCrate(location);
6495
}
65-
return new File(tempFolder);
96+
return tempFolder.toFile();
6697
}
6798
}

0 commit comments

Comments
 (0)