Skip to content

Investigation in renaming directories in zip writers #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ on:
push:
branches: [ main, development ]
pull_request:
branches: [ main, development ]
workflow_dispatch:

env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void addAuthorId(String id) {
* @throws ZipException when something goes wrong with the writing to the
* zip file.
*/
@Deprecated(since = "2.1.0", forRemoval = true)
public void saveToZip(ZipFile zipFile) throws ZipException {
if (this.path != null) {
ZipParameters zipParameters = new ZipParameters();
Expand All @@ -82,6 +83,7 @@ public void saveToZip(ZipFile zipFile) throws ZipException {
* zip file.
* @throws IOException If opening the file input stream fails.
*/
@Deprecated(since = "2.1.0", forRemoval = true)
public void saveToStream(ZipOutputStream zipStream) throws ZipException, IOException {
if (this.path != null) {
ZipUtil.addFileToZipStream(zipStream, this.path.toFile(), this.getId());
Expand All @@ -95,6 +97,7 @@ public void saveToStream(ZipOutputStream zipStream) throws ZipException, IOExcep
* @param file the folder location where the entity should be written.
* @throws IOException if something goes wrong with the writing.
*/
@Deprecated(since = "2.1.0", forRemoval = true)
public void savetoFile(File file) throws IOException {
if (this.getPath() != null) {
if (this.getPath().toFile().isDirectory()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public class FolderStrategy implements GenericWriterStrategy<String> {

private static final Logger logger = LoggerFactory.getLogger(FolderStrategy.class);

protected void saveDataEntity(DataEntity dataEntity, File file) throws IOException {
if (dataEntity.getPath() != null) {
if (dataEntity.getPath().toFile().isDirectory()) {
FileUtils.copyDirectory(dataEntity.getPath().toFile(), file.toPath().resolve(dataEntity.getId()).toFile());
} else {
FileUtils.copyFile(dataEntity.getPath().toFile(), file.toPath().resolve(dataEntity.getId()).toFile());
}
}
}

@Override
public void save(Crate crate, String destination) {
File file = new File(destination);
Expand Down Expand Up @@ -54,7 +64,7 @@ public void save(Crate crate, String destination) {
}
for (DataEntity dataEntity : crate.getAllDataEntities()) {
try {
dataEntity.savetoFile(file);
this.saveDataEntity(dataEntity, file);
} catch (IOException e) {
logger.error("Cannot save " + dataEntity.getId() + " to destination folder!", e);
}
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/edu/kit/datamanager/ro_crate/writer/ZipStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,25 @@ public void save(Crate crate, String destination) {
}
}

private void saveDataEntities(Crate crate, ZipFile zipFile) {
protected void saveDataEntities(Crate crate, ZipFile zipFile) {
for (DataEntity dataEntity : crate.getAllDataEntities()) {
try {
dataEntity.saveToZip(zipFile);
this.saveDataEntity(dataEntity, zipFile);
} catch (ZipException e) {
logger.error("Could not save " + dataEntity.getId() + " to zip file!", e);
}
}
}

private void saveMetadataJson(Crate crate, ZipFile zipFile) {
protected void saveDataEntity(DataEntity dataEntity, ZipFile zipFile) throws ZipException {
if (dataEntity.getPath() != null) {
ZipParameters zipParameters = new ZipParameters();
zipParameters.setFileNameInZip(dataEntity.getId());
zipFile.addFile(dataEntity.getPath().toFile(), zipParameters);
}
}

protected void saveMetadataJson(Crate crate, ZipFile zipFile) {
try {
// write the metadata.json file
ZipParameters zipParameters = new ZipParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

import edu.kit.datamanager.ro_crate.util.ZipUtil;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
import net.lingala.zip4j.model.ZipParameters;
import org.slf4j.Logger;
Expand All @@ -36,17 +40,24 @@ public void save(Crate crate, OutputStream destination) {
}
}

private void saveDataEntities(Crate crate, ZipOutputStream zipStream) {
protected void saveDataEntities(Crate crate, ZipOutputStream zipStream) {
for (DataEntity dataEntity : crate.getAllDataEntities()) {
try {
dataEntity.saveToStream(zipStream);
this.saveDataEntity(dataEntity, zipStream);
} catch (IOException e) {
logger.error("Could not save {} to zip stream!", dataEntity.getId(), e);
}
}
}

private void saveMetadataJson(Crate crate, ZipOutputStream zipStream) {
protected void saveDataEntity(DataEntity dataEntity, ZipOutputStream zipStream) throws ZipException, IOException {
Path path = dataEntity.getPath();
if (path != null) {
ZipUtil.addFileToZipStream(zipStream, path.toFile(), dataEntity.getId());
}
}

protected void saveMetadataJson(Crate crate, ZipOutputStream zipStream) {
try {
// write the metadata.json file
ZipParameters zipParameters = new ZipParameters();
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/edu/kit/datamanager/ro_crate/HelpFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.commons.io.FileUtils;
import io.json.compare.JSONCompare;
import io.json.compare.JsonComparator;
import org.opentest4j.AssertionFailedError;

import java.io.File;
import java.io.IOException;
Expand All @@ -19,6 +20,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class HelpFunctions {

Expand Down Expand Up @@ -89,6 +91,16 @@ public static void compareCrateJsonToFileInResources(Crate crate1, String jsonFi
compare(node1, node2, true);
}

/**
* Compare two directories. The method will compare the content of the two and return true if they are equal.
*
* @param dir1 the first directory
* @param dir2 the second directory
* @return true if the two directories are equal, false otherwise.
* @throws IOException If something goes wrong with the file system.
*
*/
//@Deprecated(since = "2.1.0", forRemoval = true)
public static boolean compareTwoDir(File dir1, File dir2) throws IOException {
// compare the content of the two directories
Map<String, File> result_map = FileUtils.listFiles(dir1, null, true)
Expand Down Expand Up @@ -117,4 +129,53 @@ public static boolean compareTwoDir(File dir1, File dir2) throws IOException {
}
return true;
}

/**
* Asserts that the two directories are equal. Throws an exception otherwise.
*
* @param originalDirectory the original directory to compare to
* @param testingDirectory the testing directory to test
* @throws IOException If something goes wrong with the file system.
*/
public static void assertEqualDirectories(File originalDirectory, File testingDirectory) throws IOException, AssertionFailedError {
assertTrue(originalDirectory.isDirectory(), "The original directory is not a directory.");
assertTrue(testingDirectory.isDirectory(), "The testing directory is not a directory.");
// compare the content of the two directories
Map<String, File> referenceFiles = FileUtils.listFiles(originalDirectory, null, true)
.stream()
.collect(Collectors.toMap(java.io.File::getName, Function.identity()));

Map<String, java.io.File> compareThoseFiles = FileUtils.listFiles(testingDirectory, null, true)
.stream()
.collect(Collectors.toMap(java.io.File::getName, Function.identity()));;

for (String filename : compareThoseFiles.keySet()) {
// we do that because the ro-crate-metadata.json can be differently formatted,
// or the order of the entities may be different
// the same holds for the html file
if (filename.equals("ro-crate-preview.html") || filename.equals("ro-crate-metadata.json")) {
if (!referenceFiles.containsKey(filename)) {
throw new AssertionFailedError("The file %s is not present in the reference directory.".formatted(filename));
}
} else if (!FileUtils.contentEqualsIgnoreEOL(compareThoseFiles.get(filename), referenceFiles.get(filename), null)) {
throw new AssertionFailedError("The content of the file %s is not equal.".formatted(filename));
}
}

referenceFiles.keySet().forEach(filename -> {
if (!compareThoseFiles.containsKey(filename)) {
throw new AssertionFailedError("The file %s is not present in the testing directory.".formatted(filename));
}
});

compareThoseFiles.keySet().forEach(filename -> {
if (!referenceFiles.containsKey(filename)) {
throw new AssertionFailedError("The file %s is not present in the reference directory.".formatted(filename));
}
});

if (referenceFiles.size() != compareThoseFiles.size()) {
throw new AssertionFailedError("The number of files in the two directories is not equal.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ void testWriting(@TempDir Path tempDir) throws IOException {
Path extractionPath = tempDir.resolve("extracted_for_testing");
this.ensureCrateIsExtractedIn(pathToZip, extractionPath);
// compare the extracted directory with the correct one
assertTrue(HelpFunctions.compareTwoDir(
HelpFunctions.assertEqualDirectories(
correctCrate.toFile(),
extractionPath.toFile()));
extractionPath.toFile());
HelpFunctions.compareCrateJsonToFileInResources(
builtCrate,
"/json/crate/fileAndDir.json");
Expand Down
Loading