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

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

15 Apr 2025 08:24AM UTC coverage: 86.955%. First build
#348

Pull #233

github

web-flow
Merge pull request #244 from kit-data-manager/renovate/commons-io-commons-io-2.x

chore(deps): update dependency commons-io:commons-io to v2.19.0
Pull Request #233: Version 2.1.0

521 of 659 new or added lines in 18 files covered. (79.06%)

1873 of 2154 relevant lines covered (86.95%)

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/ZipWriter.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

6
import edu.kit.datamanager.ro_crate.Crate;
7
import edu.kit.datamanager.ro_crate.entities.data.DataEntity;
8
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
9

10
import java.io.ByteArrayInputStream;
11
import java.io.IOException;
12
import java.io.InputStream;
13
import java.nio.charset.StandardCharsets;
14
import net.lingala.zip4j.ZipFile;
15
import net.lingala.zip4j.exception.ZipException;
16
import net.lingala.zip4j.model.ZipParameters;
17
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
19

20
/**
21
 * Implementation of the writing strategy to provide a way of writing crates to
22
 * a zip archive.
23
 */
24
public class ZipWriter implements WriterStrategy {
1✔
25

26
    private static Logger logger = LoggerFactory.getLogger(ZipWriter.class);
1✔
27

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

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

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