// A specific constructor to allow decompression of Deflate64 internal DeflateManagedStream(Stream stream, ZipArchiveEntry.CompressionMethodValues method) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException(SR.NotSupported_UnreadableStream, nameof(stream)); } InitializeInflater(stream, false, null, method); }
private Stream GetDataDecompressor(Stream compressedStreamToRead) { Stream deflateStream = null; ZipArchiveEntry.CompressionMethodValues compressionMethod = this.CompressionMethod; if (compressionMethod == ZipArchiveEntry.CompressionMethodValues.Stored || compressionMethod != ZipArchiveEntry.CompressionMethodValues.Deflate) { deflateStream = compressedStreamToRead; } else { deflateStream = new DeflateStream(compressedStreamToRead, CompressionMode.Decompress); } return(deflateStream); }
// A specific constructor to allow decompression of Deflate64 internal DeflateManagedStream(Stream stream, ZipArchiveEntry.CompressionMethodValues method, long uncompressedSize = -1) { ArgumentNullException.ThrowIfNull(stream); if (!stream.CanRead) { throw new ArgumentException(SR.NotSupported_UnreadableStream, nameof(stream)); } Debug.Assert(method == ZipArchiveEntry.CompressionMethodValues.Deflate64); _inflater = new InflaterManaged(method == ZipArchiveEntry.CompressionMethodValues.Deflate64, uncompressedSize); _stream = stream; _buffer = new byte[DefaultBufferSize]; }
private void WriteLocalFileHeaderAndDataIfNeeded() { if (this._storedUncompressedData != null || this._compressedBytes != null) { if (this._storedUncompressedData != null) { this._uncompressedSize = this._storedUncompressedData.Length; using (Stream destination = new ZipArchiveEntry.DirectToArchiveWriterStream(this.GetDataCompressor(this._archive.ArchiveStream, true, (EventHandler)null), this)) { this._storedUncompressedData.Seek(0L, SeekOrigin.Begin); ZipHelper.CopyStreamTo(this._storedUncompressedData, destination); this._storedUncompressedData.Close(); this._storedUncompressedData = null; } } else { if (this._uncompressedSize == 0L) { this.CompressionMethod = ZipArchiveEntry.CompressionMethodValues.Stored; } this.WriteLocalFileHeader(false); using (MemoryStream memoryStream = new MemoryStream(this._compressedBytes)) { ZipHelper.CopyStreamTo(memoryStream, this._archive.ArchiveStream); } } } else { if (this._archive.Mode != ZipArchiveMode.Update && this._everOpenedForWrite) { return; } this._everOpenedForWrite = true; this.WriteLocalFileHeader(true); } }
/// <summary> /// Sets up this DeflateManagedStream to be used for Inflation/Decompression /// </summary> internal void InitializeInflater(Stream stream, bool leaveOpen, IFileFormatReader reader = null, ZipArchiveEntry.CompressionMethodValues method = ZipArchiveEntry.CompressionMethodValues.Deflate) { Debug.Assert(stream != null); Debug.Assert(method == ZipArchiveEntry.CompressionMethodValues.Deflate || method == ZipArchiveEntry.CompressionMethodValues.Deflate64); if (!stream.CanRead) { throw new ArgumentException(SR.NotSupported_UnreadableStream, nameof(stream)); } _inflater = new InflaterManaged(reader, method == ZipArchiveEntry.CompressionMethodValues.Deflate64 ? true : false); _stream = stream; _mode = CompressionMode.Decompress; _leaveOpen = leaveOpen; _buffer = new byte[DefaultBufferSize]; }
private bool WriteLocalFileHeader(bool isEmptyFile) { BinaryWriter binaryWriter = new BinaryWriter(this._archive.ArchiveStream); Zip64ExtraField zip64ExtraField = new Zip64ExtraField(); bool flag = false; uint num1; uint num2; if (isEmptyFile) { this.CompressionMethod = ZipArchiveEntry.CompressionMethodValues.Stored; num1 = 0U; num2 = 0U; } else if (this._archive.Mode == ZipArchiveMode.Create && !this._archive.ArchiveStream.CanSeek && !isEmptyFile) { this._generalPurposeBitFlag = this._generalPurposeBitFlag | ZipArchiveEntry.BitFlagValues.DataDescriptor; flag = false; num1 = 0U; num2 = 0U; } else if (this.SizesTooLarge()) { flag = true; num1 = uint.MaxValue; num2 = uint.MaxValue; zip64ExtraField.CompressedSize = new long?(this._compressedSize); zip64ExtraField.UncompressedSize = new long?(this._uncompressedSize); this.VersionToExtractAtLeast(ZipVersionNeededValues.Zip64); } else { flag = false; num1 = (uint)this._compressedSize; num2 = (uint)this._uncompressedSize; } this._offsetOfLocalHeader = binaryWriter.BaseStream.Position; int num3 = (flag ? (int)zip64ExtraField.TotalSize : 0) + (this._lhUnknownExtraFields != null ? ZipGenericExtraField.TotalSize(this._lhUnknownExtraFields) : 0); ushort num4; if (num3 > (int)ushort.MaxValue) { num4 = flag ? zip64ExtraField.TotalSize : (ushort)0; this._lhUnknownExtraFields = null; } else { num4 = (ushort)num3; } binaryWriter.Write(67324752U); binaryWriter.Write((ushort)this._versionToExtract); binaryWriter.Write((ushort)this._generalPurposeBitFlag); binaryWriter.Write((ushort)this.CompressionMethod); binaryWriter.Write(ZipHelper.DateTimeToDosTime(this._lastModified.DateTime)); binaryWriter.Write(this._crc32); binaryWriter.Write(num1); binaryWriter.Write(num2); binaryWriter.Write((ushort)this._storedEntryNameBytes.Length); binaryWriter.Write(num4); binaryWriter.Write(this._storedEntryNameBytes); if (flag) { zip64ExtraField.WriteBlock(this._archive.ArchiveStream); } if (this._lhUnknownExtraFields != null) { ZipGenericExtraField.WriteAllBlocks(this._lhUnknownExtraFields, this._archive.ArchiveStream); } return(flag); }