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

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

01 Apr 2025 01:45PM UTC coverage: 88.589%. First build
#323

Pull #233

github

ThomasJejkal
Added zip stream writer, minor cleanup
Pull Request #233: CI trigger adjustments

34 of 82 new or added lines in 4 files covered. (41.46%)

1607 of 1814 relevant lines covered (88.59%)

0.89 hits per line

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

0.0
/src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStreamWriter.java
1
package edu.kit.datamanager.ro_crate.writer;
2

3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.JsonNode;
5
import com.fasterxml.jackson.databind.ObjectMapper;
6

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

11
import java.io.ByteArrayInputStream;
12
import java.io.FileInputStream;
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.io.OutputStream;
16
import java.nio.charset.StandardCharsets;
17
import net.lingala.zip4j.exception.ZipException;
18
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
19
import net.lingala.zip4j.model.ZipParameters;
20

21
/**
22
 * Implementation of the writing strategy to provide a way of writing crates to
23
 * a zip archive.
24
 */
NEW
25
public class ZipStreamWriter implements StreamWriterStrategy {
×
26

27
    @Override
28
    public void save(Crate crate, OutputStream destination) {
29

NEW
30
        try (ZipOutputStream zipFile = new ZipOutputStream(destination)) {
×
NEW
31
            saveMetadataJson(crate, zipFile);
×
NEW
32
            saveDataEntities(crate, zipFile);
×
NEW
33
        } catch (IOException e) {
×
34
            // can not close ZipOutputStream (threw Exception)
NEW
35
        }
×
NEW
36
    }
×
37

38
    private void saveDataEntities(Crate crate, ZipOutputStream zipStream) {
NEW
39
        for (DataEntity dataEntity : crate.getAllDataEntities()) {
×
40
            try {
NEW
41
                dataEntity.saveToStream(zipStream);
×
NEW
42
            } catch (IOException e) {
×
NEW
43
                System.out.println("could not save " + dataEntity.getId() + " to zip stream!");
×
NEW
44
            }
×
NEW
45
        }
×
NEW
46
    }
×
47

48
    private void saveMetadataJson(Crate crate, ZipOutputStream zipStream) {
49
        try {
50
            // write the metadata.json file
NEW
51
            ZipParameters zipParameters = new ZipParameters();
×
NEW
52
            zipParameters.setFileNameInZip("ro-crate-metadata.json");
×
NEW
53
            ObjectMapper objectMapper = MyObjectMapper.getMapper();
×
54
            // we create an JsonNode only to have the file written pretty
NEW
55
            JsonNode node = objectMapper.readTree(crate.getJsonMetadata());
×
NEW
56
            String str = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
×
57
            // write the ro-crate-metadata
58

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

69
            //TODO: Preview not written as creation requires extracted crate, 
70
            //which is not possible if directly written to stream. To be done later.
71
            /*  if (crate.getPreview() != null) {
72
                crate.getPreview().saveAllToStream(zipStream);
73
            }*/
NEW
74
        } catch (ZipException | JsonProcessingException e) {
×
NEW
75
            System.out.println("Exception writing ro-crate-metadata.json file to zip");
×
NEW
76
            e.printStackTrace();
×
NEW
77
        } catch (IOException e) {
×
NEW
78
            e.printStackTrace();
×
NEW
79
        }
×
NEW
80
    }
×
81
}
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

© 2026 Coveralls, Inc