Esempio n. 1
0
        public string Unzip(string content)
        {
            HuffmanTreeInfo huffmanTreeInfo = JsonToObject <HuffmanTreeInfo>(content);

            StringBuilder sb = new StringBuilder(huffmanTreeInfo.ZipCode.Length);

            for (int i = 0; i < huffmanTreeInfo.ZipCode.Length; i++)
            {
                sb.Append(Convert.ToString((int)huffmanTreeInfo.ZipCode[i], 2).PadLeft(8, '0'));
            }
            string huffmanCodes = sb.ToString() + huffmanTreeInfo.ZipCodeRemainder;

            StringBuilder decode = new StringBuilder(huffmanCodes.Length);
            string        temp   = string.Empty;

            for (int i = 0; i < huffmanCodes.Length; i++)
            {
                temp += huffmanCodes[i].ToString();
                if (huffmanTreeInfo.UnZipDictionary.ContainsKey(temp))
                {
                    decode.Append(huffmanTreeInfo.UnZipDictionary[temp]);
                    temp = string.Empty;
                }
            }
            return(decode.ToString());
        }
Esempio n. 2
0
        public string Compression(string content)
        {
            HuffmanNote huffmanNote = CreateHuffmanTree(CreateWordWeightDictionary(content));

            Dictionary <char, string> encodeDictionary = new Dictionary <char, string>();

            CreateWordCodeDictionay(huffmanNote, "", encodeDictionary);

            StringBuilder sb = new StringBuilder(content.Length);

            foreach (var item in content)
            {
                sb.Append(encodeDictionary[item]);
            }

            string huffmanCode = sb.ToString();

            HuffmanTreeInfo huffmanTreeInfo = new HuffmanTreeInfo();

            huffmanTreeInfo.ZipCodeRemainder = huffmanCode.Substring(huffmanCode.Length - huffmanCode.Length % 8);
            huffmanTreeInfo.ZipCode          = HuffmanCodeToByte(huffmanCode.Substring(0, huffmanCode.Length - huffmanCode.Length % 8));
            huffmanTreeInfo.UnZipDictionary  = CreateUnZipDictionary(encodeDictionary);

            return(ObjectToJson(huffmanTreeInfo));
        }