示例#1
0
        private string GetDecompressedText(string compressedFilePath, string compressedFileKeysPath)
        {
            var reader           = new TxtReader();
            var compressor       = new HuffmanCompressor();
            var byteReader       = new ByteReader();
            var byteContent      = byteReader.ReadFile(compressedFilePath);
            var bytes            = ByteExtractor.GetBytes(byteContent);
            var codesString      = reader.ReadFile(compressedFileKeysPath, Encoding.UTF8);
            var codesToDecoding  = DictionaryConverter.ConvertFromString(codesString);
            var decompressedText = compressor.Decompress(bytes, codesToDecoding);

            return(decompressedText);
        }
示例#2
0
        private Tuple <byte[], string> GetCompressedTextAndCodes(string filePath)
        {
            var reader         = new TxtReader();
            var preprocessor   = new HuffmanPreprocessor();
            var compressor     = new HuffmanCompressor();
            var fileContent    = reader.ReadFile(filePath, Encoding.UTF8);
            var charsCount     = CharCounter.GetSymbolsCount(fileContent);
            var sortedChars    = charsCount.OrderBy(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value);
            var nodeList       = preprocessor.GetNodeList(sortedChars);
            var tree           = preprocessor.GetHuffmanTree(nodeList);
            var codes          = preprocessor.GetCharCodes(tree);
            var compressedText = compressor.GetCompressedText(fileContent, codes);

            codes.Add(compressedText.Length.ToString(), new byte[0]);
            var codesText = DictionaryConverter.ConvertToString(codes);

            return(new Tuple <byte[], string>(compressedText, codesText));
        }