private static void DecompressAttachment(IEnumerable <PartInfo> payloadInfo, Attachment attachment) { PartInfo referenced = payloadInfo.FirstOrDefault(attachment.Matches); if (referenced == null) { throw new InvalidDataException( $"Can't decompress Attachment {attachment.Id} because no matching PartInfo was found"); } if (String.IsNullOrWhiteSpace(referenced.MimeType)) { throw new InvalidDataException( $"Cannot decompress attachment {attachment.Id}: MimeType is not specified in referenced <PartInfo/> element"); } if (referenced.MimeType.IndexOf("/", StringComparison.OrdinalIgnoreCase) < 0) { throw new InvalidDataException( $"Cannot decompress attachment {attachment.Id}: Invalid MimeType {referenced.MimeType} in referenced <PartInfo/> element"); } attachment.ResetContentPosition(); long unzipLength = StreamUtilities.DetermineOriginalSizeOfCompressedStream(attachment.Content); VirtualStream outputStream = VirtualStream.Create( unzipLength > -1 ? unzipLength : VirtualStream.ThresholdMax); if (unzipLength > 0) { outputStream.SetLength(unzipLength); } Stream decompressed = DecompressStream(attachment.Content); referenced.CompressionType = CompressionType; attachment.CompressionType = CompressionType; attachment.MimeType = referenced.MimeType; attachment.UpdateContent(decompressed, referenced.MimeType); }
private static Stream DecompressStream(Stream input) { long unzipLength = StreamUtilities.DetermineOriginalSizeOfCompressedStream(input); VirtualStream outputStream = VirtualStream.Create( unzipLength > -1 ? unzipLength : VirtualStream.ThresholdMax); if (unzipLength > 0) { outputStream.SetLength(unzipLength); } using (var gzipCompression = new GZipStream(input, CompressionMode.Decompress, true)) { gzipCompression.CopyTo(outputStream); outputStream.Position = 0; return(outputStream); } }