Skip to content

Commit

Permalink
#20 Extract entries async
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Dec 28, 2024
1 parent 41d6959 commit dffd0bb
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 70 deletions.
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import ru.olegcherednik.zip4jvm.model.password.PasswordProvider;
import ru.olegcherednik.zip4jvm.model.settings.UnzipSettings;
import ru.olegcherednik.zip4jvm.model.src.SrcZip;
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -47,7 +47,7 @@ public final class UnzipEngine implements ZipFile.Reader {
public UnzipEngine(SrcZip srcZip, UnzipSettings settings) {
PasswordProvider passwordProvider = settings.getPasswordProvider();
zipModel = ZipModelBuilder.read(srcZip, settings.getCharsetCustomizer(), passwordProvider);
unzipExtractEngine = new UnzipExtractEngine(passwordProvider, zipModel);
unzipExtractEngine = new UnzipExtractAsyncEngine(passwordProvider, zipModel);
}

// ---------- ZipFile.Reader ----------
Expand Down Expand Up @@ -105,9 +105,11 @@ public ZipFile.Entry next() {
};
}

public static RandomAccessDataInput createRandomAccessDataInput(SrcZip srcZip) throws IOException {
return srcZip.isSolid() ? new SolidRandomAccessDataInput(srcZip)
: new SplitRandomAccessDataInput(srcZip);
// ---------- static ----------

public static RandomAccessDataInput createRandomAccessDataInput(SrcZip srcZip) {
return Quietly.doRuntime(() -> srcZip.isSolid() ? new SolidRandomAccessDataInput(srcZip)
: new SplitRandomAccessDataInput(srcZip));
}

}
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import ru.olegcherednik.zip4jvm.model.password.PasswordProvider;
import ru.olegcherednik.zip4jvm.model.src.SrcZip;
import ru.olegcherednik.zip4jvm.utils.ZipUtils;
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;
import ru.olegcherednik.zip4jvm.utils.time.DosTimestampConverterUtils;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -105,24 +106,23 @@ protected List<ZipEntry> getEntriesByPrefix(String prefix) {
.collect(Collectors.toList());
}

// ----------

