// 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 GZipTest { public void WithOneByte(string fromfnm, string tofnm) { using(FileStream fromf = File.OpenRead(fromfnm)) using(FileStream tof = File.Create(tofnm)) using(GZipStream cmp = new GZipStream(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(GZipStream cmp = new GZipStream(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(GZipStream cmp = new GZipStream(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(GZipStream cmp = new GZipStream(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) { GZipTest gzt = new GZipTest(); gzt.WithOneByte(@"C:\small.dat", @"C:\small.gz1"); gzt.WithBuffer(@"C:\small.dat", @"C:\small.gz2"); gzt.WithCombo(@"C:\small.dat", @"C:\small.gz3"); gzt.WithComboX(@"C:\small.dat", 256, @"C:\small.gz4"); gzt.WithComboX(@"C:\small.dat", 16, @"C:\small.gz5"); gzt.WithOneByte(@"C:\bigger.dat", @"C:\bigger.gz1"); gzt.WithBuffer(@"C:\bigger.dat", @"C:\bigger.gz2"); gzt.WithCombo(@"C:\bigger.dat", @"C:\bigger.gz3"); gzt.WithComboX(@"C:\bigger.dat", 256, @"C:\bigger.gz4"); gzt.WithComboX(@"C:\bigger.dat", 16, @"C:\bigger.gz5"); } } }