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

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

13 Sep 2024 01:26PM UTC coverage: 90.688% (+0.001%) from 90.687%
#258

Pull #83

github

web-flow
Merge 0c05b218c into 5681afb22
Pull Request #83: Unpack zipped crates into unique temporary folder

24 of 25 new or added lines in 1 file covered. (96.0%)

1 existing line in 1 file now uncovered.

1607 of 1772 relevant lines covered (90.69%)

0.91 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/ZipReader.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

6
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
7

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

13
import net.lingala.zip4j.ZipFile;
14
import org.apache.commons.io.FileUtils;
15

16
/**
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. 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.
31
 */
32
public class ZipReader implements ReaderStrategy {
33

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

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

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

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

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

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

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

121
  @Override
122
  public File readContent(String location) {
123
    if (!isExtracted) {
1✔
124
      this.readCrate(location);
×
125
    }
126
    return temporaryFolder.toFile();
1✔
127
  }
128
}
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