Пример #1
0
 /// <summary>
 /// Rebuild the byte list from the original uncompressed file using the BinaryTreeWithoutHeader object made from the RebuildBinaryTree method.
 /// </summary>
 /// <param name="binaryTree">The BinaryTreeWithoutHeader object to use.</param>
 /// <returns>The byte list from the original uncompressed file.</returns>
 protected byte[] RebuildOriginalLstByte(BinaryTreeWithoutHeader<CharData> binaryTree)
 {
     List<byte> originalLstByteTmp = new List<byte>();
     BinaryTreeNode<CharData> currentNode = binaryTree.Root;
     String str = binaryTree.EncodedText.ToString(0, binaryTree.EncodedText.Length);
     int i = 0;
     while (i < str.Length)
     {
         if (str[i] == '0')
         {
             // Move Left
             if (currentNode.Left != null)
             {
                 currentNode = currentNode.Left;
             }
             else
             {
                 originalLstByteTmp.Add(currentNode.Value.CharCode);
                 currentNode = binaryTree.Root.Left;
             }
         }
         else
         {
             // Move Right
             if (currentNode.Right != null)
             {
                 currentNode = currentNode.Right;
             }
             else
             {
                 originalLstByteTmp.Add(currentNode.Value.CharCode);
                 currentNode = binaryTree.Root.Right;
             }
         }
         i++;
     }
     originalLstByteTmp.Add(currentNode.Value.CharCode);
     return originalLstByteTmp.ToArray();
 }
Пример #2
0
        /// <summary>
        /// Build a BinaryTreeWithoutHeader object that extend the BinaryTree class.
        /// This object contains the encoded text as well as the binary tree.
        /// </summary>
        /// <param name="lstByte">The byte list from the compressed file.</param>
        /// <returns>The built BinaryTreeWithoutHeader object.</returns>
        protected BinaryTreeWithoutHeader<CharData> RebuildBinaryTree(byte[] lstByte)
        {
            BinaryTreeNode<CharData> currentNode = new BinaryTreeNode<CharData>();
            BinaryTreeWithoutHeader<CharData> binaryTree = new BinaryTreeWithoutHeader<CharData>();
            binaryTree.Root = currentNode;

            StringBuilder sb = new StringBuilder();
            foreach (byte b in lstByte)
            {
                sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
            }
            bool headerDone = false;
            bool isCharCode = false;
            bool isRight = true;
            bool charCodeWasJustDone = false;
            int index = 0;
            string currentChunk = "";
            string currentBinaryCode = "";
            string previousBinaryCode = "";
            while (!headerDone)
            {
                if (!isCharCode && currentChunk.Length == 2)
                {
                    // Do something with the tree
                    switch (currentChunk)
                    {
                        case "01":
                            // Going right
                            currentBinaryCode += "1";
                            charCodeWasJustDone = false;
                            currentNode.Right = new BinaryTreeNode<CharData>();
                            currentNode.Right.Parent = currentNode;
                            currentNode = currentNode.Right;
                            isRight = true;
                            break;
                        case "10":
                            // Going left
                            currentBinaryCode += "0";
                            charCodeWasJustDone = false;
                            currentNode.Left = new BinaryTreeNode<CharData>();
                            currentNode.Left.Parent = currentNode;
                            currentNode = currentNode.Left;
                            isRight = false;
                            break;
                        case "11":
                            // Going parent
                            if (!charCodeWasJustDone)
                            {
                                isCharCode = true;
                            }
                            if (currentBinaryCode.Length > 0)
                            {
                                previousBinaryCode = currentBinaryCode;
                                currentBinaryCode = currentBinaryCode.Substring(0, currentBinaryCode.Length - 1);
                            }
                            currentNode = currentNode.Parent;
                            break;
                        case "00":
                            // Binary tree done
                            if(charCodeWasJustDone)
                                headerDone = true;
                            break;
                    }
                    currentChunk = "";
                }
                else if (isCharCode && currentChunk.Length == 8)
                {
                    CharData nodeToAdd = new CharData(new KeyValuePair<byte, int>(), previousBinaryCode, Convert.ToByte(currentChunk, 2));
                    // You got a char
                    if (isRight)
                    {
                        currentNode.Right.Value = nodeToAdd;
                    }
                    else
                    {
                        currentNode.Left.Value = nodeToAdd;
                    }
                    isCharCode = false;
                    charCodeWasJustDone = true;
                    currentChunk = "";
                }
                if (!headerDone)
                {
                    currentChunk += sb[index];
                    index++;
                }
            }
            sb.Remove(0, index);
            binaryTree.EncodedText = sb;
            return binaryTree;
        }