| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package org.apache.commons.compress.archivers.zip; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.nio.file.Files; |
| 24 | import java.nio.file.Path; |
| 25 | import java.util.concurrent.atomic.AtomicInteger; |
| 26 | |
| 27 | import org.apache.commons.compress.parallel.FileBasedScatterGatherBackingStore; |
| 28 | import org.apache.commons.compress.parallel.ScatterGatherBackingStore; |
| 29 | import org.apache.commons.compress.parallel.ScatterGatherBackingStoreSupplier; |
| 30 | |
| 31 | /** |
| 32 | * Implements {@link ScatterGatherBackingStoreSupplier} using a temporary folder. |
| 33 | * <p> |
| 34 | * For example: |
| 35 | * </p> |
| 36 | * |
| 37 | * <pre> |
| 38 | * final Path dir = Paths.get("target/custom-temp-dir"); |
| 39 | * Files.createDirectories(dir); |
| 40 | * final ParallelScatterZipCreator zipCreator = new ParallelScatterZipCreator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()), |
| 41 | * new DefaultBackingStoreSupplier(dir)); |
| 42 | * </pre> |
| 43 | * |
| 44 | * @since 1.23 |
| 45 | */ |
| 46 | public class DefaultBackingStoreSupplier implements ScatterGatherBackingStoreSupplier { |
| 47 | |
| 48 | private static final String PREFIX = "parallelscatter"; |
| 49 | |
| 50 | private final AtomicInteger storeNum = new AtomicInteger(); |
| 51 | |
| 52 | private final Path dir; |
| 53 | |
| 54 | /** |
| 55 | * Constructs a new instance. If {@code dir} is null, then use the default temporary-file directory. |
| 56 | * |
| 57 | * @param dir temporary folder, may be null, must exist if non-null. |
| 58 | */ |
| 59 | public DefaultBackingStoreSupplier(final Path dir) { |
| 60 | this.dir = dir; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public ScatterGatherBackingStore get() throws IOException { |
| 65 | final String suffix = "n" + storeNum.incrementAndGet(); |
| 66 | final Path tempFile = dir == null ? Files.createTempFile(PREFIX, suffix) : Files.createTempFile(dir, PREFIX, suffix); |
| 67 | return new FileBasedScatterGatherBackingStore(tempFile); |
| 68 | } |
| 69 | } |