static void Main(string[] args) { // check cmd args if (args.Length != 1 || args[0] == "") { Console.WriteLine("Argument Error"); return; } try { FileStream fs = new FileStream(args[0], FileMode.Open); HuffmannTree huffmannTree = HuffmannTree.BuildTree(fs); FileStream outputFile = new FileStream(args[0] + ".huff", FileMode.Create); outputFile.Write(new byte[] { 0x7B, 0x68, 0x75, 0x7C, 0x6D, 0x7D, 0x66, 0x66 }); // write header huffmannTree.PrintTree(outputFile, true); } catch (Exception ex) { if (ex is IOException || ex is UnauthorizedAccessException) { Console.WriteLine("File Error"); return; } throw; } }
public static void Encode(Stream inputStream, Stream outputStream) { HuffmannTree huffmannTree = HuffmannTree.BuildTree(inputStream); outputStream.Write(new byte[] { 0x7B, 0x68, 0x75, 0x7C, 0x6D, 0x7D, 0x66, 0x66 }, 0, 8); // write header huffmannTree.PrintTree(outputStream, true); outputStream.Write(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0, 8); // huffmann tree representation is ended with 8 bytes of zeros inputStream.Position = 0; EncodeData(inputStream, outputStream, huffmannTree); }