コード例 #1
0
        // These routines are only to be used by ZipArchive.
        //
        /// <summary>
        /// Used by ZipArchive to write the entry to the archive.
        /// </summary>
        /// <param name="writer">The stream representing the archive to write the entry to.</param>
        internal void WriteToStream(Stream writer)
        {
            Debug.Assert(!IsReadOnly);
            Debug.Assert(positionOfCompressedDataInArchive == 0);   // we don't use this on read-write archives.

            if (uncompressedData != null)
            {
                if (uncompressedData.CanWrite)
                {
                    throw new Exception("Unclosed writable handle to " + Name + " still exists at Save time");
                }

                // TODO: Consider using seeks instead of copying to the compressed data stream.
                // TODO support not running Deflate but simply skipping (useful for arcping arc files)
                //
                // Compress the data
                MemoryStream compressedDataStream = new RepairedMemoryStream((int)(uncompressedData.Length * 5 / 8));
                Stream       compressor           = new DeflateStream(compressedDataStream, CompressionMode.Compress);
                compressor.Write(uncompressedData.GetBuffer(), 0, (int)uncompressedData.Length);
                compressor.Close();

                // TODO support the NONE case too.
                compressionMethod = CompressionMethod.Deflate;

                compressedLength = (int)compressedDataStream.Length;
                compressedData   = compressedDataStream.GetBuffer();
            }

            Debug.Assert(compressedData != null);
            WriteZipFileHeader(writer);                             // Write the header.
            writer.Write(compressedData, 0, compressedLength);      // Write the data.
        }
コード例 #2
0
        // These are the primitive operations

        /// <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.");
            }

            // abandon any old data
            compressedData = null;
            positionOfCompressedDataInArchive = 0;
            compressedLength = 0;

            // We allocate some buffer so that GetBuffer() does not return null.
            uncompressedData = new RepairedMemoryStream(256);
            return(uncompressedData);
        }