• 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

93.33
/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStrategy.java
1
package edu.kit.datamanager.ro_crate.writer;
2

3
import com.fasterxml.jackson.databind.JsonNode;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import edu.kit.datamanager.ro_crate.Crate;
6
import edu.kit.datamanager.ro_crate.entities.data.DataEntity;
7
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
8
import net.lingala.zip4j.ZipFile;
9
import net.lingala.zip4j.exception.ZipException;
10
import net.lingala.zip4j.model.ZipParameters;
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13

14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
import java.nio.charset.StandardCharsets;
18

19
/**
20
 * Implementation of the writing strategy to provide a way of writing crates to
21
 * a zip archive.
22
 */
23
public class ZipStrategy implements GenericWriterStrategy<String> {
1✔
24

25
    private static final Logger logger = LoggerFactory.getLogger(ZipStrategy.class);
1✔
26

27
    @Override
28
    public void save(Crate crate, String destination) {
29
        try (ZipFile zipFile = new ZipFile(destination)) {
1✔
30
            saveMetadataJson(crate, zipFile);
1✔
31
            saveDataEntities(crate, zipFile);
1✔
NEW
32
        } catch (IOException e) {
×
33
            // can not close ZipFile (threw Exception)
NEW
34
            logger.error("Failed to write ro-crate to destination " + destination + ".", e);
×
35
        }
1✔
36
    }
1✔
37

38
    private void saveDataEntities(Crate crate, ZipFile zipFile) {
39
        for (DataEntity dataEntity : crate.getAllDataEntities()) {
1✔
40
            try {
41
                dataEntity.saveToZip(zipFile);
1✔
42
            } catch (ZipException e) {
1✔
43
                logger.error("Could not save " + dataEntity.getId() + " to zip file!", e);
1✔
44
            }
1✔
45
        }
1✔
46
    }
1✔
47

48
    private void saveMetadataJson(Crate crate, ZipFile zipFile) {
49
        try {
50
            // write the metadata.json file
51
            ZipParameters zipParameters = new ZipParameters();
1✔
52
            zipParameters.setFileNameInZip("ro-crate-metadata.json");
1✔
53
            ObjectMapper objectMapper = MyObjectMapper.getMapper();
1✔
54
            // we create an JsonNode only to have the file written pretty
55
            JsonNode node = objectMapper.readTree(crate.getJsonMetadata());
1✔
56
            String str = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
1✔
57
            InputStream inputStream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
1✔
58
            // write the ro-crate-metadata
59
            zipFile.addStream(inputStream, zipParameters);
1✔
60
            inputStream.close();
1✔
61
            if (crate.getPreview() != null) {
1✔
62
                crate.getPreview().saveAllToZip(zipFile);
1✔
63
            }
64
        } catch (IOException e) {
1✔
65
            logger.error("Exception writing ro-crate-metadata.json file to zip.", e);
1✔
66
        }
1✔
67
    }
1✔
68
}
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