Use GZipStream to Zip : GZipStream : File Stream C# Source Code


Custom Search

C# Source Code » File Stream » GZipStream »

 

Use GZipStream to Zip








    
 


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;

class Program {
    static void SaveCompressedFile(string filename, string data) {
        FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write);
        GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress);
        StreamWriter writer = new StreamWriter(compressionStream);
        writer.Write(data);
        writer.Close();
    }

    static string LoadCompressedFile(string filename) {
        FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
        GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Decompress);
        StreamReader reader = new StreamReader(compressionStream);
        string data = reader.ReadToEnd();
        reader.Close();
        return data;
    }

    static void Main(string[] args) {
        try {
            string filename = "compressedFile.txt";
            string sourceString = "Source String";
            SaveCompressedFile(filename, sourceString);
            FileInfo compressedFileData = new FileInfo(filename);
            string recoveredString = LoadCompressedFile(filename);
        } catch (IOException ex) {
            Console.WriteLine(ex.ToString());
        }
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo File Stream
» GZipStream