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

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

19 May 2025 01:23PM UTC coverage: 90.495% (+0.3%) from 90.169%
#430

Pull #258

github

web-flow
Merge b22e70bd8 into 810d1995c
Pull Request #258: Support .ELN-style crates in all zip readers and writers

276 of 305 new or added lines in 21 files covered. (90.49%)

1 existing line in 1 file now uncovered.

2009 of 2220 relevant lines covered (90.5%)

0.9 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/preview/CratePreview.java
1
package edu.kit.datamanager.ro_crate.preview;
2

3
import java.io.File;
4
import java.io.IOException;
5
import java.nio.file.Files;
6

7
import edu.kit.datamanager.ro_crate.Crate;
8
import edu.kit.datamanager.ro_crate.writer.CrateWriter;
9
import edu.kit.datamanager.ro_crate.writer.WriteFolderStrategy;
10
import net.lingala.zip4j.ZipFile;
11
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
12
import org.apache.commons.io.FileUtils;
13

14
/**
15
 * Interface for the ROCrate preview. This manages the human-readable
16
 * representation of a crate.
17
 *
18
 * @author Nikola Tzotchev on 6.2.2022 г.
19
 * @author jejkal
20
 * @version 2
21
 */
22
public interface CratePreview {
23

24
    /**
25
     * Generate a preview of the crate and store it into the given target directory.
26
     * It is the caller's responsibility to handle, e.g. delete after use, the result
27
     * (The caller takes ownership of the result).
28
     * <p>
29
     * <b>IMPORTANT NOTE:</b> This method currently has a default implementation that relies
30
     * on deprecated methods. In future, you will have to implement this method directly.
31
     *
32
     * @param crate the crate to generate a preview for.
33
     * @param targetDir the target directory to store the preview in,
34
     *                 owned by the caller.
35
     * @throws IOException if an error occurs while generating the preview.
36
     */
37
    default void generate(Crate crate, File targetDir) throws IOException {
38
        // disable preview generation to avoid recursion,
39
        // as this is usually called in the process of writing a crate
40
        // (including preview)
41
        new CrateWriter<>(new WriteFolderStrategy().disablePreview())
1✔
42
                .save(crate, targetDir.getAbsolutePath());
1✔
43
        this.saveAllToFolder(targetDir);
1✔
44
        try (var stream = Files.list(targetDir.toPath())) {
1✔
45
            stream
1✔
46
                    .filter(path -> !path.getFileName().toString().equals("ro-crate-preview.html"))
1✔
47
                    .filter(path -> !path.getFileName().toString().equals("ro-crate-preview_files"))
1✔
48
                    .forEach(path -> {
1✔
49
                        try {
50
                            if (Files.isDirectory(path)) {
1✔
51
                                FileUtils.deleteDirectory(path.toFile());
1✔
52
                            } else {
53
                                Files.delete(path);
1✔
54
                            }
NEW
55
                        } catch (IOException e) {
×
56
                            // Silently ignore deletion errors
57
                        }
1✔
58
                    });
1✔
59
        }
60
    }
1✔
61

62
    /**
63
     * Takes a crate in form of a zip file and generates a preview of it,
64
     * which will be stored within the crate.
65
     *
66
     * @param zipFile the zip file with the crate, which should receive a preview.
67
     * @throws IOException if an error occurs while saving the preview
68
     *
69
     * @deprecated Use {@link #generate(Crate, File)} instead.
70
     */
71
    @Deprecated(since = "2.1.0", forRemoval = true)
72
    void saveAllToZip(ZipFile zipFile) throws IOException;
73

74
    /**
75
     * Saves the preview, given by the folder, into the given folder.
76
     *
77
     * @param folder the folder (containing a crate) to save the preview in.
78
     * @throws IOException if an error occurs while saving the preview.
79
     *
80
     * @deprecated Use {@link #generate(Crate, File)} instead.
81
     */
82
    @Deprecated(since = "2.1.0", forRemoval = true)
83
    void saveAllToFolder(File folder) throws IOException;
84

85
    /**
86
     * Saves the preview, given by the metadata, into the given stream.
87
     *
88
     * @param metadata the metadata of the crate to save the preview in.
89
     * @param stream the stream to save the preview in.
90
     * @throws IOException if an error occurs while saving the preview.
91
     *
92
     * @deprecated Use {@link #generate(Crate, File)} instead.
93
     */
94
    @Deprecated(since = "2.1.0", forRemoval = true)
95
    void saveAllToStream(String metadata, ZipOutputStream stream) throws IOException;
96

97
}
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