-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41d6959
commit 750e213
Showing
7 changed files
with
186 additions
and
78 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/ru/olegcherednik/zip4jvm/engine/unzip/DataInputThreadLocal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ru.olegcherednik.zip4jvm.engine.unzip; | ||
|
||
import ru.olegcherednik.zip4jvm.io.in.DataInput; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* @author Oleg Cherednik | ||
* @since 28.12.2024 | ||
*/ | ||
@RequiredArgsConstructor | ||
public class DataInputThreadLocal<T extends DataInput> extends ThreadLocal<T> { | ||
|
||
private final Supplier<T> dataInputSup; | ||
private final List<T> dataInputs = new CopyOnWriteArrayList<>(); | ||
|
||
public void release() { | ||
dataInputs.forEach(IOUtils::closeQuietly); | ||
dataInputs.clear(); | ||
} | ||
|
||
// ---------- ThreadLocal ---------- | ||
|
||
@Override | ||
public T get() { | ||
T in = super.get(); | ||
|
||
if (in == null) { | ||
in = dataInputSup.get(); | ||
set(in); | ||
dataInputs.add(in); | ||
} | ||
|
||
return in; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/main/java/ru/olegcherednik/zip4jvm/engine/unzip/UnzipExtractAsyncEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* 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 ru.olegcherednik.zip4jvm.engine.unzip; | ||
|
||
import ru.olegcherednik.zip4jvm.io.in.file.consecutive.ConsecutiveAccessDataInput; | ||
import ru.olegcherednik.zip4jvm.model.ZipModel; | ||
import ru.olegcherednik.zip4jvm.model.entry.ZipEntry; | ||
import ru.olegcherednik.zip4jvm.model.password.PasswordProvider; | ||
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly; | ||
import ru.olegcherednik.zip4jvm.utils.quitely.functions.RunnableWithException; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.concurrent.ForkJoinWorkerThread; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* @author Oleg Cherednik | ||
* @since 28.12.2024 | ||
*/ | ||
public class UnzipExtractAsyncEngine extends UnzipExtractEngine { | ||
|
||
public UnzipExtractAsyncEngine(PasswordProvider passwordProvider, ZipModel zipModel) { | ||
super(passwordProvider, zipModel); | ||
} | ||
|
||
// ---------- UnzipExtractEngine ---------- | ||
|
||
@Override | ||
protected void extractEntry(Path dstDir, Map<String, String> map) { | ||
List<CompletableFuture<Void>> tasks = new LinkedList<>(); | ||
Iterator<ZipEntry> it = zipModel.absOffsAscIterator(); | ||
|
||
DataInputThreadLocal<ConsecutiveAccessDataInput> threadLocalDataInput = | ||
new DataInputThreadLocal<>(this::createConsecutiveDataInput); | ||
ExecutorService executor = createExecutor(); | ||
|
||
try { | ||
while (it.hasNext()) { | ||
ZipEntry zipEntry = it.next(); | ||
|
||
if (map != null && !map.containsKey(zipEntry.getFileName())) | ||
continue; | ||
|
||
String fileName = Optional.ofNullable(map) | ||
.map(m -> m.get(zipEntry.getFileName())) | ||
.orElse(zipEntry.getFileName()); | ||
Path file = dstDir.resolve(fileName); | ||
|
||
CompletableFuture<Void> task = createCompletableFuture( | ||
() -> extractEntry(file, zipEntry, threadLocalDataInput.get()), executor); | ||
|
||
tasks.add(task); | ||
} | ||
|
||
tasks.forEach(CompletableFuture::join); | ||
} finally { | ||
threadLocalDataInput.release(); | ||
executor.shutdown(); | ||
} | ||
} | ||
|
||
protected ExecutorService createExecutor() { | ||
AtomicInteger counter = new AtomicInteger(); | ||
|
||
ForkJoinPool.ForkJoinWorkerThreadFactory factory = pool -> { | ||
ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool); | ||
thread.setName(String.format("zip4jvm-extract-%02d", counter.incrementAndGet())); | ||
return thread; | ||
}; | ||
|
||
return new ForkJoinPool(Runtime.getRuntime().availableProcessors(), factory, null, false); | ||
} | ||
|
||
protected CompletableFuture<Void> createCompletableFuture(RunnableWithException task, Executor executor) { | ||
return CompletableFuture.runAsync(() -> Quietly.doRuntime(task), executor); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters