• 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

81.82
/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStreamStrategy.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.io.OutputStream;
14
import java.nio.charset.StandardCharsets;
15
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
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 ZipStreamStrategy implements GenericWriterStrategy<OutputStream> {
1✔
25

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

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

39
    private void saveDataEntities(Crate crate, ZipOutputStream zipStream) {
40
        for (DataEntity dataEntity : crate.getAllDataEntities()) {
1✔
41
            try {
42
                dataEntity.saveToStream(zipStream);
1✔
43
            } catch (IOException e) {
×
NEW
44
                logger.error("Could not save {} to zip stream!", dataEntity.getId(), e);
×
45
            }
1✔
46
        }
1✔
47
    }
1✔
48

49
    private void saveMetadataJson(Crate crate, ZipOutputStream zipStream) {
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
            // write the ro-crate-metadata
59

60
            byte[] buff = new byte[4096];
1✔
61
            int readLen;
62
            zipStream.putNextEntry(zipParameters);
1✔
63
            try (InputStream inputStream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))) {
1✔
64
                while ((readLen = inputStream.read(buff)) != -1) {
1✔
65
                    zipStream.write(buff, 0, readLen);
1✔
66
                }
67
            }
68
            zipStream.closeEntry();
1✔
69

70
            if (crate.getPreview() != null) {
1✔
71
                crate.getPreview().saveAllToStream(str, zipStream);
1✔
72
            }
73
        } catch (IOException e) {
×
74
            logger.error("Exception writing ro-crate-metadata.json file to zip.", e);
×
75
        }
1✔
76
    }
1✔
77
}
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