|
| 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 | + private static final int MIN_ENTRIES_AMOUNT = 100; |
| 47 | + |
| 48 | + public UnzipExtractAsyncEngine(PasswordProvider passwordProvider, ZipModel zipModel) { |
| 49 | + super(passwordProvider, zipModel); |
| 50 | + } |
| 51 | + |
| 52 | + // ---------- UnzipExtractEngine ---------- |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void extractEntry(Path dstDir, Map<String, String> map) { |
| 56 | + if (map == null ? zipModel.getTotalEntries() <= MIN_ENTRIES_AMOUNT : map.size() <= MIN_ENTRIES_AMOUNT) |
| 57 | + super.extractEntry(dstDir, map); |
| 58 | + else |
| 59 | + extractEntryAsync(dstDir, map); |
| 60 | + } |
| 61 | + |
| 62 | + // ---------- |
| 63 | + |
| 64 | + protected void extractEntryAsync(Path dstDir, Map<String, String> map) { |
| 65 | + List<CompletableFuture<Void>> tasks = new LinkedList<>(); |
| 66 | + Iterator<ZipEntry> it = zipModel.absOffsAscIterator(); |
| 67 | + DataInputThreadLocal<ConsecutiveAccessDataInput> threadLocalDataInput = |
| 68 | + new DataInputThreadLocal<>(this::createConsecutiveDataInput); |
| 69 | + |
| 70 | + try { |
| 71 | + Executor executor = createExecutor(); |
| 72 | + |
| 73 | + while (it.hasNext()) { |
| 74 | + ZipEntry zipEntry = it.next(); |
| 75 | + |
| 76 | + if (map != null && !map.containsKey(zipEntry.getFileName())) |
| 77 | + continue; |
| 78 | + |
| 79 | + String fileName = Optional.ofNullable(map) |
| 80 | + .map(m -> m.get(zipEntry.getFileName())) |
| 81 | + .orElse(zipEntry.getFileName()); |
| 82 | + Path file = dstDir.resolve(fileName); |
| 83 | + |
| 84 | + CompletableFuture<Void> task = CompletableFuture.runAsync( |
| 85 | + () -> { |
| 86 | + try { |
| 87 | + ConsecutiveAccessDataInput in = threadLocalDataInput.get(); |
| 88 | + extractEntry(file, zipEntry, in); |
| 89 | + } catch (IOException e) { |
| 90 | + throw new Zip4jvmException(e); |
| 91 | + } |
| 92 | + }, |
| 93 | + executor); |
| 94 | + |
| 95 | +// CompletableFuture<Void> task = CompletableFuture.runAsync(() -> { |
| 96 | +// try { |
| 97 | +// ConsecutiveAccessDataInput in = threadLocalDataInput.get(); |
| 98 | +// extractEntry(file, zipEntry, in); |
| 99 | +// } catch (IOException e) { |
| 100 | +// throw new Zip4jvmException(e); |
| 101 | +// } |
| 102 | +// }); |
| 103 | + |
| 104 | + tasks.add(task); |
| 105 | + } |
| 106 | + |
| 107 | + tasks.forEach(CompletableFuture::join); |
| 108 | + } finally { |
| 109 | + threadLocalDataInput.release(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + protected Executor createExecutor() { |
| 114 | + AtomicInteger counter = new AtomicInteger(); |
| 115 | + |
| 116 | + ForkJoinPool.ForkJoinWorkerThreadFactory factory = pool -> { |
| 117 | + ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool); |
| 118 | + thread.setName(String.format("zip4jvm-extract-%02d", counter.incrementAndGet())); |
| 119 | + return thread; |
| 120 | + }; |
| 121 | + |
| 122 | + return new ForkJoinPool(Runtime.getRuntime().availableProcessors(), factory, null, false); |
| 123 | + } |
| 124 | + |
| 125 | + protected void extractEntry(Path file, ZipEntry zipEntry, ConsecutiveAccessDataInput in) throws IOException { |
| 126 | + in.seekForward(zipEntry.getLocalFileHeaderAbsOffs()); |
| 127 | + |
| 128 | + if (zipEntry.isSymlink()) |
| 129 | + extractSymlink(file, zipEntry, in); |
| 130 | + else if (zipEntry.isDirectory()) |
| 131 | + extractEmptyDirectory(file); |
| 132 | + else |
| 133 | + extractRegularFile(file, zipEntry, in); |
| 134 | + |
| 135 | + // TODO attributes for directory should be set at the end (under Posix, it could have less privileges) |
| 136 | + setFileAttributes(file, zipEntry); |
| 137 | + setFileLastModifiedTime(file, zipEntry); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments