// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.IO.Compression; namespace E { public class DeflateTest { public void WithOneByte(string fromfnm, string tofnm) { using(FileStream fromf = File.OpenRead(fromfnm)) using(FileStream tof = File.Create(tofnm)) using(DeflateStream cmp = new DeflateStream(tof, CompressionMode.Compress)) { int c; while((c = fromf.ReadByte()) >= 0) { cmp.WriteByte((byte)c); } } } public void WithBuffer(string fromfnm, string tofnm) { using(FileStream fromf = File.OpenRead(fromfnm)) using(FileStream tof = File.Create(tofnm)) using(DeflateStream cmp = new DeflateStream(tof, CompressionMode.Compress)) { byte[] b = new byte[100000]; int n; while((n = fromf.Read(b, 0, b.Length)) > 0) { cmp.Write(b, 0, n); } } } public void WithCombo(string fromfnm, string tofnm) { using(FileStream fromf = File.OpenRead(fromfnm)) using(FileStream tof = File.Create(tofnm)) using(DeflateStream cmp = new DeflateStream(tof, CompressionMode.Compress)) using(BufferedStream buf = new BufferedStream(cmp)) { int c; while((c = fromf.ReadByte()) >= 0) { buf.WriteByte((byte)c); } } } public void WithComboX(string fromfnm, int bufsiz, string tofnm) { using(FileStream fromf = File.OpenRead(fromfnm)) using(FileStream tof = File.Create(tofnm)) using(DeflateStream cmp = new DeflateStream(tof, CompressionMode.Compress)) using(BufferedStream buf = new BufferedStream(cmp, bufsiz)) { int c; while((c = fromf.ReadByte()) >= 0) { buf.WriteByte((byte)c); } } } public static void Main(string[] args) { DeflateTest gzt = new DeflateTest(); gzt.WithOneByte(@"C:\foobar.txt", @"C:\foobar.gz1"); gzt.WithBuffer(@"C:\foobar.txt", @"C:\foobar.gz2"); gzt.WithCombo(@"C:\foobar.txt", @"C:\foobar.gz3"); gzt.WithComboX(@"C:\foobar.txt", 256, @"C:\foobar.gz4"); gzt.WithComboX(@"C:\foobar.txt", 16, @"C:\foobar.gz5"); } } }