private CheckSumAndSizeWriteStream GetDataCompressor(Stream backingStream, bool leaveBackingStreamOpen, EventHandler onClose)
{
//stream stack: backingStream -> DeflateStream -> CheckSumWriteStream
//we should always be compressing with deflate. Stored is used for empty files, but we don't actually
//call through this function for that - we just write the stored value in the header
Debug.Assert(CompressionMethod == CompressionMethodValues.Deflate);
Stream compressorStream = _compressionLevel.HasValue
? new DeflateStream(backingStream, _compressionLevel.Value, leaveBackingStreamOpen)
: new DeflateStream(backingStream, CompressionMode.Compress, leaveBackingStreamOpen);
bool isIntermediateStream = true;
bool leaveCompressorStreamOpenOnClose = leaveBackingStreamOpen && !isIntermediateStream;
var checkSumStream = new CheckSumAndSizeWriteStream(
compressorStream,
backingStream,
leaveCompressorStreamOpenOnClose,
this,
onClose,
(long initialPosition, long currentPosition, uint checkSum, Stream backing, ZipArchiveEntry thisRef, EventHandler closeHandler) =>
{
thisRef._crc32 = checkSum;
thisRef._uncompressedSize = currentPosition;
thisRef._compressedSize = backing.Position - initialPosition;
if (closeHandler != null)
closeHandler(thisRef, EventArgs.Empty);
});
return checkSumStream;
}