-
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 dffd0bb
Showing
6 changed files
with
221 additions
and
70 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
140 changes: 140 additions & 0 deletions
140
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,140 @@ | ||
/* | ||
* 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.exception.Zip4jvmException; | ||
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 java.io.IOException; | ||
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.ForkJoinPool; | ||
import java.util.concurrent.ForkJoinWorkerThread; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* @author Oleg Cherednik | ||
* @since 28.12.2024 | ||
*/ | ||
public class UnzipExtractAsyncEngine extends UnzipExtractEngine { | ||
|
||
private static final int MIN_ENTRIES_AMOUNT = 100; | ||
|
||
public UnzipExtractAsyncEngine(PasswordProvider passwordProvider, ZipModel zipModel) { | ||
super(passwordProvider, zipModel); | ||
} | ||
|
||
// ---------- UnzipExtractEngine ---------- | ||
|
||
@Override | ||
protected void extractEntry(Path dstDir, Map<String, String> map) { | ||
if (map == null ? zipModel.getTotalEntries() <= MIN_ENTRIES_AMOUNT : map.size() <= MIN_ENTRIES_AMOUNT) | ||
super.extractEntry(dstDir, map); | ||
else | ||
extractEntryAsync(dstDir, map); | ||
} | ||
|
||
// ---------- | ||
|
||
protected void extractEntryAsync(Path dstDir, Map<String, String> map) { | ||
List<CompletableFuture<Void>> tasks = new LinkedList<>(); | ||
Iterator<ZipEntry> it = zipModel.absOffsAscIterator(); | ||
DataInputThreadLocal<ConsecutiveAccessDataInput> threadLocalDataInput = | ||
new DataInputThreadLocal<>(this::createConsecutiveDataInput); | ||
|
||
try { | ||
Executor executor = createExecutor(); | ||
|
||
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 = CompletableFuture.runAsync( | ||
() -> { | ||
try { | ||
ConsecutiveAccessDataInput in = threadLocalDataInput.get(); | ||
extractEntry(file, zipEntry, in); | ||
} catch (IOException e) { | ||
throw new Zip4jvmException(e); | ||
} | ||
}, | ||
executor); | ||
|
||
// CompletableFuture<Void> task = CompletableFuture.runAsync(() -> { | ||
// try { | ||
// ConsecutiveAccessDataInput in = threadLocalDataInput.get(); | ||
// extractEntry(file, zipEntry, in); | ||
// } catch (IOException e) { | ||
// throw new Zip4jvmException(e); | ||
// } | ||
// }); | ||
|
||
tasks.add(task); | ||
} | ||
|
||
tasks.forEach(CompletableFuture::join); | ||
} finally { | ||
threadLocalDataInput.release(); | ||
} | ||
} | ||
|
||
protected Executor 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 void extractEntry(Path file, ZipEntry zipEntry, ConsecutiveAccessDataInput in) throws IOException { | ||
in.seekForward(zipEntry.getLocalFileHeaderAbsOffs()); | ||
|
||
if (zipEntry.isSymlink()) | ||
extractSymlink(file, zipEntry, in); | ||
else if (zipEntry.isDirectory()) | ||
extractEmptyDirectory(file); | ||
else | ||
extractRegularFile(file, zipEntry, in); | ||
|
||
// TODO attributes for directory should be set at the end (under Posix, it could have less privileges) | ||
setFileAttributes(file, zipEntry); | ||
setFileLastModifiedTime(file, zipEntry); | ||
} | ||
|
||
} |
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