DeflateStream Class

Overview

  • Used to compress and decompress data.
  • Slightly smaller result than GZipStream because of lack of header information.
  • Use for internal use in your own application.
  • Works like a BufferedStream as it sits on top of another stream.
  • Limited to files up to 4GB (uncompressed).

Good practice

  • Remember to call Close on a stream.

Compress File

FileStream fileIn = File.OpenRead(@"C:\Public\Test.txt");
FileStream fileOut = File.OpenWrite(@"C:\Public\TestComp.txt");
 
DeflateStream compress = new DeflateStream(fileOut, CompressionMode.Compress);
 
byte[] buffer = new byte[fileIn.Length];
fileIn.Read(buffer, 0, buffer.Length);
compress.Write(buffer, 0, buffer.Length);
compress.Close();
 
fileIn.Close();
fileOut.Close();

Decompress File

FileStream compressedFile = File.OpenRead(@"C:\Public\TestComp.txt");
DeflateStream decompress = new DeflateStream(compressedFile, CompressionMode.Decompress);
StreamReader read = new StreamReader(decompress);
 
Console.WriteLine(read.ReadToEnd());
read.Close();
decompress.Close();
compressedFile.Close();
 
learning/exam70-536/deflatestream.txt · Last modified: 2008/08/27 15:08 by david
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki