C#文件操作

public static void Compress(string sourceFile, string destFile)

        {

            FileStream fs = File.OpenRead(sourceFile);

            byte[] bs = new byte[fs.Length];

            fs.Read(bs, 0, bs.Length);

            fs.Close();



            FileStream fd = File.Create(destFile);

            GZipStream gs = new GZipStream(fd, CompressionMode.Compress);

            //DeflateStream ds = new DeflateStream(fs, CompressionMode.Compress);

            gs.Write(bs, 0, bs.Length);

            gs.Close();

            fd.Close();



        }



        public static void Decompress(string sourceFile, string destFile)

        {

            FileStream fs = File.OpenRead(sourceFile);

            FileStream fd = File.Create(destFile);

            GZipStream gs = new GZipStream(fs, CompressionMode.Decompress);

            //byte[] bs = new byte[gs.Length];

            //gs.Read(bs, 0, bs.Length);

            //gs.Close();

            //fs.Close();

            //fd.Write(bs, 0, bs.Length);

            //fd.Flush();

            //fd.Close();

            int b = gs.ReadByte();

            while (b != -1)

            {

                fd.WriteByte((byte)b);

                b = gs.ReadByte();

            }

            fd.Flush();

            fd.Close();

            gs.Close();

            fs.Close();



        }

单文件压缩与解压

你可能感兴趣的:(文件操作)