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

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

29 Apr 2025 01:00PM UTC coverage: 86.311%. First build
#378

Pull #233

github

web-flow
Merge pull request #253 from kit-data-manager/fix-renaming-of-data-set-entities

Fix renaming of data set entities
Pull Request #233: Version 2.1.0

598 of 763 new or added lines in 29 files covered. (78.37%)

1879 of 2177 relevant lines covered (86.31%)

0.86 hits per line

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

79.31
/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✔
NEW
42
            } catch (ZipException e) {
×
NEW
43
                logger.error("Could not save " + dataEntity.getId() + " to zip file!", e);
×
44
            }
1✔
45
        }
1✔
46
    }
1✔
47

48
    private void saveMetadataJson(Crate crate, ZipFile zipFile) {
49
        // write the metadata.json file
50
        ZipParameters zipParameters = new ZipParameters();
1✔
51
        zipParameters.setFileNameInZip("ro-crate-metadata.json");
1✔
52
        ObjectMapper objectMapper = MyObjectMapper.getMapper();
1✔
53
        try {
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
            try (InputStream inputStream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))) {
1✔
58
                // write the ro-crate-metadata
59
                zipFile.addStream(inputStream, zipParameters);
1✔
60
            }
61
            if (crate.getPreview() != null) {
1✔
62
                crate.getPreview().saveAllToZip(zipFile);
1✔
63
            }
NEW
64
        } catch (IOException e) {
×
NEW
65
            logger.error("Exception writing ro-crate-metadata.json file to zip.", e);
×
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