• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / ro-crate-java / #373

28 Apr 2025 09:57AM UTC coverage: 86.891% (+0.9%) from 85.949%
#373

Pull #247

github

web-flow
Merge a4e0ad782 into 0ab3f3ca9
Pull Request #247: Generalize reading and writing crates

249 of 278 new or added lines in 16 files covered. (89.57%)

2 existing lines in 2 files now uncovered.

1889 of 2174 relevant lines covered (86.89%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.56
/src/main/java/edu/kit/datamanager/ro_crate/reader/ZipStrategy.java
1
package edu.kit.datamanager.ro_crate.reader;
2

3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import com.fasterxml.jackson.databind.node.ObjectNode;
5
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
6
import net.lingala.zip4j.ZipFile;
7
import org.apache.commons.io.FileUtils;
8

9
import java.io.File;
10
import java.io.IOException;
11
import java.nio.file.Path;
12
import java.util.UUID;
13

14
/**
15
 * A ReaderStrategy implementation which reads from ZipFiles.
16
 * <p>
17
 * May be used as a dependency for CrateReader. It will unzip
18
 * the ZipFile in a path relative to the directory this application runs in.
19
 * By default, it will be `./.tmp/ro-crate-java/zipReader/$UUID/`.
20
 * <p>
21
 * NOTE: The resulting crate may refer to these temporary files. Therefore,
22
 * these files are only being deleted before the JVM exits. If you need to free
23
 * space because your application is long-running or creates a lot of
24
 * crates, you may use the getters to retrieve information which will help
25
 * you to clean up manually. Keep in mind that crates may refer to this
26
 * folder after extraction. Use RoCrateWriter to export it so some
27
 * persistent location and possibly read it from there, if required. Or use
28
 * the ZipWriter to write it back to its source.
29
 */
30
public class ZipStrategy implements GenericReaderStrategy<String> {
31

32
  protected final String ID = UUID.randomUUID().toString();
1✔
33
  protected Path temporaryFolder = Path.of(String.format("./.tmp/ro-crate-java/zipReader/%s/", ID));
1✔
34
  protected boolean isExtracted = false;
1✔
35

36
  /**
37
   * Crates a ZipReader with the default configuration as described in the class documentation.
38
   */
39
  public ZipStrategy() {}
1✔
40

41
  /**
42
   * Creates a ZipReader which will extract the contents temporary
43
   * to the given location instead of the default location.
44
   *
45
   * @param folderPath            the custom directory to extract
46
   *                              content to for temporary access.
47
   * @param shallAddUuidSubfolder if true, the reader will extract
48
   *                              into subdirectories of the given
49
   *                              directory. These subdirectories
50
   *                              will have UUIDs as their names.
51
   */
52
  public ZipStrategy(Path folderPath, boolean shallAddUuidSubfolder) {
1✔
53
    if (shallAddUuidSubfolder) {
1✔
54
      this.temporaryFolder = folderPath.resolve(ID);
1✔
55
    } else {
56
      this.temporaryFolder = folderPath;
1✔
57
    }
58
  }
1✔
59

60
  /**
61
   * @return the identifier which may be used as the name for a subfolder in the temporary directory.
62
   */
63
  public String getID() {
64
    return ID;
1✔
65
  }
66

67
  /**
68
   * @return the folder (considered temporary) where the zipped crate will be or has been extracted to.
69
   */
70
  public Path getTemporaryFolder() {
71
    return temporaryFolder;
1✔
72
  }
73

74
  /**
75
   * @return whether the crate has already been extracted into the temporary folder.
76
   */
77
  public boolean isExtracted() {
78
    return isExtracted;
1✔
79
  }
80

81
  private void readCrate(String location) {
82
    try {
83
      File folder = temporaryFolder.toFile();
1✔
84
      // ensure the directory is clean
85
      if (folder.isDirectory()) {
1✔
86
        FileUtils.cleanDirectory(folder);
1✔
87
      } else if (folder.isFile()) {
1✔
NEW
88
        FileUtils.delete(folder);
×
89
      }
90
      // extract
91
      try (ZipFile zf = new ZipFile(location)) {
1✔
92
        zf.extractAll(temporaryFolder.toAbsolutePath().toString());
1✔
93
        this.isExtracted = true;
1✔
94
      }
95
      // register deletion on exit
96
      FileUtils.forceDeleteOnExit(folder);
1✔
NEW
97
    } catch (IOException e) {
×
NEW
98
      e.printStackTrace();
×
99
    }
1✔
100
  }
1✔
101

102
  @Override
103
  public ObjectNode readMetadataJson(String location) {
104
    if (!isExtracted) {
1✔
105
      this.readCrate(location);
1✔
106
    }
107

108
    ObjectMapper objectMapper = MyObjectMapper.getMapper();
1✔
109
    File jsonMetadata = temporaryFolder.resolve("ro-crate-metadata.json").toFile();
1✔
110
    
111
    try {
112
      return objectMapper.readTree(jsonMetadata).deepCopy();
1✔
NEW
113
    } catch (IOException e) {
×
NEW
114
      e.printStackTrace();
×
NEW
115
      return null;
×
116
    }
117
  }
118

119
  @Override
120
  public File readContent(String location) {
121
    if (!isExtracted) {
1✔
NEW
122
      this.readCrate(location);
×
123
    }
124
    return temporaryFolder.toFile();
1✔
125
  }
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc