.NET GZip压缩和解压缩文件

来源:互联网 发布:linux sh 开机启动 编辑:程序博客网 时间:2024/06/09 18:15

   //压缩文件 

public static void ZGip(string fileName, string gipFileName)

        { 
         
            FileStream fc = File.OpenRead(fileName);
            FileStream fr = File.Create(gipFileName);
            GZipStream gzs = new GZipStream(fr, CompressionMode.Compress); //压缩文件类
            /*
                     int thebyte=fc.ReadByte();
                    while(thebyte!=-1)
                    {
                         gzs.WriteByte((byte)thebyte);
                        thebyte=fc.ReadByte();
                    }
              */         
            byte []arr = new byte[fc.Length];
            fc.Read(arr, 0, (int)fc.Length);
            gzs.Write(arr, 0, (int)fc.Length);
            gzs.Close();
            fc.Close();
            fr.Close();
            

        }

//解压缩文件方法

        public static void DeZGip(string fileName, string gipFileName)
        {
            //准备输入输出文件
            FileStream fc = File.Create(fileName);
            FileStream fr = File.OpenRead(gipFileName);

            GZipStream gzs = new GZipStream(fr, CompressionMode.Decompress);

            byte[] arr = new byte[fr.Length];
            fr.Read(arr, 0, (int)fr.Length);
            fc.Write(arr, 0, (int)fr.Length);
            gzs.Close();
            fr.Close();
            fc.Close();
        }
原创粉丝点击