C# deflatestream compress and decompress, compare their respective md5

using System;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;


static void Main(string[] args)
        {
            DeflateStreamDemo();
            Console.ReadLine();
        }

        static string fileName = "original.txt";
        static string compressFileName = "compressed.cmp";
        static string decompressFileName = "decompressed.txt";
        static void DeflateStreamDemo()
        {            
            using (FileStream fs = File.Create(fileName))
            {
                StringBuilder builder = new StringBuilder(); 
                for(int i=0;i<10000;i++)
                {
                    builder.AppendLine("Everything depend on myself!");
                    builder.AppendLine("Actions speak lounder than words!");
                    builder.AppendLine("Implement the promise via vision and adamant execution!\n");
                }                
                byte[] bytesData = Encoding.UTF8.GetBytes(builder.ToString());
                fs.Write(bytesData, 0, bytesData.Length);
            }
            CompressFile(fileName);
            DecompressFile(compressFileName);
            ValidateOriginalAndDecompressed();
        }

        private static void CompressFile(string fileName)
        {
            using (FileStream originalStream = File.OpenRead(fileName))
            {
                using (FileStream compressStream = File.Create(compressFileName))
                {
                    using (DeflateStream ds = new DeflateStream(compressStream, CompressionMode.Compress))
                    {
                        originalStream.CopyTo(ds);
                    }
                }
            }
        }

        static void DecompressFile(string compressedFile)
        {
            using(FileStream compressedStream=File.OpenRead(compressedFile))
            {
                using(FileStream decompressStream=File.Create(decompressFileName))
                {
                    using(DeflateStream ds=new DeflateStream(compressedStream, CompressionMode.Decompress))
                    {
                        ds.CopyTo(decompressStream);
                    }
                }
            }
        }

        static void ValidateOriginalAndDecompressed()
        {
            string rawMd5 = string.Empty;
            string decompressMd5 = string.Empty;
            using (StreamReader reader=new StreamReader(fileName))
            {
                string rawText = reader.ReadToEnd();
                rawMd5 = GetStringMd5(rawText);
            }

            using(StreamReader reader=new StreamReader(decompressFileName))
            {
                string decompressedText = reader.ReadToEnd();
                decompressMd5 = GetStringMd5(decompressedText);
            }

            if(string.Equals(rawMd5,decompressMd5))
            {
                Console.WriteLine("The identical!");
            }
            else
            {
                Console.WriteLine("Error!");
            }
        }

        static string GetStringMd5(string str)
        {
            if(string.IsNullOrWhiteSpace(str))
            {
                return null;
            }
            StringBuilder builder = new StringBuilder();     
            
            using(MD5 md5= MD5.Create())
            {
                byte[] rawData = Encoding.UTF8.GetBytes(str);
                byte[] md5Data = md5.ComputeHash(rawData);
                for(int i=0;i<md5Data.Length;i++)
                {
                    builder.Append(md5Data[i].ToString("x2"));
                }
            }
            return builder.ToString();
        }

 

C# deflatestream compress and decompress, compare their respective md5

上一篇:C# HttpClientHelper


下一篇:window.history.go 返回上一页bing'shua'xin