private void _SetupStream() { _crcStream= _currentEntry.InternalOpenReader(_password); _LeftToRead = _crcStream.Length; _needSetup = false; }
internal void PrepOutputStream(Stream s, long streamLength, out CountingStream outputCounter, out Stream encryptor, out Stream deflater, out PMDCP.Compression.Zlib.CrcCalculatorStream output) { TraceWriteLine("PrepOutputStream: e({0}) comp({1}) crypto({2}) zf({3})", FileName, CompressionLevel, Encryption, (_container).Name); // Wrap a counting stream around the raw output stream: // This is the last thing that happens before the bits go to the // application-provided stream. outputCounter = new CountingStream(s); // Sometimes the incoming "raw" output stream is already a CountingStream. // Doesn't matter. Wrap it with a counter anyway. We need to count at both levels. if (streamLength != 0L) { // Maybe wrap an encrypting stream around that: // This will happen BEFORE output counting, and AFTER deflation, if encryption // is used. encryptor = MaybeApplyEncryption(outputCounter); // Maybe wrap a DeflateStream around that. // This will happen BEFORE encryption (if any) as we write data out. deflater = MaybeApplyDeflation(encryptor, streamLength); } else { encryptor = deflater = outputCounter; } // Wrap a CrcCalculatorStream around that. // This will happen BEFORE deflation (if any) as we write data out. output = new PMDCP.Compression.Zlib.CrcCalculatorStream(deflater, true); }
private Int32 _ExtractOne(Stream output) { Stream input = this.ArchiveStream; // change for workitem 8098 input.Seek(this.FileDataPosition, SeekOrigin.Begin); // to validate the CRC. Int32 CrcResult = 0; byte[] bytes = new byte[BufferSize]; // The extraction process varies depending on how the entry was stored. // It could have been encrypted, and it coould have been compressed, or both, or // neither. So we need to check both the encryption flag and the compression flag, // and take the proper action in all cases. Int64 LeftToRead = (_CompressionMethod_FromZipFile == (short)CompressionMethod.Deflate) ? this.UncompressedSize : this._CompressedFileDataSize; // Get a stream that either decrypts or not. _inputDecryptorStream = GetExtractDecryptor(input); Stream input3 = GetExtractDecompressor( _inputDecryptorStream ); Int64 bytesWritten = 0; // As we read, we maybe decrypt, and then we maybe decompress. Then we write. using (var s1 = new PMDCP.Compression.Zlib.CrcCalculatorStream(input3)) { while (LeftToRead > 0) { //Console.WriteLine("ExtractOne: LeftToRead {0}", LeftToRead); // Casting LeftToRead down to an int is ok here in the else clause, // because that only happens when it is less than bytes.Length, // which is much less than MAX_INT. int len = (LeftToRead > bytes.Length) ? bytes.Length : (int)LeftToRead; int n = s1.Read(bytes, 0, len); // must check data read - essential for detecting corrupt zip files _CheckRead(n); output.Write(bytes, 0, n); LeftToRead -= n; bytesWritten += n; // fire the progress event, check for cancels OnExtractProgress(bytesWritten, UncompressedSize); if (_ioOperationCanceled) { break; } } CrcResult = s1.Crc; } return CrcResult; }
private Int32 FigureCrc32() { if (_crcCalculated == false) { Stream input = null; // get the original stream: if (this._Source == ZipEntrySource.WriteDelegate) { var output = new PMDCP.Compression.Zlib.CrcCalculatorStream(Stream.Null); // allow the application to write the data this._WriteDelegate(this.FileName, output); _Crc32 = output.Crc; } else if (this._Source == ZipEntrySource.ZipFile) { // nothing to do - the CRC is already set } else { if (this._Source == ZipEntrySource.Stream) { PrepSourceStream(); input = _sourceStream; } else if (this._Source == ZipEntrySource.JitStream) { // allow the application to open the stream if (this._sourceStream == null) _sourceStream = this._OpenDelegate(this.FileName); PrepSourceStream(); input = this._sourceStream; } else if (this._Source == ZipEntrySource.ZipOutputStream) { //throw new InvalidOperationException("you cannot use PKZIP encryption with a ZipOutputStream."); } else { //input = File.OpenRead(LocalFileName); input = File.Open(LocalFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); } var crc32 = new PMDCP.Compression.Zlib.CRC32(); _Crc32 = crc32.GetCrc32(input); if (_sourceStream == null) { input.Close(); #if !NETCF input.Dispose(); #endif } } _crcCalculated = true; } return _Crc32; }