public static void Main (string [] args) { if (args.Length == 0 || args.Length > 3) { Console.WriteLine ("Usage: zipmark FILE [ITERATIONS] [BLOCKSIZE]"); return; } string filename = args [0]; FileInfo file = new FileInfo (filename); if (!file.Exists) { Console.WriteLine ("Couldn't find file {0}", filename); return; } FileStream fs = file.OpenRead (); byte [] raw = new byte [file.Length]; int count = fs.Read (raw, 0, (int)file.Length); fs.Close (); if (count != file.Length) { Console.WriteLine ("Couldn't read file {0}", filename); return; } Deflater def = new Deflater (Deflater.BEST_COMPRESSION, false); Inflater inf = new Inflater (false); // 1. Count deflated size int cooked_size = Deflate (def, raw, null); byte [] cooked = new byte [cooked_size]; // 2. Deflate & Inflate if (args.Length > 1) Iterations = Int32.Parse (args [1]); if (args.Length > 2) BlockSize = Int32.Parse (args [2]); for (int i = 0; i < Iterations; ++ i) { Deflate (def, raw, cooked); Inflate (inf, cooked, raw); } return; }
static int Deflate (Deflater def, byte [] src, byte [] dest) { bool count; int offset, length, remain; if (dest == null) { dest = new byte [BlockSize]; count = true; } else count = false; def.Reset (); def.SetInput (src); offset = 0; while (!def.IsFinished) { if (def.IsNeedingInput) def.Finish (); remain = Math.Min (dest.Length - offset, BlockSize); if (remain == 0) break; length = def.Deflate (dest, offset, remain); if (!count) offset += length; } return def.TotalOut; }