/// <summary> /// Truncates the archiveFile represented by the ZipArchiveFile to be empty and returns a Stream that can be used /// to write (binary) data into it. /// </summary> /// <returns>A Stream that can be written on. </returns> public Stream Create() { if (IsReadOnly) { throw new ApplicationException("Archive is ReadOnly"); } if (uncompressedData != null && (uncompressedData.CanWrite || uncompressedData.CanRead)) { throw new ApplicationException("ZipArchiveFile already open."); } compressedData = null; positionOfCompressedDataInArchive = 0; compressedLength = 0; uncompressedData = new RepairedMemoryStream(256); return(uncompressedData); }
internal void WriteToStream(Stream writer) { System.Diagnostics.Debug.Assert(!IsReadOnly); System.Diagnostics.Debug.Assert(positionOfCompressedDataInArchive == 0); if (uncompressedData != null) { if (uncompressedData.CanWrite) { throw new Exception("Unclosed writable handle to " + Name + " still exists at Save time"); } MemoryStream compressedDataStream = new RepairedMemoryStream((int)(uncompressedData.Length * 5 / 8)); Stream compressor = new System.IO.Compression.DeflateStream(compressedDataStream, System.IO.Compression.CompressionMode.Compress); compressor.Write(uncompressedData.GetBuffer(), 0, (int)uncompressedData.Length); compressor.Close(); compressionMethod = CompressionMethod.Deflate; compressedLength = (int)compressedDataStream.Length; compressedData = compressedDataStream.GetBuffer(); } System.Diagnostics.Debug.Assert(compressedData != null); WriteZipFileHeader(writer); writer.Write(compressedData, 0, compressedLength); }