• 文件压缩和解压 FileStream GZipStream


      using (FileStream reader=new FileStream (@"c:1.txt",FileMode.Open,FileAccess.Read))
                {
                    using (FileStream writer=new FileStream (@"c:4.zp",FileMode.OpenOrCreate,FileAccess.Write))
                    {
                        //压缩写的流
                        using (GZipStream zip=new GZipStream (writer,CompressionMode.Compress))
                        {
                            byte[] buffer = new byte[12];
                            int r = 0;
                            while ((r=reader.Read(buffer,0,buffer.Length))>0)
                            {
                                //压缩写入
                                zip.Write(buffer,0,r);
                                
                            }
                        }
                    }
                }
                Console.WriteLine("/压缩完成");
    
                using (FileStream reader = new FileStream(@"c:4.zp", FileMode.OpenOrCreate, FileAccess.Read))
                {
                    using (FileStream writer = new FileStream(@"c:5.txt", FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        byte[] buffer = new byte[13];
                        //解压读的流 因读的是压缩文件,reader读出的是压缩后的流
                        using (GZipStream zip = new GZipStream(reader, CompressionMode.Decompress))
                        {
                            int r=0;
                            
                            while ((r = zip.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                //正常的流写入
                                writer.Write(buffer, 0, r);
                            }
                        }
    
                    }
                }

    这种压缩只能实现单个文件的压缩和解压,多个文件的暂时不能使用

  • 相关阅读:
    sscanf功能详解(转)
    String to Integer (atoi)
    Reverse Words in a String
    在一个字符串中寻找某个字串
    回文数
    Two Sum
    java 判断牌型?
    股票的最大利润
    队列的最大值
    加密和解密例子
  • 原文地址:https://www.cnblogs.com/nanxiaoxiang/p/6558979.html
Copyright © 2020-2023  润新知