|
| 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 | + * http://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 | +package ru.olegcherednik.zip4jvm.engine.unzip; |
| 20 | + |
| 21 | +import ru.olegcherednik.zip4jvm.model.ZipModel; |
| 22 | +import ru.olegcherednik.zip4jvm.model.entry.ZipEntry; |
| 23 | +import ru.olegcherednik.zip4jvm.model.password.PasswordProvider; |
| 24 | +import ru.olegcherednik.zip4jvm.utils.quitely.Quietly; |
| 25 | +import ru.olegcherednik.zip4jvm.utils.quitely.functions.RunnableWithException; |
| 26 | + |
| 27 | +import org.apache.commons.collections4.CollectionUtils; |
| 28 | + |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.util.Iterator; |
| 31 | +import java.util.LinkedList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Set; |
| 34 | +import java.util.concurrent.CompletableFuture; |
| 35 | +import java.util.concurrent.Executor; |
| 36 | +import java.util.concurrent.ExecutorService; |
| 37 | +import java.util.concurrent.ForkJoinPool; |
| 38 | +import java.util.concurrent.ForkJoinWorkerThread; |
| 39 | +import java.util.concurrent.atomic.AtomicInteger; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Oleg Cherednik |
| 43 | + * @since 28.12.2024 |
| 44 | + */ |
| 45 | +public class UnzipExtractAsyncEngine extends UnzipExtractEngine { |
| 46 | + |
| 47 | + protected final int totalThreads; |
| 48 | + |
| 49 | + public UnzipExtractAsyncEngine(PasswordProvider passwordProvider, ZipModel zipModel, int totalThreads) { |
| 50 | + super(passwordProvider, zipModel); |
| 51 | + this.totalThreads = totalThreads <= 0 ? Runtime.getRuntime().availableProcessors() : totalThreads; |
| 52 | + } |
| 53 | + |
| 54 | + // ---------- UnzipExtractEngine ---------- |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void extractAllEntries(Path dstDir) { |
| 58 | + List<CompletableFuture<Void>> tasks = new LinkedList<>(); |
| 59 | + Iterator<ZipEntry> it = zipModel.absOffsAscIterator(); |
| 60 | + |
| 61 | + ConsecutiveAccessDataInputHolder dataInputHolder = |
| 62 | + new ConsecutiveAccessDataInputHolder(this::createConsecutiveDataInput); |
| 63 | + ExecutorService executor = createExecutor(); |
| 64 | + |
| 65 | + try { |
| 66 | + while (it.hasNext()) { |
| 67 | + ZipEntry zipEntry = it.next(); |
| 68 | + Path file = dstDir.resolve(zipEntry.getFileName()); |
| 69 | + |
| 70 | + CompletableFuture<Void> task = createCompletableFuture( |
| 71 | + () -> extractEntry(file, zipEntry, dataInputHolder.get()), executor); |
| 72 | + |
| 73 | + tasks.add(task); |
| 74 | + } |
| 75 | + |
| 76 | + tasks.forEach(CompletableFuture::join); |
| 77 | + } finally { |
| 78 | + executor.shutdown(); |
| 79 | + dataInputHolder.release(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected void extractEntryByPrefix(Path dstDir, Set<String> prefixes) { |
| 85 | + assert CollectionUtils.isNotEmpty(prefixes); |
| 86 | + |
| 87 | + List<CompletableFuture<Void>> tasks = new LinkedList<>(); |
| 88 | + Iterator<ZipEntry> it = zipModel.absOffsAscIterator(); |
| 89 | + |
| 90 | + ConsecutiveAccessDataInputHolder dataInputHolder = |
| 91 | + new ConsecutiveAccessDataInputHolder(this::createConsecutiveDataInput); |
| 92 | + ExecutorService executor = createExecutor(); |
| 93 | + |
| 94 | + try { |
| 95 | + while (it.hasNext()) { |
| 96 | + ZipEntry zipEntry = it.next(); |
| 97 | + String fileName = getFileName(zipEntry, prefixes); |
| 98 | + |
| 99 | + if (fileName != null) { |
| 100 | + Path file = dstDir.resolve(fileName); |
| 101 | + CompletableFuture<Void> task = createCompletableFuture( |
| 102 | + () -> extractEntry(file, zipEntry, dataInputHolder.get()), executor); |
| 103 | + |
| 104 | + tasks.add(task); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + tasks.forEach(CompletableFuture::join); |
| 109 | + } finally { |
| 110 | + dataInputHolder.release(); |
| 111 | + executor.shutdown(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // ---------- |
| 116 | + |
| 117 | + protected ExecutorService createExecutor() { |
| 118 | + AtomicInteger counter = new AtomicInteger(); |
| 119 | + String format = String.format("zip4jvm-extract-%%0%dd", String.valueOf(totalThreads).length()); |
| 120 | + |
| 121 | + ForkJoinPool.ForkJoinWorkerThreadFactory factory = pool -> { |
| 122 | + ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool); |
| 123 | + thread.setName(String.format(format, counter.incrementAndGet())); |
| 124 | + return thread; |
| 125 | + }; |
| 126 | + |
| 127 | + return new ForkJoinPool(totalThreads, factory, null, false); |
| 128 | + } |
| 129 | + |
| 130 | + protected CompletableFuture<Void> createCompletableFuture(RunnableWithException task, Executor executor) { |
| 131 | + return CompletableFuture.runAsync(() -> Quietly.doRuntime(task), executor); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments