class Program
    {
        static void Main(string[] args)
        {
            //第一种方法
            //第一个路径是要压缩的文件路径,第二个路径是压缩到哪里
            ZipFile.CreateFromDirectory(@"E:\AA", @"E:\BB.zip"); //压缩
            ZipFile.ExtractToDirectory(@"E:\BB.zip", @"E:\CC");  //解压

            Console.Read();
        }

        //第二种方法
        //压缩
        private static void Compress()
        {
            FileStream fs = new FileStream(@"e:\AA.txt", FileMode.Open);

            FileStream ds = new FileStream(@"e:\BB.dat", FileMode.Create);
            GZipStream g = new GZipStream(ds, CompressionMode.Compress);

            byte[] buffers = new byte[1024];
            int count = fs.Read(buffers, 0, 1024);

            while (count>0)
            {
                g.Write(buffers, 0, count);
                count = fs.Read(buffers, 0, 1024);
            }

            g.Close();
            fs.Close();
            ds.Close();

            Console.Read();
        }
        //解压
        private static void Decompress()
        {
            FileStream fs = new FileStream(@"e:\BB.dat", FileMode.Open);

            FileStream ds = new FileStream(@"e:\CC.txt", FileMode.Create);
            GZipStream g = new GZipStream(fs, CompressionMode.Decompress);

            byte[] buffers = new byte[1024];
            int count = g.Read(buffers, 0, 1024);

            while (count > 0)
            {
                ds.Write(buffers, 0, count);
                count = g.Read(buffers, 0, 1024);
            }

            g.Close();
            fs.Close();
            ds.Close();

            Console.Read();
        }
    }
posted on 2019-11-18 16:34  .net之路  阅读(74)  评论(0编辑  收藏  举报