C# 通过ICSharpCode.SharpZipLib实现文件压缩解压下载

通过管理NuGet包添加ICSharpCode.SharpZipLib引用以完成,多文件或者文件夹压缩后下载效果
1、压缩文件实体类

      /// <summary>
    /// 文件路径集合,文件名集合,压缩文件名
    /// </summary>
    public class FileNameListZip
    {
   
        /// <summary>
        /// 文件路径集合,文件名集合
        /// </summary>
        public Dictionary<string, string> fileDictionary {
    get; set; }
        /// <summary>
        /// 压缩文件名
        /// </summary>
        public string fileZipName {
    get; set; }
    }

2、文件压缩解压帮助类

    /// <summary>
    /// 压缩文件辅助类
    /// </summary>
    public class ZipHelper
    {
   
        #region 压缩文件
        /// <summary>
        /// 压缩单个文件
        /// </summary>
        /// <param name="FileToZip">需要压缩的文件(绝对路径)</param>
        /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
        /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件同名)</param>
        /// <param name="CompressionLevel">压缩等级(0 无 - 9 最高,默认 5)</param>
        /// <param name="BlockSize">缓存大小(每次写入文件大小,默认 2048)</param>
        /// <param name="PassWord">加密密码(默认 123456)</param>
        /// <param name="IsEncrypt">是否加密(默认 不加密)</param>
        public static void ZipFile(string FileToZip, string ZipedPath, string ZipedFileName = "", int CompressionLevel = 5, int BlockSize = 2048, string PassWord = "123456", bool IsEncrypt = false)
        {
   
            //如果文件没有找到,则报错
            if (!System.IO.File.Exists(FileToZip))
            {
   
                throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
            }
            //文件名称(默认同源文件名称相同)
            string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new FileInfo(FileToZip).Name.Substring(0, new FileInfo(FileToZip).Name.LastIndexOf('.')) + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";
            using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
            {
   
                using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
                {
   
                    using (System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                    {
   
                        string fileName = FileToZip.Substring(FileToZip.LastIndexOf("\\") + 1);
                        ZipEntry ZipEntry = new ZipEntry(fileName);
                        if (IsEncrypt)
                        {
   
                            //压缩文件加密
                            ZipStream.Password = PassWord;
                        }
                        ZipStream.PutNextEntry(ZipEntry);
                        //设置压缩级别
                        ZipStream.SetLevel(CompressionLevel);
                        //缓存大小
                        byte[] buffer = new byte[BlockSize];
                        int sizeRead = 0;
                        try
                        {
   
                            do
                            {
   
                                sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                                ZipStream.Write(buffer, 0, sizeRead);
                            }
                            while (sizeRead > 0);
                        }
                        catch (System.Exception ex)
                        {
   
                            throw ex;
                        }
                        StreamToZip.Close();
                    }
                    ZipStream.Finish();
                    ZipStream.Close();
                }
                ZipFile.Close();
            }
        }
        /// <summary> 
        /// 压缩多个文件
        /// </summary>
        /// <param name="filePathList">要进行压缩的文件路径</param>
        /// <param name="fileNameList">要进行压缩的文件名</param>
        /// <param name="zipedFilePath">压缩后生成的压缩所在文件夹</param>
        /// <param name="zipedFileName">压缩后生成的压缩文件名</param>
        /// <param name="Level">压缩等级(0 无 - 9 最高,默认 5)</param>
        public static void ZipFileList(List<string> filePathList, List<string> fileNameList,string zipedFilePath, string zipedFileName, int Level = 5)
        {
   
            FileNameListZip fileNameListZip = GetFileName(filePathList, fileNameList, zipedFilePath, zipedFileName);
            using (FileStream ZipFile = System.IO.File.Create(fileNameListZip.fileZipName))
            {
   
                using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
                {
   
                    try
                    {
   
                        foreach (var fileToZip in fileNameListZip.fileDictionary)
                        {
   
                            string fileName = fileToZip.Value;
                            using (FileStream fs = System.IO.File.OpenRead(fileToZip.Key))
                            {
   
                                byte[] buffer = new byte[fs.Length];
                                fs.Read(buffer, 0, buffer.Length);
                                fs.Close();
                                ZipEntry ZipEntry = new ZipEntry(fileName);
                                ZipStream.PutNextEntry(ZipEntry);
                                ZipStream.SetLevel(Level);
                                ZipStream.Write(buffer, 0, buffer.Length);
                            }
                        }
                    }
                    finally
                    {
   
                        if (ZipStream != null)
                        {
   
                            ZipStream.Dispose();
                            ZipStream.Finish();
                            ZipStream.Close();
                        }
                    }
                }
            }

        }
        /// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param>
        /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
        /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param>
        /// <param name="PassWord">压缩加密密码(默认 123456)</param>
        /// <param name="IsEncrypt">是否加密(默认 不加密)</param>
        public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", string PassWord = "123456", bool IsEncrypt = false)
        {
   
            //如果目录不存在,则报错
            if (!System.IO.Directory.Exists(DirectoryToZip))
            {
   
                throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!");
            }
            //文件名称(默认同源文件名称相同)
            string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";

            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值