压缩流--DeflateStream和GZipStream

//使用DeflateStream压缩和解压缩文件

namespace 压缩流1
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs;
            DeflateStream deflate;
            FileInfo myFile;
            string originalXML = "original.xml";
            string compressedXML = "commpressed";
            string decompressedXML = "decommpressed.xml";
            DataSet ds = new DataSet();

            myFile = new FileInfo(originalXML);
            Console.WriteLine("原文件{0}中包含{1}字节", myFile.Name, myFile.Length.ToString());
            ds.ReadXml(originalXML);
            //创建一个文件
            fs = new FileStream(compressedXML, FileMode.Create, FileAccess.Write);
            //对文件进行压缩
            deflate = new DeflateStream(fs, CompressionMode.Compress);
            //保存XML文件
            ds.WriteXml(deflate);
            //关闭流
            deflate.Close();

            myFile = new FileInfo(compressedXML);
            Console.WriteLine("文件被压缩成{0}字节,存放在{1}中", myFile.Length.ToString(), myFile.Name);
            //创建一个文件
            fs = new FileStream(compressedXML, FileMode.Open, FileAccess.Read);
            //对文件进行解压
            deflate = new DeflateStream(fs, CompressionMode.Decompress);

            ds = new DataSet();
            ds.ReadXml(deflate);
            ds.WriteXml(decompressedXML);

            myFile = new FileInfo(decompressedXML);
            Console.WriteLine("文件被解压成{0}字节,存放在{1}中", myFile.Length.ToString(), myFile.Name);
           

            Console.ReadLine();
        }
    }

 

 

//使用GZipStream压缩和解压缩文件,与上一个基本一样

using System.IO;
using System.IO.Compression;
using System.Data;

namespace 压缩流2
{
    class Program
    {
        static void Main(string[] args)
        {
            //GZipStream解压缩文件
            FileStream fs;
            GZipStream gzip;
            FileInfo myFile;
            string originalXML = "original.xml";
            string compressedXML = "originalGZip.gz";
            string decompressedXML = "UnZip.xml";
            DataSet ds = new DataSet();

            myFile = new FileInfo(originalXML);
            Console.WriteLine("原文件{0}中包含{1}字节", myFile.Name, myFile.Length.ToString());
            ds.ReadXml(originalXML);

            //压缩
            fs = new FileStream(compressedXML, FileMode.Create, FileAccess.Write);
            gzip = new GZipStream(fs, CompressionMode.Compress);
            ds.WriteXml(gzip);
            gzip.Close();

            myFile = new FileInfo(compressedXML);
            Console.WriteLine("文件被压缩成{0}字节,存放在{1}中", myFile.Length.ToString(), myFile.Name);

            fs = new FileStream(compressedXML, FileMode.Open, FileAccess.Read);
            gzip = new GZipStream(fs, CompressionMode.Decompress);

            ds = new DataSet();
            ds.ReadXml(gzip);
            ds.WriteXml(decompressedXML);

            myFile = new FileInfo(decompressedXML);
            Console.WriteLine("文件被解压成{0}字节,存放在{1}中", myFile.Length.ToString(), myFile.Name);
            Console.ReadLine();
        }
    }
}

posted @ 2012-04-09 15:11  大智若简  阅读(852)  评论(0编辑  收藏  举报