• 压缩与解压


    //1.创建读取文本文件的流
    using (FileStream fsRead = File.OpenRead(@"D:YY.TXT"))
    {
    //2.创建写文件流
    using (FileStream fsWrite = File.OpenWrite(@"D:XX.rar"))
    {
    //3.创建压缩流
    using (GZipStream zipStream = new GZipStream(fsWrite, CompressionMode.Compress))
    {
    byte[] byts = new byte[1024];
    int len = 0;
    //4.通过读取文件流读取数据
    while ((len = fsRead.Read(byts, 0, byts.Length)) > 0)
    {

    //通过压缩流写入数据
    zipStream.Write(byts, 0, len);

    }
    }
    }
    }

    ---------------------------

    //1.创建读取流
    using (FileStream fsRead = File.OpenRead(@"D:XX.rar"))
    {
    //2.创建压缩流,把读取流作为参数,
    using (GZipStream zip = new GZipStream(fsRead, CompressionMode.Decompress))
    {
    //创建写入流
    using (FileStream fsWrite = File.OpenWrite(@"D:Z.TXT"))
    {
    byte[] byts = new byte[1024];
    int len = 0;//用于表示真是接受到是字节个数
    //通过压缩流读取数据
    while ((len = zip.Read(byts, 0, byts.Length)) > 0)
    {
    //MessageBox.Show(Encoding.UTF8.GetString(byts.Take(len).ToArray()));
    //通过文件流写入文件
    fsWrite.Write(byts, 0, len);//读取的长度为len,这样不会造成数据的错误
    }
    }
    }
    }

  • 相关阅读:
    SimpleDateFormat解析的时区问题
    linux之cp/scp命令+scp命令详解
    java.net.SocketException: java.security.NoSuchAlgorithmException
    Gradle使用手册(一):为什么要用Gradle?
    js_实用
    exp.validate.js
    MySQL实用技巧
    MongoDB 用户配置
    js 图片处理 Jcrop.js API
    MySQL连接池
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/11018750.html
Copyright © 2020-2023  润新知