示例#1
0
        public static bool DecodeFile(string InputPath, string OutputPath)
        {
            bool         OpenSuccessfully = true;
            BinaryWriter OutputFile       = null;

            try
            {
                SourceFile = new BinaryReader(File.OpenRead(InputPath));
                OutputFile = new BinaryWriter(File.Create(OutputPath));
            }
            catch
            {
                OpenSuccessfully = false;
            }

            if (OpenSuccessfully)
            {
                AdaptiveHuffmanTree DecodingModel = new AdaptiveHuffmanTree();
                DecodingModel.CreateModel();
                DecodingModel.NotEnoughCodeEvent += new AdaptiveHuffmanTree.NotEnoughCodeEventDelegate(OnNotEnoughCodeEvent);
                bool   FinishFlag = false;
                Byte?  Symbol     = 0;
                string Code       = "";

                //убираем расширение
                do
                {
                    ProgressEvent();
                }while (SourceFile.ReadChar() != '.');

                ProgressEvent();
                Code = AdaptiveHuffmanTree.ToBinaryString(SourceFile.ReadByte());

                while (!FinishFlag)
                {
                    Symbol = DecodingModel.Decode(ref Code);
                    if (Symbol == null)
                    {
                        FinishFlag = true;
                    }
                    if (!FinishFlag)
                    {
                        OutputFile.Write((Byte)Symbol);
                    }
                }
            }
            SourceFile.Close();
            OutputFile.Close();
            return(OpenSuccessfully);
        }
        //кодирование символа
        public string Encode(Byte?SymbolToEncode)
        {
            string Code = "";
            char   a    = Convert.ToChar(SymbolToEncode);
            AdaptiveHuffmanTree KeyLeave;

            if (SymbolToEncode == null)
            {
                KeyLeave = Find("EoF");
            }
            else
            {
                KeyLeave = Find(SymbolToEncode);
                if (KeyLeave == null)
                {
                    Code     = AdaptiveHuffmanTree.ToBinaryString(SymbolToEncode);
                    KeyLeave = Find("Esc");
                }
            }
            AdaptiveHuffmanTree Parent = this.FindParent(KeyLeave);

            do
            {
                if (Parent.Left == KeyLeave)
                {
                    Code = "0" + Code;
                }
                if (Parent.Right == KeyLeave)
                {
                    Code = "1" + Code;
                }
                KeyLeave = Parent;
                Parent   = this.FindParent(KeyLeave);
            }while (Parent != KeyLeave);

            Rebuild(SymbolToEncode);

            return(Code);
        }
示例#3
0
 private static void OnNotEnoughCodeEvent(ref string Code)
 {
     Code += AdaptiveHuffmanTree.ToBinaryString(SourceFile.ReadByte());
     ProgressEvent();
 }