Example #1
0
        public Stream WriteToStream(string entryPath, DateTime?modificationTime, string comment, string password = null, Crc32?crc = null, CompressionInfo compressionInfo = null)
        {
            entryPath        = NormalizeFilename(entryPath);
            modificationTime = modificationTime ?? DateTime.Now;
            comment          = comment ?? string.Empty;

            var  explicitPassword = password ?? this.password;
            bool doEncryption     = !String.IsNullOrEmpty(explicitPassword);

            var entry = new ZipCentralDirectoryEntry()
            {
                Comment          = comment,
                FileName         = entryPath,
                ModificationTime = modificationTime,
                HeaderOffset     = (uint)streamPosition,
                IsEncrypted      = doEncryption
            };

            var headersize = (uint)WriteHeader(entryPath, modificationTime, doEncryption, compressionInfo);

            Stream outputStream;

            if (doEncryption)
            {
                byte[] encryptionHeader;
                PkwareTraditionalEncryptionData pkwareTraditionalEncryptionData;
                if (OutputStream.CanSeek)
                {
                    if (crc.HasValue)
                    {
                        pkwareTraditionalEncryptionData = PkwareTraditionalEncryptionData.ForWrite(explicitPassword, (uint)(int)crc, out encryptionHeader);
                    }
                    else
                    {
                        throw new ArgumentNullException(nameof(crc), "Crc32 must be provided when password is set and target archive stream supports seeking");
                    }
                }
                else
                {
                    pkwareTraditionalEncryptionData = PkwareTraditionalEncryptionData.ForWrite(explicitPassword, modificationTime, out encryptionHeader);
                }
                OutputStream.Write(encryptionHeader, 0, encryptionHeader.Length);
                entry.Compressed += (uint)encryptionHeader.Length;
                outputStream      = new PkwareTraditionalCryptoStream(OutputStream, pkwareTraditionalEncryptionData, CryptoMode.Encrypt);
            }
            else
            {
                outputStream = OutputStream;
            }

            streamPosition += headersize;
            return(new ZipWritingStream(this, outputStream, entry, compressionInfo));
        }
Example #2
0
        internal PkwareTraditionalEncryptionData ComposeEncryptionData(Stream archiveStream)
        {
            if (archiveStream is null)
            {
                throw new ArgumentNullException(nameof(archiveStream));
            }

            var buffer = new byte[12];

            archiveStream.ReadFully(buffer);

            PkwareTraditionalEncryptionData encryptionData = PkwareTraditionalEncryptionData.ForRead(Password !, this, buffer);

            return(encryptionData);
        }