From b0e6fef5acc32b228a376c42dbec9e763f9ed875 Mon Sep 17 00:00:00 2001
From: vkovtun <viktor.kovtun@id.ethz.ch>
Date: Fri, 1 Dec 2023 16:43:02 +0100
Subject: [PATCH] BIS-772: Working on data export. Fixed the bug thatan empty
 file with the same name as the folder was created.

---
 .../v3/executor/exporter/ExportExecutor.java  | 36 ++++++++++---------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/server-application-server/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutor.java b/server-application-server/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutor.java
index 55231240732..eeaeea3bfba 100644
--- a/server-application-server/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutor.java
+++ b/server-application-server/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutor.java
@@ -400,19 +400,9 @@ public class ExportExecutor implements IExportExecutor
                     DataSetFileDownload file;
                     while ((file = reader.read()) != null)
                     {
-                        final DataSetFile dataSetFile = file.getDataSetFile();
-                        final String filePath = dataSetFile.getPath();
-                        putNextDataZipEntry(existingZipEntries, zos, 'O', getSpaceCode(sample), getProjectCode(sample),
+                        putNextDataZipEntry(existingZipEntries, zos, bos, 'O', getSpaceCode(sample), getProjectCode(sample),
                                 container == null ? null : container.getCode(), sample.getCode(), dataSetTypeCode, dataSetCode,
-                                getEntityName(dataSet), getFileName(filePath));
-
-                        if (!dataSetFile.isDirectory())
-                        {
-                            try (final InputStream is = file.getInputStream())
-                            {
-                                writeInChunks(bos, is);
-                            }
-                        }
+                                getEntityName(dataSet), file);
                     }
 
 //                    for (final DataSetFile dataSetFile : dataSetFiles)
@@ -945,17 +935,31 @@ public class ExportExecutor implements IExportExecutor
         return entryBuilder.toString();
     }
 
-    private static void putNextDataZipEntry(final Set<String> existingZipEntries, final ZipOutputStream zos, final char prefix,
-            final String spaceCode, final String projectCode, final String containerCode, final String entityCode,
-            final String dataSetTypeCode, final String dataSetCode, final String dataSetName, final String fileName)
+    private static void putNextDataZipEntry(final Set<String> existingZipEntries, final ZipOutputStream zos, final BufferedOutputStream bos,
+            final char prefix, final String spaceCode, final String projectCode, final String containerCode, final String entityCode,
+            final String dataSetTypeCode, final String dataSetCode, final String dataSetName, final DataSetFileDownload dataSetFileDownload)
             throws IOException
     {
+        final DataSetFile dataSetFile = dataSetFileDownload.getDataSetFile();
+        final String filePath = dataSetFile.getPath();
+        final boolean isDirectory = dataSetFile.isDirectory();
+
         final String entry = getFolderName(prefix, spaceCode, projectCode, containerCode, entityCode, dataSetTypeCode, dataSetCode,
-                dataSetName, fileName);
+                dataSetName, filePath) + (isDirectory ? "/" : "");
         if (!existingZipEntries.contains(entry))
         {
             zos.putNextEntry(new ZipEntry(entry));
             existingZipEntries.add(entry);
+
+            if (!isDirectory)
+            {
+                try (final InputStream is = dataSetFileDownload.getInputStream())
+                {
+                    writeInChunks(bos, is);
+                }
+            }
+
+            zos.closeEntry();
         }
     }
 
-- 
GitLab