From 0990b06cc9eca5372d0ab03e4df45dfe4ef9921e Mon Sep 17 00:00:00 2001 From: Anders Gardebring Date: Tue, 25 Apr 2017 12:48:56 +0200 Subject: [PATCH] Create new TransferTo method and pass Entry and IReaderExtractionListener instead of passing an action lambda. --- src/SharpCompress/Readers/AbstractReader.cs | 2 +- src/SharpCompress/Utility.cs | 32 ++++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/SharpCompress/Readers/AbstractReader.cs b/src/SharpCompress/Readers/AbstractReader.cs index 85289cb4..ecfeeb4b 100644 --- a/src/SharpCompress/Readers/AbstractReader.cs +++ b/src/SharpCompress/Readers/AbstractReader.cs @@ -191,7 +191,7 @@ namespace SharpCompress.Readers { using (Stream s = OpenEntryStream()) { - s.TransferTo(writeStream, (sizeTransferred, iterations) => streamListener.FireEntryExtractionProgress(Entry, sizeTransferred, iterations)); + s.TransferTo(writeStream, Entry, streamListener); } } diff --git a/src/SharpCompress/Utility.cs b/src/SharpCompress/Utility.cs index 00952846..0486a5fd 100644 --- a/src/SharpCompress/Utility.cs +++ b/src/SharpCompress/Utility.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using SharpCompress.Readers; namespace SharpCompress { @@ -229,22 +230,45 @@ namespace SharpCompress return DosDateToDateTime((UInt32)iTime); } - public static long TransferTo(this Stream source, Stream destination, Action action = null) + public static long TransferTo(this Stream source, Stream destination) { - byte[] array = new byte[81920]; + byte[] array = GetTransferByteArray(); + int count; + long total = 0; + while (ReadTransferBlock(source, array, out count)) + { + total += count; + destination.Write(array, 0, count); + } + return total; + } + + public static long TransferTo(this Stream source, Stream destination, Common.Entry entry, IReaderExtractionListener readerExtractionListener) + { + byte[] array = GetTransferByteArray(); int count; var iterations = 0; long total = 0; - while ((count = source.Read(array, 0, array.Length)) != 0) + while (ReadTransferBlock(source, array, out count)) { total += count; destination.Write(array, 0, count); iterations++; - action?.Invoke(total, iterations); + readerExtractionListener.FireEntryExtractionProgress(entry, total, iterations); } return total; } + private static bool ReadTransferBlock(Stream source, byte[] array, out int count) + { + return (count = source.Read(array, 0, array.Length)) != 0; + } + + private static byte[] GetTransferByteArray() + { + return new byte[81920]; + } + public static bool ReadFully(this Stream stream, byte[] buffer) { int total = 0;