Beispiel #1
0
        public static void Main(String[] args)
        {
            String encodedFileName = "Encoded.txt";
            String fileToEncode    = "handmade1.txt";

            //String str = "AAAAABBCCCD";
            string[] lines = File.ReadAllLines(fileToEncode);

            var wholeText = "";

            foreach (string line in lines)
            {
                wholeText += line;
            }

            var encoded = RLE.encode(wholeText);

            //Console.WriteLine(encoded);
            using StreamWriter outputFile = new StreamWriter("Encoded.txt");
            outputFile.Write(encoded);
            Console.WriteLine("Compression Ratio: " + CompressionRatio.Calculate(wholeText, encoded).ToString());
            Console.WriteLine("Source BitRate: " + BitRate.Calculate(wholeText).ToString());
            Console.WriteLine("Coded BitRate: " + BitRate.Calculate(wholeText, encoded).ToString());
            Console.WriteLine("Saving Percentage: " + SavingPercentage.Calculate
                                  (wholeText, encoded).ToString()
                              + "%");
            Console.WriteLine("Saving Percentage (File): " + SavingPercentage.CalculateFile(
                                  Path.Combine(Environment.CurrentDirectory, fileToEncode),
                                  Path.Combine(Environment.CurrentDirectory, encodedFileName)).ToString() + "%");
            Console.WriteLine("Compression Ratio (File): " + CompressionRatio.CalculateFile(Path.
                                                                                            Combine(Environment.CurrentDirectory, fileToEncode),
                                                                                            Path.Combine(Environment.CurrentDirectory, encodedFileName)).ToString());
        }
        public static void Main(string[] args)
        {
            // Console.WriteLine("Please enter the string:");

            string[] lines          = File.ReadAllLines(fileToEncode);
            var      encodedStrings = new List <BitArray>();
            // Build the Huffman tree
            var i         = 0;
            var wholeText = "";

            foreach (string line in lines)
            {
                wholeText += line;
            }
            Console.WriteLine(i++);
            HuffmanTree huffmanTree = new HuffmanTree();

            huffmanTree.Build(wholeText);
            BitArray encoded = huffmanTree.Encode(wholeText);

            encodedStrings.Add(encoded);
            Console.Write("Encoded: ");

            Console.WriteLine();
            string decoded = huffmanTree.Decode(encoded);

            foreach (var encodedString in encodedStrings)
            {
                byte[] bytes = new byte
                               [encodedString.Length / 8 + (encodedString.Length % 8 == 0 ? 0 : 1)];
                encodedString.CopyTo(bytes, 0);
                File.WriteAllBytes(encodedFileName, bytes);
            }
            Console.WriteLine("Compression Ratio: " + CompressionRatio.CalculateHuffman(wholeText, encoded).
                              ToString());
            Console.WriteLine("Source BitRate: " + BitRate.Calculate(wholeText).ToString());
            Console.WriteLine("Coded BitRate: " + BitRate.Calculate(wholeText, encoded).ToString());
            Console.WriteLine("Saving Percentage (File): " + SavingPercentage.
                              CalculateFile(Path.Combine(Environment.CurrentDirectory, fileToEncode),
                                            Path.Combine(Environment.CurrentDirectory, encodedFileName)).ToString() + "%");
            Console.WriteLine("Saving Percentage: " + SavingPercentage.
                              CalculateHuffmanSavingPercentage(wholeText
                                                               , encoded).ToString() + "%");
            Console.WriteLine("Compression Ratio (File): " + CompressionRatio.CalculateFile
                                  (Path.Combine(Environment.CurrentDirectory, fileToEncode),
                                  Path.Combine(Environment.CurrentDirectory, encodedFileName)).ToString());
        }
        static void Main(string[] args)
        {
            string sourceFileName       = "test.txt";
            string compressedFileName   = "CompressedFile.txt";
            string decompressedFileName = "DecompressedFile.txt";
            string allText = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, sourceFileName), Encoding.Default);

            string compressed = Compress(allText);
            //string decompressed = Decompress(compressed, alphabet);

            int numOfBytes = compressed.Length / 8;

            byte[] bytesArray = new byte[numOfBytes];
            for (int i = 0; i < numOfBytes; ++i)
            {
                bytesArray[i] = Convert.ToByte(compressed.Substring(8 * i, 8), 2);
            }
            using (FileStream fs = File.Create(Path.Combine(Environment.CurrentDirectory, compressedFileName)))
            {
                fs.Write(bytesArray, 0, bytesArray.Length);
            }
            var    compressedFromFileBytes = File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, compressedFileName));
            string decompressed            = Decompress(compressedFromFileBytes);

            using (FileStream fs = File.Create(Path.Combine(Environment.CurrentDirectory, decompressedFileName)))
            {
                byte[] bytes = Encoding.UTF8.GetBytes(decompressed);
                fs.Write(bytes, 0, bytes.Length);
            }
            Console.WriteLine("Compression Ratio: " + CompressionRatio.Calculate(allText.Length, bytesArray.Length).ToString());
            Console.WriteLine("Compression Ratio (File): " + CompressionRatio.CalculateFile(Path.Combine(Environment.CurrentDirectory, sourceFileName),
                                                                                            Path.Combine(Environment.CurrentDirectory, "CompressedFile.txt")).ToString());
            Console.WriteLine("Source BitRate: " + BitRate.Calculate(allText).ToString());
            Console.WriteLine("Coded BitRate: " + BitRate.Calculate(allText.Length, compressed.Length).ToString());
            Console.WriteLine("Saving Percentage: " + SavingPercentage.Calculate(allText.Length, bytesArray.Length).ToString());
            Console.WriteLine("Saving Percentage (File): " + SavingPercentage.CalculateFile(Path.Combine(Environment.CurrentDirectory, sourceFileName),
                                                                                            Path.Combine(Environment.CurrentDirectory, "CompressedFile.txt")).ToString() + "%");
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            string sourceFileName       = "test1.txt";
            string compressedFileName   = "CompressedFile.txt";
            string decompressedFileName = "DecompressedFile.txt";
            string source = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, sourceFileName), Encoding.Default);

            (string compressed, Dictionary <char, ProbabilityInterval> probabilityIntervals) = ArithmeticCoder.Coding(source);
            //string decompressed =  ArithmeticCoder.Decoding(compressed, probabilityIntervals, source.Length);

            int numOfBytes = compressed.Length / 8;

            byte[] bytesArray = new byte[numOfBytes];
            for (int i = 0; i < numOfBytes; ++i)
            {
                bytesArray[i] = Convert.ToByte(compressed.Substring(8 * i, 8), 2);
            }
            using (FileStream fs = File.Create(Path.Combine(Environment.CurrentDirectory, compressedFileName)))
            {
                fs.Write(bytesArray, 0, bytesArray.Length);
            }
            //var compressedFromFileBytes = File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, compressedFileName));
            //var binaryCompressedStr = string.Join("", compressedFromFileBytes.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')));
            //string decompressed = Decoding(binaryCompressedStr, 4);
            //using (FileStream fs = File.Create(Path.Combine(Environment.CurrentDirectory, decompressedFileName)))
            //{
            //    byte[] bytes = Encoding.UTF8.GetBytes(decompressed);
            //    fs.Write(bytes, 0, bytes.Length);
            //}
            Console.WriteLine("Compression Ratio: " + CompressionRatio.Calculate(source.Length, bytesArray.Length).ToString());
            Console.WriteLine("Compression Ratio (File): " + CompressionRatio.CalculateFile(Path.Combine(Environment.CurrentDirectory, sourceFileName),
                                                                                            Path.Combine(Environment.CurrentDirectory, "CompressedFile.txt")).ToString());
            Console.WriteLine("Source BitRate: " + BitRate.Calculate(source).ToString());
            Console.WriteLine("Coded BitRate: " + BitRate.Calculate(source.Length, compressed.Length).ToString());
            Console.WriteLine("Saving Percentage: " + SavingPercentage.Calculate(source.Length, bytesArray.Length).ToString());
            Console.WriteLine("Saving Percentage (File): " + SavingPercentage.CalculateFile(Path.Combine(Environment.CurrentDirectory, sourceFileName),
                                                                                            Path.Combine(Environment.CurrentDirectory, "CompressedFile.txt")).ToString() + "%");
        }