ICSharpCode.SharpZipLib.Zip.ZipInputStream.InitialRead C# (CSharp) Method

InitialRead() private method

Perform the initial read on an entry which may include reading encryption headers and setting up inflation.
private InitialRead ( byte destination, int offset, int count ) : int
destination byte The destination to fill with data read.
offset int The offset to start reading at.
count int The maximum number of bytes to read.
return int
        int InitialRead(byte[] destination, int offset, int count)
        {
            if (!CanDecompressEntry) {
                throw new ZipException("Library cannot extract this entry. Version required is (" + entry.Version + ")");
            }

            // Handle encryption if required.
            if (entry.IsCrypted) {
                if (password == null) {
                    throw new ZipException("No password set.");
                }

                // Generate and set crypto transform...
                var managed = new PkzipClassicManaged();
                byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));

                inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);

                byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
                inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize);

                if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue) {
                    throw new ZipException("Invalid password");
                }

                if (csize >= ZipConstants.CryptoHeaderSize) {
                    csize -= ZipConstants.CryptoHeaderSize;
                } else if ((entry.Flags & (int)GeneralBitFlags.Descriptor) == 0) {
                    throw new ZipException(string.Format("Entry compressed size {0} too small for encryption", csize));
                }
            } else {
                inputBuffer.CryptoTransform = null;
            }

            if ((csize > 0) || ((flags & (int)GeneralBitFlags.Descriptor) != 0)) {
                if ((method == (int)CompressionMethod.Deflated) && (inputBuffer.Available > 0)) {
                    inputBuffer.SetInflaterInput(inf);
                }

                internalReader = new ReadDataHandler(BodyRead);
                return BodyRead(destination, offset, count);
            } else {
                internalReader = new ReadDataHandler(ReadingNotAvailable);
                return 0;
            }
        }