You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/01/12 16:10:30 UTC

[commons-compress] branch master updated: Add org.apache.commons.compress.archivers.zip.DefaultBackingStoreSupplier to write to a custom folder instead of the default temporary folder

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git


The following commit(s) were added to refs/heads/master by this push:
     new a6c6d671 Add org.apache.commons.compress.archivers.zip.DefaultBackingStoreSupplier to write to a custom folder instead of the default temporary folder
a6c6d671 is described below

commit a6c6d6710beec71af392f7595a786335dafa3073
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jan 12 11:10:25 2023 -0500

    Add org.apache.commons.compress.archivers.zip.DefaultBackingStoreSupplier
    to write to a custom folder instead of the default temporary folder
---
 src/changes/changes.xml                            |  1 +
 .../archivers/zip/DefaultBackingStoreSupplier.java | 67 ++++++++++++++++++++++
 .../archivers/zip/ParallelScatterZipCreator.java   | 16 +-----
 .../zip/ParallelScatterZipCreatorTest.java         | 27 ++++++++-
 4 files changed, 95 insertions(+), 16 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 386e657f..abde48a9 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -56,6 +56,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="add" issue="COMPRESS-621" dev="ggregory" due-to="Glavo">Fix calculation the offset of the first zip central directory entry #334.</action>
       <action type="add" issue="COMPRESS-633" dev="ggregory" due-to="Daniel Santos, Bruno P. Kinoshita, Gary Gregory">Add encryption support for SevenZ #332.</action>
       <action type="add" issue="COMPRESS-613" dev="ggregory" due-to="Andre Brait, Gary Gregory, Bruno P. Kinoshita">Support for extra time data in Zip archives #345.</action>      
+      <action type="add" issue="COMPRESS-621" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.compress.archivers.zip.DefaultBackingStoreSupplier to write to a custom folder instead of the default temporary folder.</action>
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Dependabot">Bump mockito.version from 4.8.0 to 4.11.0 #328, #331, #340, #348.</action>
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump pmd from 6.50.0 to 6.53.0.</action>
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/DefaultBackingStoreSupplier.java b/src/main/java/org/apache/commons/compress/archivers/zip/DefaultBackingStoreSupplier.java
new file mode 100644
index 00000000..67cd6f9e
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/DefaultBackingStoreSupplier.java
@@ -0,0 +1,67 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.commons.compress.archivers.zip;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.commons.compress.parallel.FileBasedScatterGatherBackingStore;
+import org.apache.commons.compress.parallel.ScatterGatherBackingStore;
+import org.apache.commons.compress.parallel.ScatterGatherBackingStoreSupplier;
+
+/**
+ * Implements {@link ScatterGatherBackingStoreSupplier} using a temporary folder.
+ * <p>
+ * For example:
+ * </p>
+ * <pre>
+ * final Path dir = Paths.get("target/custom-temp-dir");
+ * Files.createDirectories(dir);
+ * final ParallelScatterZipCreator zipCreator = new ParallelScatterZipCreator(
+ *     Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()),
+ *     new DefaultBackingStoreSupplier(dir));
+ * </pre>
+ *
+ * @since 1.23
+ */
+public class DefaultBackingStoreSupplier implements ScatterGatherBackingStoreSupplier {
+
+    private static final String PREFIX = "parallelscatter";
+
+    /**
+     * Constructs a new instance. If {@code dir} is null, then use the default temporary-file directory.
+     *
+     * @param dir temporary folder, may be null, must exist if non-null.
+     */
+    public DefaultBackingStoreSupplier(final Path dir) {
+        this.dir = dir;
+    }
+
+    private final AtomicInteger storeNum = new AtomicInteger();
+    private final Path dir;
+
+    @Override
+    public ScatterGatherBackingStore get() throws IOException {
+        final String suffix = "n" + storeNum.incrementAndGet();
+        final Path tempFile = dir == null ? Files.createTempFile(PREFIX, suffix) : Files.createTempFile(dir, PREFIX, suffix);
+        return new FileBasedScatterGatherBackingStore(tempFile);
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreator.java b/src/main/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreator.java
index 69d30b3e..2ade4aed 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreator.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreator.java
@@ -21,8 +21,6 @@ import static org.apache.commons.compress.archivers.zip.ZipArchiveEntryRequest.c
 
 import java.io.IOException;
 import java.io.UncheckedIOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
 import java.util.Deque;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ConcurrentLinkedDeque;
@@ -31,10 +29,8 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
 import java.util.zip.Deflater;
 
-import org.apache.commons.compress.parallel.FileBasedScatterGatherBackingStore;
 import org.apache.commons.compress.parallel.InputStreamSupplier;
 import org.apache.commons.compress.parallel.ScatterGatherBackingStore;
 import org.apache.commons.compress.parallel.ScatterGatherBackingStoreSupplier;
@@ -55,16 +51,6 @@ import org.apache.commons.compress.parallel.ScatterGatherBackingStoreSupplier;
  */
 public class ParallelScatterZipCreator {
 
-    private static class DefaultBackingStoreSupplier implements ScatterGatherBackingStoreSupplier {
-        final AtomicInteger storeNum = new AtomicInteger(0);
-
-        @Override
-        public ScatterGatherBackingStore get() throws IOException {
-            final Path tempFile = Files.createTempFile("parallelscatter", "n" + storeNum.incrementAndGet());
-            return new FileBasedScatterGatherBackingStore(tempFile);
-        }
-    }
-
     private final Deque<ScatterZipOutputStream> streams = new ConcurrentLinkedDeque<>();
     private final ExecutorService es;
     private final ScatterGatherBackingStoreSupplier backingStoreSupplier;
@@ -104,7 +90,7 @@ public class ParallelScatterZipCreator {
      *                        this will be shut down by this class.
      */
     public ParallelScatterZipCreator(final ExecutorService executorService) {
-        this(executorService, new DefaultBackingStoreSupplier());
+        this(executorService, new DefaultBackingStoreSupplier(null));
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreatorTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreatorTest.java
index 71ae7b94..cadfb2ac 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreatorTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreatorTest.java
@@ -30,6 +30,8 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
@@ -144,7 +146,30 @@ public class ParallelScatterZipCreatorTest {
     }
 
     @Test
-    public void concurrent()
+    public void concurrentCustomTempFolder()
+            throws Exception {
+        result = File.createTempFile("parallelScatterGather1", "");
+        final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(result);
+        zos.setEncoding("UTF-8");
+
+        // Formatter:off
+        final Path dir = Paths.get("target/custom-temp-dir");
+        Files.createDirectories(dir);
+        final ParallelScatterZipCreator zipCreator = new ParallelScatterZipCreator(
+                Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()),
+                new DefaultBackingStoreSupplier(dir));
+        // Formatter:on
+
+        final Map<String, byte[]> entries = writeEntries(zipCreator);
+        zipCreator.writeTo(zos);
+        zos.close();
+        removeEntriesFoundInZipFile(result, entries);
+        assertTrue(entries.isEmpty());
+        assertNotNull( zipCreator.getStatisticsMessage());
+    }
+
+    @Test
+    public void concurrentDefaultTempFolder()
             throws Exception {
         result = File.createTempFile("parallelScatterGather1", "");
         final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(result);