8
8
import java .io .File ;
9
9
import java .io .IOException ;
10
10
import java .util .UUID ;
11
+ import java .nio .file .Path ;
11
12
12
13
import net .lingala .zip4j .ZipFile ;
13
14
import org .apache .commons .io .FileUtils ;
14
15
15
16
/**
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.
18
25
*/
19
26
public class ZipReader implements ReaderStrategy {
20
27
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 ;
22
31
23
- private String tempFolder = "./temp/" + uuid + "/" ;
32
+ public ZipReader () {}
24
33
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
+ }
26
52
27
53
private void readCrate (String location ) {
28
54
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 );
32
61
}
62
+ // extract
33
63
try (ZipFile zf = new ZipFile (location )) {
34
- zf .extractAll (tempFolder );
64
+ zf .extractAll (tempFolder .toAbsolutePath ().toString ());
65
+ this .read = true ;
35
66
}
36
- FileUtils . forceDeleteOnExit ( new File ( tempFolder ));
37
- this . read = true ;
67
+ // register deletion on exit
68
+ FileUtils . forceDeleteOnExit ( folder ) ;
38
69
} catch (IOException e ) {
39
70
e .printStackTrace ();
40
71
}
@@ -47,7 +78,7 @@ public ObjectNode readMetadataJson(String location) {
47
78
}
48
79
49
80
ObjectMapper objectMapper = MyObjectMapper .getMapper ();
50
- File jsonMetadata = new File ( tempFolder + "ro-crate-metadata.json" );
81
+ File jsonMetadata = tempFolder . resolve ( "ro-crate-metadata.json" ). toFile ( );
51
82
52
83
try {
53
84
return objectMapper .readTree (jsonMetadata ).deepCopy ();
@@ -62,6 +93,6 @@ public File readContent(String location) {
62
93
if (!read ) {
63
94
this .readCrate (location );
64
95
}
65
- return new File ( tempFolder );
96
+ return tempFolder . toFile ( );
66
97
}
67
98
}
0 commit comments