protected void extractEntry(Path dstDir, Map<String, String> map) {
try (ConsecutiveAccessDataInput in = createConsecutiveDataInput(zipModel.getSrcZip())) {
try (ConsecutiveAccessDataInput in = createConsecutiveDataInput()) {
Iterator<ZipEntry> it = zipModel.absOffsAscIterator();

while (it.hasNext()) {
ZipEntry zipEntry = it.next();

if (map == null || map.containsKey(zipEntry.getFileName())) {
in.seekForward(zipEntry.getLocalFileHeaderAbsOffs());
if (map != null && !map.containsKey(zipEntry.getFileName()))
continue;

in.seekForward(zipEntry.getLocalFileHeaderAbsOffs());

String fileName = Optional.ofNullable(map)
.map(m -> m.get(zipEntry.getFileName()))
.orElse(zipEntry.getFileName());
Path file = dstDir.resolve(fileName);
extractEntry(file, zipEntry, in);
}
String fileName = Optional.ofNullable(map)
.map(m -> m.get(zipEntry.getFileName()))
.orElse(zipEntry.getFileName());
Path file = dstDir.resolve(fileName);
extractEntry(file, zipEntry, in);
}
} catch (IOException e) {
throw new Zip4jvmException(e);
Expand Down Expand Up @@ -164,12 +164,23 @@ protected void extractRegularFile(Path file, ZipEntry zipEntry, DataInput in) th
ZipUtils.copyLarge(zipEntry.createInputStream(in), getOutputStream(file));
}

// ---------- static ----------

public ConsecutiveAccessDataInput createConsecutiveDataInput() {
return Quietly.doRuntime(() -> {
SrcZip srcZip = zipModel.getSrcZip();

return srcZip.isSolid() ? new SolidConsecutiveAccessDataInput(srcZip)
: new SplitConsecutiveAccessDataInput(srcZip);
});
}

protected static void setFileAttributes(Path path, ZipEntry zipEntry) throws IOException {
if (zipEntry.getExternalFileAttributes() != null)
zipEntry.getExternalFileAttributes().apply(path);
}

private static void setFileLastModifiedTime(Path path, ZipEntry zipEntry) throws IOException {
protected static void setFileLastModifiedTime(Path path, ZipEntry zipEntry) throws IOException {
long lastModifiedTime = DosTimestampConverterUtils.dosToJavaTime(zipEntry.getLastModifiedTime());
Files.setLastModifiedTime(path, FileTime.fromMillis(lastModifiedTime));
}
Expand All @@ -184,12 +195,4 @@ protected static OutputStream getOutputStream(Path file) throws IOException {
return Files.newOutputStream(file);
}

// ---------- static ----------

public static ConsecutiveAccessDataInput createConsecutiveDataInput(SrcZip srcZip) throws IOException {
return srcZip.isSolid() ? new SolidConsecutiveAccessDataInput(srcZip)
: new SplitConsecutiveAccessDataInput(srcZip);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class SolidConsecutiveAccessDataInput extends BaseConsecutiveAccessDataIn
private final InputStream in;

public SolidConsecutiveAccessDataInput(SrcZip srcZip) throws IOException {
System.out.println(Thread.currentThread().getName());
byteOrder = srcZip.getByteOrder();
in = new BufferedInputStream(Files.newInputStream(srcZip.getDiskByNo(0).getPath()));
}
Expand Down
53 changes: 8 additions & 45 deletions src/test/java/ru/olegcherednik/zip4jvm/Foo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
*/
package ru.olegcherednik.zip4jvm;

import ru.olegcherednik.zip4jvm.model.settings.ZipInfoSettings;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

/**
* @author Oleg Cherednik
Expand All @@ -34,51 +31,17 @@ public class Foo {

public static void main(String[] args) throws IOException {
final long timeFrom = System.currentTimeMillis();
int[][] token = new int[3][3];

// Path zip = Paths.get("d:/zip4jvm/zip64/split/ferdinand.zip");
// Path zip = Paths.get("d:/zip4jvm/aaa/split/ducati.zip");

// Path zip = Paths.get("d:/zip4jvm/aaa/ducati-panigale-1199.zip");
// Path zip = Paths.get("d:/zip4jvm/aaa/ducati-panigale-1199-ecd.zip");
// Path zip = Paths.get("d:/zip4jvm/aaa/bikes.zip");
// Path zip = Paths.get("d:/zip4jvm/aaa/ducati-panigale-1199-dcl.zip");
// Path zip = Paths.get("d:/zip4jvm/aaa/app.apk");
// Path zip = Paths.get("d:/zip4jvm/aaa/android.apk");

// Path zip = Paths.get("d:/zip4jvm/aaa/ducati-panigale-1199.zip");
// Path zip = Paths.get("d:/zip4jvm/zip64/bzip2-aes256-strong.zip");

// Path zip = Paths.get("d:/zip4jvm/zip64/bzip2-aes256-strong.zip");
// Path zip = Paths.get("d:/Programming/GitHub/zip4jvm/src/test/resources/secure-zip/strong/store_solid_aes256_strong_ecd.zip");
Path zip = Paths.get("d:/zip4jvm/zip64/multi/aes_10k.zip");
Path dstDir = Paths.get("d:/zip4jvm/zip64/multi/out");

//Path zip = Paths.get("d:/zip4jvm/zip64/src.zip");
// Path zip = Paths.get("d:/zip4jvm/scd/aes256bit.zip");
// Path zip = Paths.get("d:/zip4jvm/scd/P1AA4B3C.zip");
Path zip = Paths.get("d:/zip4jvm/scd/onetwo.zip");
// Path zip = Paths.get("D:/Programming/GitHub/zip4jvm/src/test/resources/symlink/win/unique-symlink-target.zip");
Path dstDir = Paths.get("d:/zip4jvm/scd/xxx");

// ZipIt.zip(zip).settings(settings).add(dirSrcData);


// for (Path zip : Arrays.asList(zip1, zip2)) {
// System.out.println(zip);
// UnzipIt.zip(zip).dstDir(dstDir)
// .settings(UnzipSettings.builder()
// .password(password)
// .build())
// .extract();
// ZipInfo.zip(zip).password("1".toCharArray()).printShortInfo();
ZipInfo.zip(zip)
.settings(ZipInfoSettings.builder()
.copyPayload(true)
.readEntries(true)
.build())
.password("1".toCharArray())
.decompose(Paths.get(dstDir.toString(), zip.getFileName().toString()));
UnzipIt.zip(zip).dstDir(dstDir).extract();

final long timeTo = System.currentTimeMillis();
System.out.format("Time: %d sec", TimeUnit.MILLISECONDS.toSeconds(timeTo - timeFrom));
long millis = timeTo - timeFrom;
long minutes = (millis / 1000) / 60;
int seconds = (int) ((millis / 1000) % 60);
System.out.format("Time: %02d:%02d", minutes, seconds);
}

}

0 comments on commit dffd0bb

Please sign in to comment.