Skip to content

Commit

Permalink
#106 Do not use CentralDirectory when extract all
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Dec 22, 2024
1 parent 7ac52e1 commit 5ba0100
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
*/
public interface RandomAccessDataInput extends DataInput {

void seek(int diskNo, long diskOffs) throws IOException;

void seek(long absOffs) throws IOException;

void seek(String id) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ public long skip(long bytes) throws IOException {
return in.skipBytes((int) Math.min(Integer.MAX_VALUE, bytes));
}

// ---------- RandomAccessDataInput ----------

@Override
public void seek(int diskNo, long diskOffs) throws IOException {
seek(srcZip.getAbsOffs(diskNo, diskOffs));
}

// ---------- ReadBuffer ----------

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,11 @@ public void close() throws IOException {
super.close();
}

// ---------- DataInputFile ----------

@Override
public void seek(int diskNo, long diskOffs) throws IOException {
seek(srcZip.getAbsOffs(diskNo, diskOffs));
}

// ---------- Object ----------

@Override
public String toString() {
return PathUtils.getOffsStr(getAbsOffs());
return PathUtils.getOffsStr(getAbsOffs(), getDiskOffs(), disk.getNo());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected EndCentralDirectoryReader getEndCentralDirectoryReader() {

@Override
protected Zip64Reader getZip64Reader() {
return new Zip64Reader();
return new Zip64Reader(srcZip);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected EndCentralDirectoryReader getEndCentralDirectoryReader() {

@Override
protected Zip64Reader getZip64Reader() {
return new BlockZip64Reader(zip64Block);
return new BlockZip64Reader(srcZip, zip64Block);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@
import ru.olegcherednik.zip4jvm.io.readers.zip64.ExtensibleDataSectorReader;
import ru.olegcherednik.zip4jvm.io.readers.zip64.Zip64Reader;
import ru.olegcherednik.zip4jvm.model.block.Zip64Block;

import lombok.RequiredArgsConstructor;
import ru.olegcherednik.zip4jvm.model.src.SrcZip;

/**
* @author Oleg Cherednik
* @since 20.10.2019
*/
@RequiredArgsConstructor
public final class BlockZip64Reader extends Zip64Reader {

private final Zip64Block zip64Block;

@SuppressWarnings("MethodParameterNamingConvention")
public BlockZip64Reader(SrcZip srcZip, Zip64Block zip64Block) {
super(srcZip);
this.zip64Block = zip64Block;
}

@Override
protected EndCentralDirectoryLocatorReader getEndCentralDirectoryLocatorReader() {
return new BlockEndCentralDirectoryLocatorReader(zip64Block.getEndCentralDirectoryLocatorBlock());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@
import ru.olegcherednik.zip4jvm.io.in.DataInput;
import ru.olegcherednik.zip4jvm.io.in.file.random.RandomAccessDataInput;
import ru.olegcherednik.zip4jvm.model.Zip64;
import ru.olegcherednik.zip4jvm.model.src.SrcZip;

import lombok.RequiredArgsConstructor;

import java.io.IOException;

import static ru.olegcherednik.zip4jvm.utils.ValidationUtils.requireLessOrEqual;

/**
* @author Oleg Cherednik
* @since 22.08.2019
*/
@RequiredArgsConstructor
public class Zip64Reader {

protected final SrcZip srcZip;

public final Zip64 read(RandomAccessDataInput in) throws IOException {
return read(in, false);
}
Expand All @@ -47,7 +55,7 @@ private Zip64 read(RandomAccessDataInput in, boolean locatorOnly) throws IOExcep
Zip64.ExtensibleDataSector extensibleDataSector = null;

if (!locatorOnly) {
findEndCentralDirectorySignature(locator, in);
findEndCentralDirectorySignature(srcZip, locator, in);

endCentralDirectory = getEndCentralDirectoryReader().read(in);
extensibleDataSector = readExtensibleDataSector(endCentralDirectory, in);
Expand Down Expand Up @@ -76,9 +84,11 @@ private Zip64.ExtensibleDataSector readExtensibleDataSector(Zip64.EndCentralDire
return extensibleDataSector;
}

private static void findEndCentralDirectorySignature(Zip64.EndCentralDirectoryLocator locator,
private static void findEndCentralDirectorySignature(SrcZip srcZip,
Zip64.EndCentralDirectoryLocator locator,
RandomAccessDataInput in) throws IOException {
in.seek((int) locator.getMainDiskNo(), locator.getEndCentralDirectoryRelativeOffs());
requireLessOrEqual(locator.getMainDiskNo(), Integer.MAX_VALUE, "zip64.locator.mainDisk");
in.seek(srcZip.getAbsOffs((int) locator.getMainDiskNo(), locator.getEndCentralDirectoryRelativeOffs()));
long offs = in.getAbsOffs();

if (!in.isDwordSignature(Zip64.EndCentralDirectory.SIGNATURE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void shouldReadStreamWhenUsingDataInput() throws IOException {

assertThat(in.readQword()).isEqualTo(0x0E0D0C0B0A090807L);
assertThat(in.getAbsOffs()).isEqualTo(14);
assertThat(in.toString()).isEqualTo("offs: 14 (0xe)");
assertThat(in.toString()).isEqualTo("absOffs: 14 (0xe) | diskOffs: 14 (0xe) | disk: 0");

assertThat(in.skip(2)).isEqualTo(2);
assertThat(in.getAbsOffs()).isEqualTo(16);
Expand Down

0 comments on commit 5ba0100

Please sign in to comment.