예제 #1
0
        //compressing pdf file

        //Writng Compress File
        public void PDFCompressFile(string content)
        {
            //string fileName = "compressed.cmu";

            // Create a new file
            writeBitByBit bit = new writeBitByBit(CompName);  //creating an instance of bit write

            //writing tree info
            PQueue.cNode top = root;
            printHeaderTree(top, bit);

            int    c;
            string code = "0";                       //initializing

            for (int i = 0; i < content.Length; ++i) //reading char from string containg input
            {
                Console.WriteLine(content[i]);
                if (HuffmanCode.ContainsKey(content[i]))
                {
                    code = HuffmanCode[content[i]];   //reading character's huffman code from th Code table
                }
                else
                {
                    //if not fount in table do nothing
                }
                for (int j = 0; j < code.Length; ++j)    //reading each character from the Huffman code  like if code for A = 011 the first 0 then 1 and then 1
                {
                    if (code[j] == '0')
                    {
                        bit.BitWrite(0);                //calling writing function with 0
                    }
                    else
                    {
                        bit.BitWrite(1);               //calling writing function with 1
                    }
                }
            }
            //now we need to write psedu_EOF so that we don't reed extra bytes from the file
            code = HuffmanCode[(char)Pseudo_EOF];
            for (int i = 0; i < code.Length; ++i)    //reading each character from the Huffman code  like if code for A = 011 the first 0 then 1 and then 1
            {
                if (code[i] == '0')
                {
                    bit.BitWrite(0);                //calling writing function with 0
                }
                else
                {
                    bit.BitWrite(1);               //calling writing function with 1
                }
            }
            bit.close();
        }
예제 #2
0
 //writing tree
 public void printHeaderTree(PQueue.cNode top, writeBitByBit bit)
 {
     if (top == null)
     {
         return;
     }
     if (top.leftZero == null && top.rightOne == null)
     {
         bit.ByteWrite('1');
         bit.ByteWrite(top.value);
     }
     else
     {
         bit.ByteWrite('0');
         printHeaderTree(top.leftZero, bit);
         printHeaderTree(top.rightOne, bit);
     }
 }
예제 #3
0
        //Writng Compress File
        public void WriteCompressFile(FileStream fs)
        {
            // Create a new file
            writeBitByBit bit = new writeBitByBit(CompName);  //creating an instance of bit write

            //writing tree info
            PQueue.cNode top = root;
            printHeaderTree(top, bit);
            string code = "0";

            if (fileExten1 == ".csv")                      //code for csv is different because that code cause problem with csv files
            {
                var sr = new StreamReader(fs);             //opeining input file to be compreesed so to read from it and compare and store
                int c;
                while ((c = sr.Read()) != -1)              //reading char from input file
                {
                    if (frequencyMap.ContainsKey((char)c)) //while calculating frequencies we don't calculate 10 which is LF so we use try to avoid exception when 10 comes as it is not present in the HuffmanCode directory
                    {
                        code = HuffmanCode[(char)c];       //reading character's huffman code from th Code table
                    }

                    for (int i = 0; i < code.Length; ++i)    //reading each character from the Huffman code  like if code for A = 011 the first 0 then 1 and then 1
                    {
                        if (code[i] == '0')
                        {
                            bit.BitWrite(0);                //calling writing function with 0
                        }
                        else
                        {
                            bit.BitWrite(1);               //calling writing function with 1
                        }
                    }
                }
            }

            else   //code for .txt and .cpp .c .cs
            {
                using (var sr1 = new StreamReader(fs))
                {
                    while (!sr1.EndOfStream)
                    {
                        string s = sr1.ReadLine();
                        foreach (char ch in s)
                        {
                            if (frequencyMap.ContainsKey(ch))
                            {
                                code = HuffmanCode[ch];           //reading character's huffman code from th Code table
                            }
                            for (int i = 0; i < code.Length; ++i) //reading each character from the Huffman code  like if code for A = 011 the first 0 then 1 and then 1
                            {
                                if (code[i] == '0')
                                {
                                    bit.BitWrite(0);                //calling writing function with 0
                                }
                                else
                                {
                                    bit.BitWrite(1);               //calling writing function with 1
                                }
                            }
                        }
                    }
                }
            }


            //now we need to write psedu_EOF so that we don't reed extra bytes from the file
            code = HuffmanCode[(char)Pseudo_EOF];
            for (int i = 0; i < code.Length; ++i)    //reading each character from the Huffman code  like if code for A = 011 the first 0 then 1 and then 1
            {
                if (code[i] == '0')
                {
                    bit.BitWrite(0);                //calling writing function with 0
                }
                else
                {
                    bit.BitWrite(1);               //calling writing function with 1
                }
            }

            bit.close();
            fs.Close();
        }