Create new TransferTo method and pass Entry and IReaderExtractionListener instead of passing an action lambda.

This commit is contained in:
Anders Gardebring
2017-04-25 12:48:56 +02:00
parent e05f9843ba
commit 0990b06cc9
2 changed files with 29 additions and 5 deletions

View File

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

View File

@@ -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<long, int> 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;