• C#文件压缩


    /// <summary>
    /// 文件压缩
    /// </summary>
    /// <param name="filesUrl">多个文件路径</param>
    /// <param name="zipStr">压缩文件名</param>
    public static void ZipFile(List<string> filesUrl, string zipStr)
    {
    try
    {
    
    //这行代码表示文件名称有中文字。不然好像会出现乱码
    Encoding gbk = Encoding.GetEncoding("gbk");
    ZipConstants.DefaultCodePage = gbk.CodePage;
    if (filesUrl != null && filesUrl.Count > 0)
    {
    ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipStr));
    zipStream.SetLevel(6);
    foreach (string itemStr in filesUrl)
    {
    zip(itemStr, zipStream);
    }
    zipStream.Finish();
    zipStream.Close();
    }
    }
    catch (Exception ex)
    {
    Common.SetException("类库", ex);
    }
    }
    /// <summary>
    /// 填写文件
    /// </summary>
    /// <param name="fileStr">文件路径</param>
    /// <param name="zipStream"></param>
    private static void zip(string fileStr, ZipOutputStream zipStream)
    {
    try
    {
    Crc32 crc = new Crc32();
    if (File.Exists(fileStr))
    {
    FileStream fs = File.OpenRead(fileStr);
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    ZipEntry entry = new ZipEntry(fileStr.Substring(fileStr.LastIndexOf("\") + 1));
    
    entry.DateTime = DateTime.Now;
    entry.Size = fs.Length;
    
    fs.Close();
    crc.Reset();
    crc.Update(buffer);
    entry.Crc = crc.Value;
    zipStream.PutNextEntry(entry);
    zipStream.Write(buffer, 0, buffer.Length);
    }
    }
    catch (Exception ex)
    {
    Common.SetException("类库", ex);
    }
    }

    压缩DLL下载 https://files.cnblogs.com/files/changeMe/SharpZipLib.rar

  • 相关阅读:
    BZOJ 2157: 旅游 (2017.7.21 6:30-2017.7.21 15:38 今日第一题。。)
    洛谷 P1021 邮票面值设计
    洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking
    COGS 2111. [NOIP2015普及]扫雷游戏
    洛谷 P3038 [USACO11DEC]牧草种植Grass Planting
    COGS 1439. [NOIP2013]货车运输
    COGS 908. 校园网
    codevs 1422 河城荷取
    codevs 1183 泥泞的道路
    洛谷 P3390 【模板】矩阵快速幂
  • 原文地址:https://www.cnblogs.com/changeMe/p/4421465.html
Copyright © 2020-2023  润新知