Here i will give you one examples which implements GZipstream class.
You just go through the program which is written in C# language
using System.IO; using System.IO.Compression; using System.Text; class Program { static void Main() { try { // 1. // Starting file is 26,747 bytes. string anyString = File.ReadAllText("TextFile1.txt"); // 2. // Output file is 7,388 bytes. CompressStringToFile("new.gz", anyString); } catch { // Couldn't compress. } } public static void CompressStringToFile(string fileName, string value) { // A. // Write string to temporary file. string temp = Path.GetTempFileName(); File.WriteAllText(temp, value); // B. // Read file into byte array buffer. byte[] b; using (FileStream f = new FileStream(temp, FileMode.Open)) { b = new byte[f.Length]; f.Read(b, 0, (int)f.Length); } // C. // Use GZipStream to write compressed bytes to target file. using (FileStream f2 = new FileStream(fileName, FileMode.Create)) using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false)) { gz.Write(b, 0, b.Length); } } } Result Starting file is 26,747 bytes. Output file is 7,388 bytes.
No comments:
Post a Comment