友情链接:.NET项目开发者博客

压缩流GZipStream

1>文件压缩过程:

     1.创建读取流File.OpenRead()
     2.创建写入流File.OpenWrite();
     3.创建压缩流new GZipStream();将写入流作为参数与。
     4.每次通过读取流读取一部分数据,通过压缩流写入。

2>文件解压过程

     1.创建读取流:File.OpenRead()
     2.创建压缩流:new GZipStream();将读取流作为参数
     3.创建写入流File.OpenWrite();
     4.每次通过压缩流读取数据,通过写入流写入数据。

文件压缩举例说明:

     //1.创建读取文本文件的流
            using (FileStream fsRead = File.OpenRead("a.txt"))
            {
                //2.创建写入文本文件的流
                using (FileStream fsWrite = File.OpenWrite("aa.txt"))
                {
                    //3.创建压缩流
                    using (GZipStrZipStream zipStream = eam zipStream = new GZipStream(fsWrite, CompressionMode.Compress))
                    {
                        //4.每次读取1024byte
                        byte[] byts = new byte[1024];
                        int len = 0;
                        while ((len = fsRead.Read(byts, 0, byts.Length)) > 0)
                        {
                            //通过压缩流写入文件
                            zipStream.Write(byts, 0, len);
                        }
                    }
                }
            }
            Console.WriteLine("ok");
            Console.ReadKey();

文件解举例说明: 

  using (FileStream fsRead = File.OpenRead("yasuo.txt"))

 {

   using (GZipStream gzipStream = new GZipStream(fsRead, CompressionMode.Decompress))                

               {                    

                       using (FileStream fsWrite = File.OpenWrite("jieya.txt"))                    

                       {

                                byte[] byts = new byte[1024];                        

                                int len = 0;                        

                               while ((len = gzipStream.Read(byts, 0, byts.Length)) > 0)                                        

                                 {                            

                                             fsWrite.Write(byts, 0, len);                        

                                          }

                                   }                

                     }            

              }            

                  Console.WriteLine("ok");            

                 Console.ReadKey();

posted @ 2013-03-17 23:17  千年老妖  Views(338)  Comments(0Edit  收藏  举报
友情链接:.NET项目开发者博客