コード例 #1
0
        public void Start()
        {
            try
            {
                string textOutput = "\n Decoding: ";
                Console.Write(textOutput);
                this.progLocation = textOutput.Length;

                // Read in bytes and convert to a string buffer
                List <byte> encodedBytes = ReadBytesFromFile(this.incoming);
                this.sizeOfCompressedFile = encodedBytes.Count();

                // Extract decoding key and the encoded/compressed text and generate a frequency list
                List <byte> decodingKey = GetKeyFromBytes(encodedBytes);
                List <byte> encodedText = GetEncodedBytesFromFile(encodedBytes);
                List <Tuple <string, int, HuffmanNode> > freqList = GetFrequencyList(decodingKey);

                // Create huffman Tree and decoding table
                HuffmanNode rootNode = ConstructHuffmanTree(freqList);
                Dictionary <char, string> decodingDict = CreateNewBinaryDictionary(rootNode);

                // Using the decoding table decode the text.  Dump text into a List of bytes
                string      decodedText  = DecodeBytes(decodingDict, encodedText);
                List <byte> decodedBytes = new List <byte>();
                decodedBytes.AddRange(Encoding.ASCII.GetBytes(decodedText));
                this.sizeOfTextFile = decodedBytes.Count();

                // Write decoded bytes to file
                WriteBytesToFile(path + outgoing, "txt", decodedBytes);

                DrawText(100, 100, progLocation);
                Console.WriteLine("\n");
                Console.WriteLine(" Decoding Complete! \n");
            }
            catch (Exception e)
            {
                Console.WriteLine("\n\n The following error occurred during decoding: {0}", e.Message);
                System.Environment.Exit(1);
            }
        }
コード例 #2
0
        public void Start()
        {
            try
            {
                string textOutput = "\n Compressing: ";
                Console.Write(textOutput);
                this.progLocation = textOutput.Length;

                // Read in bytes and convert to a string buffer
                List <byte> byteList = ReadBytesFromFile(this.incoming);
                this.sizeOfTextFile = byteList.Count;
                string textBuffer = System.Text.Encoding.ASCII.GetString(byteList.ToArray());

                // Generate frequency table and Huffman tree
                List <Tuple <string, int, HuffmanNode> > freqList = GetFrequencyList(textBuffer);
                HuffmanNode rootNode = ConstructHuffmanTree(freqList);

                // Generate Encoded dictionary (key) for converting ASCII into a new binary format
                Dictionary <char, string> encodedDict = CreateNewBinaryDictionary(rootNode);

                // Using the Encoded dictionary to create the encoded text and encoded key
                // **NOTE: The encoded key is used to decode the compressed file back into text
                List <byte> encodedText = GenerateBinaryEncoding(encodedDict, textBuffer);
                List <byte> encodedKey  = CreateEncodingKey(freqList);
                List <byte> encodedFile = BuildFullEndcodedList(encodedKey, encodedText);
                this.sizeOfCompressedFile = encodedFile.Count;

                // Write encoded bytes to file
                WriteBytesToFile(path + outgoing, "huf", encodedFile);
                DrawText(100, 100, progLocation);
                Console.WriteLine("\n");
                Console.WriteLine(" Compression Complete! \n");
            }
            catch (Exception e)
            {
                Console.WriteLine("\n\n The following error occurred during compression: {0}", e.Message);
                System.Environment.Exit(1);
            }
        }