From b2f8fa64d6a6b4da7dad5d3cdffd69a13053f3a4 Mon Sep 17 00:00:00 2001 From: Srikanth Reddy Lingala Date: Wed, 27 May 2020 07:20:41 +0200 Subject: [PATCH] #190 Fix failing test --- .../java/net/lingala/zip4j/headers/HeaderWriter.java | 7 ++++--- .../zip4j/io/outputstream/CountingOutputStream.java | 4 +++- .../outputstream/OutputStreamWithSplitZipSupport.java | 10 ++++++++++ .../zip4j/io/outputstream/SplitOutputStream.java | 4 +++- .../java/net/lingala/zip4j/tasks/RenameFilesTask.java | 3 +-- 5 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 src/main/java/net/lingala/zip4j/io/outputstream/OutputStreamWithSplitZipSupport.java diff --git a/src/main/java/net/lingala/zip4j/headers/HeaderWriter.java b/src/main/java/net/lingala/zip4j/headers/HeaderWriter.java index 91622ca9..252de828 100755 --- a/src/main/java/net/lingala/zip4j/headers/HeaderWriter.java +++ b/src/main/java/net/lingala/zip4j/headers/HeaderWriter.java @@ -18,6 +18,7 @@ import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.io.outputstream.CountingOutputStream; +import net.lingala.zip4j.io.outputstream.OutputStreamWithSplitZipSupport; import net.lingala.zip4j.io.outputstream.SplitOutputStream; import net.lingala.zip4j.model.AESExtraDataRecord; import net.lingala.zip4j.model.ExtraDataRecord; @@ -348,10 +349,10 @@ private void writeZipHeaderBytes(ZipModel zipModel, OutputStream outputStream, b private void processHeaderData(ZipModel zipModel, OutputStream outputStream) throws IOException { int currentSplitFileCounter = 0; - if (outputStream instanceof CountingOutputStream) { + if (outputStream instanceof OutputStreamWithSplitZipSupport) { zipModel.getEndOfCentralDirectoryRecord().setOffsetOfStartOfCentralDirectory( - ((CountingOutputStream) outputStream).getFilePointer()); - currentSplitFileCounter = ((CountingOutputStream) outputStream).getCurrentSplitFileCounter(); + ((OutputStreamWithSplitZipSupport) outputStream).getFilePointer()); + currentSplitFileCounter = ((OutputStreamWithSplitZipSupport) outputStream).getCurrentSplitFileCounter(); } if (zipModel.isZip64Format()) { diff --git a/src/main/java/net/lingala/zip4j/io/outputstream/CountingOutputStream.java b/src/main/java/net/lingala/zip4j/io/outputstream/CountingOutputStream.java index 3de08a18..35df880c 100644 --- a/src/main/java/net/lingala/zip4j/io/outputstream/CountingOutputStream.java +++ b/src/main/java/net/lingala/zip4j/io/outputstream/CountingOutputStream.java @@ -5,7 +5,7 @@ import java.io.IOException; import java.io.OutputStream; -public class CountingOutputStream extends OutputStream { +public class CountingOutputStream extends OutputStream implements OutputStreamWithSplitZipSupport { private OutputStream outputStream; private long numberOfBytesWritten = 0; @@ -30,6 +30,7 @@ public void write(byte[] b, int off, int len) throws IOException { numberOfBytesWritten += len; } + @Override public int getCurrentSplitFileCounter() { if (isSplitZipFile()) { return ((SplitOutputStream) outputStream).getCurrentSplitFileCounter(); @@ -75,6 +76,7 @@ public boolean checkBuffSizeAndStartNextSplitFile(int bufferSize) throws ZipExce return ((SplitOutputStream)outputStream).checkBufferSizeAndStartNextSplitFile(bufferSize); } + @Override public long getFilePointer() throws IOException { if (outputStream instanceof SplitOutputStream) { return ((SplitOutputStream) outputStream).getFilePointer(); diff --git a/src/main/java/net/lingala/zip4j/io/outputstream/OutputStreamWithSplitZipSupport.java b/src/main/java/net/lingala/zip4j/io/outputstream/OutputStreamWithSplitZipSupport.java new file mode 100644 index 00000000..3f307822 --- /dev/null +++ b/src/main/java/net/lingala/zip4j/io/outputstream/OutputStreamWithSplitZipSupport.java @@ -0,0 +1,10 @@ +package net.lingala.zip4j.io.outputstream; + +import java.io.IOException; + +public interface OutputStreamWithSplitZipSupport { + + long getFilePointer() throws IOException; + + int getCurrentSplitFileCounter(); +} diff --git a/src/main/java/net/lingala/zip4j/io/outputstream/SplitOutputStream.java b/src/main/java/net/lingala/zip4j/io/outputstream/SplitOutputStream.java index 8ba2125b..8f7dcf88 100755 --- a/src/main/java/net/lingala/zip4j/io/outputstream/SplitOutputStream.java +++ b/src/main/java/net/lingala/zip4j/io/outputstream/SplitOutputStream.java @@ -30,7 +30,7 @@ import static net.lingala.zip4j.util.FileUtils.getZipFileNameWithoutExtension; import static net.lingala.zip4j.util.InternalZipConstants.MIN_SPLIT_LENGTH; -public class SplitOutputStream extends OutputStream { +public class SplitOutputStream extends OutputStream implements OutputStreamWithSplitZipSupport { private RandomAccessFile raf; private long splitLength; @@ -192,6 +192,7 @@ public void close() throws IOException { raf.close(); } + @Override public long getFilePointer() throws IOException { return raf.getFilePointer(); } @@ -204,6 +205,7 @@ public long getSplitLength() { return splitLength; } + @Override public int getCurrentSplitFileCounter() { return currSplitFileCounter; } diff --git a/src/main/java/net/lingala/zip4j/tasks/RenameFilesTask.java b/src/main/java/net/lingala/zip4j/tasks/RenameFilesTask.java index 179d5ffe..6e0319da 100644 --- a/src/main/java/net/lingala/zip4j/tasks/RenameFilesTask.java +++ b/src/main/java/net/lingala/zip4j/tasks/RenameFilesTask.java @@ -3,7 +3,6 @@ import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.headers.HeaderUtil; import net.lingala.zip4j.headers.HeaderWriter; -import net.lingala.zip4j.io.outputstream.CountingOutputStream; import net.lingala.zip4j.io.outputstream.SplitOutputStream; import net.lingala.zip4j.model.FileHeader; import net.lingala.zip4j.model.ZipModel; @@ -48,7 +47,7 @@ protected void executeTask(RenameFilesTaskParameters taskParameters, ProgressMon File temporaryFile = getTemporaryFile(zipModel.getZipFile().getPath()); boolean successFlag = false; try(RandomAccessFile inputStream = new RandomAccessFile(zipModel.getZipFile(), RandomAccessFileMode.WRITE.getValue()); - CountingOutputStream outputStream = new CountingOutputStream(new SplitOutputStream(temporaryFile))) { + SplitOutputStream outputStream = new SplitOutputStream(temporaryFile)) { long currentFileCopyPointer = 0;