Beispiel #1
0
        /// <summary>
        /// read data part of input file, writes it to output file decompressed
        /// </summary>
        /// <param name="Reader"></param>
        /// <param name="Writer"></param>
        /// <param name="Tree">contains huffman tree</param>
        public static void DecompressFile(BinaryReader Reader, BinaryWriter Writer, Huff3TreeBuilder Tree)
        {
            int size = 4096;

            byte[] buffer;
            bool   empty = true;
            uint   mask  = 1; //to get least significant bit
            //we need to know, how many symbols should be in file otherwise we could interpret trailing zeros at the end of file as symbols
            ulong remainingSymbols = Tree.NumberOfSymbols;
            Node  Root             = Tree.Root;
            Node  iterator         = Tree.Root;

            //read file using buffer
            do
            {
                buffer = Reader.ReadBytes(size);
                foreach (byte item in buffer)
                {
                    uint it = (uint)item;
                    for (int i = 0; i < 8; i++)
                    {
                        //get value of the least significant bit
                        uint curr = it & mask;
                        // Console.Write("{0} ", curr);
                        //shift - get rid of bit already taken into account
                        it = it >> 1;

                        if (remainingSymbols == 0)
                        {
                            //end of file - only trailling zeroes remaining
                            return;
                        }

                        DoStepInTree(ref iterator, curr, Writer, Root, ref remainingSymbols);
                    }
                    empty = false;
                }
            } while (buffer.Length == size);
            if (remainingSymbols > 0)
            {
                Console.WriteLine("File Error");
            }
        }
Beispiel #2
0
        static void HuffIII(string[] args)
        {
            bool opened = Program.Huff3OpenFiles(args, out BinaryReader reader, out BinaryWriter writer);

            if (!opened)
            {
                return;
            }
            Huff3TreeBuilder b = new Huff3TreeBuilder();

            bool success = b.ReadHeaderAndTreeFromBinaryFile(reader);

            if (!success)
            {
                return;
            }
            Huff3Decompresor.DecompressFile(reader, writer, b);
            reader.Close();
            writer.Close();
        }