|
| 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.exception.Zip4jvmException; |
| 22 | +import ru.olegcherednik.zip4jvm.io.in.file.consecutive.ConsecutiveAccessDataInput; |
| 23 | +import ru.olegcherednik.zip4jvm.model.ZipModel; |
| 24 | +import ru.olegcherednik.zip4jvm.model.entry.ZipEntry; |
| 25 | +import ru.olegcherednik.zip4jvm.model.password.PasswordProvider; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.util.Iterator; |
| 30 | +import java.util.LinkedList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Optional; |
| 34 | +import java.util.concurrent.CompletableFuture; |
| 35 | +import java.util.concurrent.Executor; |
| 36 | +import java.util.concurrent.ForkJoinPool; |
| 37 | +import java.util.concurrent.ForkJoinWorkerThread; |
| 38 | +import java.util.concurrent.atomic.AtomicInteger; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Oleg Cherednik |
| 42 | + * @since 28.12.2024 |
| 43 | + */ |
| 44 | +public class UnzipExtractAsyncEngine extends UnzipExtractEngine { |
| 45 | + |
| 46 | + public UnzipExtractAsyncEngine(PasswordProvider passwordProvider, ZipModel zipModel) { |
| 47 | + super(passwordProvider, zipModel); |
| 48 | + } |
| 49 | + |
| 50 | + // ---------- UnzipExtractEngine ---------- |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void extractEntry(Path dstDir, Map<String, String> map) { |
| 54 | + List<CompletableFuture<Void>> tasks = new LinkedList<>(); |
| 55 | + Iterator<ZipEntry> it = zipModel.absOffsAscIterator(); |
| 56 | + DataInputThreadLocal<ConsecutiveAccessDataInput> threadLocalDataInput = |
| 57 | + new DataInputThreadLocal<>(this::createConsecutiveDataInput); |
| 58 | + |
| 59 | + try { |
| 60 | + Executor executor = createExecutor(); |
| 61 | + |
| 62 | + while (it.hasNext()) { |
| 63 | + ZipEntry zipEntry = it.next(); |
| 64 | + |
| 65 | + if (map != null && !map.containsKey(zipEntry.getFileName())) |
| 66 | + continue; |
| 67 | + |
| 68 | + String fileName = Optional.ofNullable(map) |
| 69 | + .map(m -> m.get(zipEntry.getFileName())) |
| 70 | + .orElse(zipEntry.getFileName()); |
| 71 | + Path file = dstDir.resolve(fileName); |
| 72 | + |
| 73 | + CompletableFuture<Void> task = CompletableFuture.runAsync( |
| 74 | + () -> { |
| 75 | + try { |
| 76 | + ConsecutiveAccessDataInput in = threadLocalDataInput.get(); |
| 77 | + in.seekForward(zipEntry.getLocalFileHeaderAbsOffs()); |
| 78 | + extractEntry(file, zipEntry, in); |
| 79 | + } catch (IOException e) { |
| 80 | + throw new Zip4jvmException(e); |
| 81 | + } |
| 82 | + }, |
| 83 | + executor); |
| 84 | + |
| 85 | +// CompletableFuture<Void> task = CompletableFuture.runAsync(() -> { |
| 86 | +// try { |
| 87 | +// ConsecutiveAccessDataInput in = threadLocalDataInput.get(); |
| 88 | +// extractEntry(file, zipEntry, in); |
| 89 | +// } catch (IOException e) { |
| 90 | +// throw new Zip4jvmException(e); |
| 91 | +// } |
| 92 | +// }); |
| 93 | + |
| 94 | + tasks.add(task); |
| 95 | + } |
| 96 | + |
| 97 | + tasks.forEach(CompletableFuture::join); |
| 98 | + } finally { |
| 99 | + threadLocalDataInput.release(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + protected Executor createExecutor() { |
| 104 | + AtomicInteger counter = new AtomicInteger(); |
| 105 | + |
| 106 | + ForkJoinPool.ForkJoinWorkerThreadFactory factory = pool -> { |
| 107 | + ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool); |
| 108 | + thread.setName(String.format("zip4jvm-extract-%02d", counter.incrementAndGet())); |
| 109 | + return thread; |
| 110 | + }; |
| 111 | + |
| 112 | + return new ForkJoinPool(Runtime.getRuntime().availableProcessors(), factory, null, false); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments