ICSharpCode.SharpZipLib.Zip.ZipFile.CopyBytes C# (CSharp) Method

CopyBytes() private method

private CopyBytes ( ZipUpdate update, Stream destination, Stream source, long bytesToCopy, bool updateCrc ) : void
update ZipUpdate
destination Stream
source Stream
bytesToCopy long
updateCrc bool
return void
        private void CopyBytes(ZipUpdate update, Stream destination, Stream source,
                               long bytesToCopy, bool updateCrc) {
            if (destination==source) {
                throw new InvalidOperationException("Destination and source are the same");
            }

            // NOTE: Compressed size is updated elsewhere.
            var crc=new Crc32();
            byte[] buffer=GetBuffer();

            long targetBytes=bytesToCopy;
            long totalBytesRead=0;

            int bytesRead;
            do {
                int readSize=buffer.Length;

                if (bytesToCopy<readSize) {
                    readSize=(int)bytesToCopy;
                }

                bytesRead=source.Read(buffer, 0, readSize);
                if (bytesRead>0) {
                    if (updateCrc) {
                        crc.Update(buffer, 0, bytesRead);
                    }
                    destination.Write(buffer, 0, bytesRead);
                    bytesToCopy-=bytesRead;
                    totalBytesRead+=bytesRead;
                }
            } while ((bytesRead>0)&&(bytesToCopy>0));

            if (totalBytesRead!=targetBytes) {
                throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", targetBytes,
                                                     totalBytesRead));
            }

            if (updateCrc) {
                update.OutEntry.Crc=crc.Value;
            }
        }