You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2019/12/30 12:40:59 UTC

[isis] 02/03: ISIS-2231: reconcile zip service with already ported to v2 version (in incode-platform origin/v2 branch)

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit bf42001cf3e97bc3b4bd93ea86be80e318f30591
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Mon Dec 30 12:37:19 2019 +0000

    ISIS-2231: reconcile zip service with already ported to v2 version (in incode-platform origin/v2 branch)
---
 .../isis/extensions/zip/dom/impl/ZipService.java   | 40 +++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/extensions/lib/zip/impl/src/main/java/org/apache/isis/extensions/zip/dom/impl/ZipService.java b/extensions/lib/zip/impl/src/main/java/org/apache/isis/extensions/zip/dom/impl/ZipService.java
index 620115a..e702dfa 100644
--- a/extensions/lib/zip/impl/src/main/java/org/apache/isis/extensions/zip/dom/impl/ZipService.java
+++ b/extensions/lib/zip/impl/src/main/java/org/apache/isis/extensions/zip/dom/impl/ZipService.java
@@ -26,11 +26,16 @@ public class ZipService {
         private final File file;
     }
 
+    @Deprecated
+    public byte[] zip(final List<FileAndName> fileAndNameList) {
+        return zipNamedFiles(fileAndNameList);
+    }
+
     /**
      * Rather than use the name of the file (which might be temporary files, for example)
      * we explicitly provide the name to use (in the ZipEntry).
      */
-    public byte[] zip(final List<FileAndName> fileAndNameList) {
+    public byte[] zipNamedFiles(final List<FileAndName> fileAndNameList) {
 
         try {
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -58,4 +63,37 @@ public class ZipService {
                            .collect(Collectors.toList())
                 );
     }
+
+    @Data
+    public static class BytesAndName {
+        private final String name;
+        private final byte[] bytes;
+    }
+
+    /**
+     * Similar to {@link #zipNamedFiles(List)}, but uses simple byte[] as the input, rather than files.
+     *
+     * @param bytesAndNameList
+     * @return
+     */
+    public byte[] zipNamedBytes(final List<BytesAndName> bytesAndNameList) {
+
+        final byte[] bytes;
+        try {
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final ZipOutputStream zos = new ZipOutputStream(baos);
+
+            for (final BytesAndName ban : bytesAndNameList) {
+                zos.putNextEntry(new ZipEntry(ban.getName()));
+                zos.write(ban.getBytes());
+                zos.closeEntry();
+            }
+            zos.close();
+            bytes = baos.toByteArray();
+        } catch (final IOException ex) {
+            throw new FatalException("Unable to create zip", ex);
+        }
+        return bytes;
+    }
+
 }