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()); }
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() + "%"); }
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() + "%"); }