Ionic.Zip.ZipOutputStream.Write C# (CSharp) Method

Write() public method

Write the data from the buffer to the stream.
As the application writes data into this stream, the data may be compressed and encrypted before being written out to the underlying stream, depending on the settings of the CompressionLevel and the Encryption properties.
public Write ( byte buffer, int offset, int count ) : void
buffer byte The buffer holding data to write to the stream.
offset int the offset within that data array to find the first byte to write.
count int the number of bytes to write.
return void
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_disposed)
            {
                _exceptionPending = true;
                throw new System.InvalidOperationException("The stream has been closed.");
            }

            if (buffer==null)
            {
                _exceptionPending = true;
                throw new System.ArgumentNullException("buffer");
            }

            if (_currentEntry == null)
            {
                _exceptionPending = true;
                throw new System.InvalidOperationException("You must call PutNextEntry() before calling Write().");
            }

            if (_currentEntry.IsDirectory)
            {
                _exceptionPending = true;
                throw new System.InvalidOperationException("You cannot Write() data for an entry that is a directory.");
            }

            if (_needToWriteEntryHeader)
                _InitiateCurrentEntry(false);

            if (count != 0)
                _entryOutputStream.Write(buffer, offset, count);
        }

Usage Example

Example #1
0
 /********************************************************
 * CLASS METHODS
 *********************************************************/
 /// <summary>
 /// 
 /// </summary>
 /// <param name="zipPath"></param>
 /// <param name="filenamesAndData"></param>
 /// <returns></returns>
 public static bool Zip(string zipPath, System.Collections.Generic.Dictionary<string, string> filenamesAndData)
 {
     var success = true;
     var buffer = new byte[4096];
     try
     {
         using (var stream = new ZipOutputStream(System.IO.File.Create(zipPath)))
         {
             foreach (var filename in filenamesAndData.Keys)
             {
                 var file = filenamesAndData[filename].GetBytes();
                 var entry = stream.PutNextEntry(filename);
                 using (var ms = new System.IO.MemoryStream(file))
                 {
                     int sourceBytes;
                     do
                     {
                         sourceBytes = ms.Read(buffer, 0, buffer.Length);
                         stream.Write(buffer, 0, sourceBytes);
                     }
                     while (sourceBytes > 0);
                 }
             }
             stream.Flush();
             stream.Close();
         }
     }
     catch (System.Exception err)
     {
         System.Console.WriteLine("Compression.ZipData(): " + err.Message);
         success = false;
     }
     return success;
 }
All Usage Examples Of Ionic.Zip.ZipOutputStream::Write