Beispiel #1
0
        static void DecodeHuffmanEncodedFile(string inputFile, string outputFile)
        {
            BinaryReader reader  = new BinaryReader(new FileStream(inputFile, FileMode.Open));
            ITreeBuilder builder = new TreeBuilder.TreeBuilder(null, reader);

            if (!builder.BuildTreeFromFile())
            {
                ReportFileError();
                return;
            }

            BinaryWriter writer  = new BinaryWriter(new FileStream(outputFile, FileMode.OpenOrCreate));
            IDecoder     decoder = new TreeDecoder.Decoder(builder.tree, reader, writer);

            decoder.DecodeTree();

            writer.Close();
            reader.Close();
        }
Beispiel #2
0
        /// <summary>
        /// Main program method...Processes input file into a tree and prints the tree in prefix notation
        /// </summary>
        /// <param name="inputFile">Input File</param>
        static void ProduceHuffmanTree(string inputFile)
        {
            IInputProcessor processor = new InputProcessor.InputProcessor(new FileStream(inputFile, FileMode.Open));
            var             leafTree  = processor.ProcessInput();

            ITreeBuilder builder = new TreeBuilder.TreeBuilder(leafTree, new BinaryReader(Stream.Null));

            if (leafTree.Count == 0)
            {
                // TODO: When empty do something(I guess nothing)
                return;
            }
            builder.BuildTree();
            INode root = builder.tree.Min;

            IEncoder encoder = new TreeEncoder.Encoder();

            string[] binaryArray = encoder.EncodeNodesWithBinary(root, new StringBuilder(), new string[256]);

            IWriter writer = new Writer(new FileStream(inputFile, FileMode.Open), new BinaryWriter(File.Open(inputFile + ".huff", FileMode.OpenOrCreate)));

            writer.EncodeAndPrint(binaryArray, root);
        }