26 Temmuz 2011 Salı

Compress JSON Decompress JSON

using System.Text;
using System.IO;
using System.IO.Compression;

  public static void CompressJson(string json, string path)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(json);

            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                using (GZipStream gzs = new GZipStream(fs, CompressionMode.Compress))
                {
                    gzs.Write(bytes, 0, bytes.Length);
                }
            }
        }
 
 public static string DecompressJson(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                using (GZipStream gzs = new GZipStream(fs, CompressionMode.Decompress))
                {
                    return gzs.GetString(Encoding.UTF8);
                }
            }
        }

Hiç yorum yok:

Yorum Gönder