diff --git a/src/main/java/com/databasepreservation/common/api/v1/utils/ZipOutputStream.java b/src/main/java/com/databasepreservation/common/api/v1/utils/ZipOutputStream.java index 42f40d1a..1a09d6f4 100644 --- a/src/main/java/com/databasepreservation/common/api/v1/utils/ZipOutputStream.java +++ b/src/main/java/com/databasepreservation/common/api/v1/utils/ZipOutputStream.java @@ -133,7 +133,7 @@ protected void writeToZipFile(ZipFile siardArchive, ZipArchiveOutputStream out, if (binaryColumn != null) { if (isSiardDK) { - handleWriteSIARDDKLobs(out, cellEntry.getValue()); + handleWriteSIARDDKLobs(out, cellEntry.getValue(), binaryColumn, row); } else { if (ViewerType.dbTypes.CLOB.equals(binaryColumn.getType())) { handleWriteClob(out, binaryColumn, row); @@ -193,18 +193,27 @@ private void handleWriteExternalLobs(ZipArchiveOutputStream out, ColumnStatus bi addEntryToZip(out, inputStream, templateFilename); } - private void handleWriteSIARDDKLobs(ZipArchiveOutputStream out, ViewerCell cell) throws IOException { + private void handleWriteSIARDDKLobs(ZipArchiveOutputStream out, ViewerCell cell, ColumnStatus binaryColumn, + ViewerRow row) throws IOException { final String lobLocation = cell.getValue(); final Path lobPath = Paths.get(lobLocation); if (lobPath.toFile().isDirectory()) { + int index = 0; for (File file : Objects.requireNonNull(lobPath.toFile().listFiles())) { InputStream inputStream = Files.newInputStream(file.toPath()); - addEntryToZip(out, inputStream, file.getName()); + String extension = file.getName().substring(file.getName().lastIndexOf(".") + 1); + String templateFilename = index + "_" + FilenameUtils.getTemplateFilename(row, configTable, binaryColumn, + lobPath.getFileName().toString()); + String templateFilenameWithActualExtension = templateFilename.replace(ExtraMediaType.ZIP_FILE_EXTENSION, "." + extension); + addEntryToZip(out, inputStream, templateFilenameWithActualExtension); + index++; } } else { InputStream inputStream = Files.newInputStream(lobPath); - addEntryToZip(out, inputStream, lobPath.getFileName().toString()); + final String templateFilename = FilenameUtils.getTemplateFilename(row, configTable, binaryColumn, + lobPath.getFileName().toString()); + addEntryToZip(out, inputStream, templateFilename); } } diff --git a/src/main/java/com/databasepreservation/common/api/v1/utils/ZipOutputStreamMultiRow.java b/src/main/java/com/databasepreservation/common/api/v1/utils/ZipOutputStreamMultiRow.java index 20f68b7a..f4a4f7b0 100644 --- a/src/main/java/com/databasepreservation/common/api/v1/utils/ZipOutputStreamMultiRow.java +++ b/src/main/java/com/databasepreservation/common/api/v1/utils/ZipOutputStreamMultiRow.java @@ -18,6 +18,7 @@ import java.util.stream.Stream; import java.util.zip.ZipFile; +import com.databasepreservation.common.client.ViewerConstants; import org.apache.commons.compress.archivers.zip.Zip64Mode; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; @@ -53,7 +54,6 @@ public ZipOutputStreamMultiRow(final CollectionStatus configurationCollection, f @Override public void consumeOutputStream(OutputStream out) throws IOException { - ZipFile siardArchive = new ZipFile(getDatabase().getPath()); boolean all = false; if (sublist == null) { @@ -76,7 +76,13 @@ public void consumeOutputStream(OutputStream out) throws IOException { nIndex++; continue; } else { - writeToZipFile(siardArchive, zipArchiveOutputStream, row, lobColumns); + if (getDatabase().getVersion().equals(ViewerConstants.SIARD_DK_1007) + || getDatabase().getVersion().equals(ViewerConstants.SIARD_DK_128)) { + writeToZipFile(null, zipArchiveOutputStream, row, lobColumns, true); + } else { + ZipFile siardArchive = new ZipFile(getDatabase().getPath()); + writeToZipFile(siardArchive, zipArchiveOutputStream, row, lobColumns); + } } nIndex++; }