Net Core教程

C#使用ICSharpCode.SharpZipLib.dll压缩多个文件

本文主要是介绍C#使用ICSharpCode.SharpZipLib.dll压缩多个文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C#使用ICSharpCode.SharpZipLib.dll压缩多个文件
https://www.cnblogs.com/zhuyongblogs/p/5178164.html

首先通过NuGet管理安装ICSharpCode.SharpZipLib.dll

以下是压缩的通用方法:

复制代码
using System;
using System.IO;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using ICSharpCode.SharpZipLib.Zip;
namespace Common

{

/// <summary>

/// 压缩文件帮助类

/// </summary>

public static class ZipHelper

{

/// <summary>

/// 压缩多个文件

/// </summary>

/// <param name="filesToZip">要压缩的文件的相对路径集合</param>

/// <param name="zipedFileName">压缩后的文件名</param>
     /// <param name="zipPassword">压缩密码</param>

/// <param name="blockSize">每次写入的缓冲区大小</param>

/// <param name="zipLevel">压缩等级(0-9)</param>

/// <returns></returns>

public static string ZipFile(List<string> filesToZip, string zipedFileName, string zipPassword = "", int blockSize = 2048, int zipLevel = 9)

{

try

{

//压缩后的压缩文件相对路径

var newFileName = @"~/UploadFiles/Temp/" + zipedFileName + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".zip";
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">压缩后的压缩文件物理地址</span>
            <span style="color: rgba(0, 0, 255, 1)">var</span> zipedFilePath =<span style="color: rgba(0, 0, 0, 1)"> HttpContext.Current.Server.MapPath(newFileName);

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取所有文件的物理地址</span>
            List&lt;<span style="color: rgba(0, 0, 255, 1)">string</span>&gt; allFilesPath = <span style="color: rgba(0, 0, 255, 1)">new</span> List&lt;<span style="color: rgba(0, 0, 255, 1)">string</span>&gt;<span style="color: rgba(0, 0, 0, 1)">();
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (filesToZip != <span style="color: rgba(0, 0, 255, 1)">null</span> &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> filesToZip.Any())
            {
                filesToZip.ForEach(file </span>=&gt;<span style="color: rgba(0, 0, 0, 1)">
                {
                    </span><span style="color: rgba(0, 0, 255, 1)">var</span> serverPath =<span style="color: rgba(0, 0, 0, 1)"> HttpContext.Current.Server.MapPath(file);
                    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (File.Exists(serverPath))
                    {
                        allFilesPath.Add(serverPath);
                    }
                });
            }

            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (allFilesPath.Any())
            {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建临时目录</span>
                <span style="color: rgba(0, 0, 255, 1)">var</span> directory = HttpContext.Current.Server.MapPath(<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">~/UploadFiles/Temp</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建压缩文件</span>
                ZipOutputStream zipStream = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ZipOutputStream(File.Create(zipedFilePath));
                zipStream.SetLevel(zipLevel);
                zipStream.Password </span>=<span style="color: rgba(0, 0, 0, 1)"> zipPassword;

                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">写入所有文件到压缩文件</span>
                <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i &lt; allFilesPath.Count; i++<span style="color: rgba(0, 0, 0, 1)">)
                {
                    </span><span style="color: rgba(0, 0, 255, 1)">string</span> strFilePath =<span style="color: rgba(0, 0, 0, 1)"> allFilesPath[i];<br>              FileStream fs = null;
                    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
                    {
                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">被压缩的文件名</span>
                        <span style="color: rgba(0, 0, 255, 1)">string</span> strFileName = strFilePath.Substring(strFilePath.LastIndexOf(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">\\</span><span style="color: rgba(128, 0, 0, 1)">"</span>) + <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">);

                        ZipEntry entry </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ZipEntry(strFileName);
                        entry.DateTime </span>=<span style="color: rgba(0, 0, 0, 1)"> DateTime.Now;
                        zipStream.PutNextEntry(entry);

                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">读取文件</span>
                        fs =<span style="color: rgba(0, 0, 0, 1)"> File.OpenRead(strFilePath);

                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">缓冲区大小</span>
                        <span style="color: rgba(0, 0, 255, 1)">byte</span>[] buffer = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[blockSize];
                        </span><span style="color: rgba(0, 0, 255, 1)">int</span> sizeRead = <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
                        </span><span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)">
                        {
                            sizeRead </span>= fs.Read(buffer, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, buffer.Length);
                            zipStream.Write(buffer, </span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, sizeRead);
                        }
                        </span><span style="color: rgba(0, 0, 255, 1)">while</span> (sizeRead &gt; <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);
                    }
                    </span><span style="color: rgba(0, 0, 255, 1)">catch <span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 0, 1)">(Exception ex)</span></span></span><span style="color: rgba(0, 0, 0, 1)">
                    {
                        //</span><span style="color: rgba(0, 0, 255, 1)">continue</span><span style="color: rgba(0, 0, 0, 1)">;
                    }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(0, 0, 255, 1)">finally</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (fs != null)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fs.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fs.Dispose();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
                }

                zipStream.Finish();
                zipStream.Close();<br>

            //返回压缩后的压缩文件相对路径

return newFileName;

}
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty;
        }
        </span><span style="color: rgba(0, 0, 255, 1)">catch<span style="color: rgba(0, 0, 0, 1)"> (Exception ex)</span></span><span style="color: rgba(0, 0, 0, 1)">
        {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty;
        }
    }
}

}

复制代码

调用:

//要压缩的附件相对路径集合
List<string> filesToZip = new List<string>();
var ziped_file = ZipHelper.ZipFile(filesToZip, "压缩后的文件名");

 

这篇关于C#使用ICSharpCode.SharpZipLib.dll压缩多个文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!