首页 > Windows开发 > 详细

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

时间:2016-02-02 18:46:53      阅读:290      评论:0      收藏:0      [点我收藏+]

首先通过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="blockSize">每次写入的缓冲区大小</param>
        /// <param name="zipLevel">压缩等级(0-9)</param>
        /// <param name="zipPassword">压缩密码</param>
        /// <returns></returns> 
        public static string ZipFile(List<string> filesToZip, string zipedFileName, int blockSize = 2048, int zipLevel = 9, string zipPassword = "")
        {
            try
            {
                //压缩后的压缩文件相对路径
                var newFileName = @"~/UploadFiles/Temp/" + zipedFileName + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".zip";
                
                //压缩后的压缩文件物理地址
                var zipedFilePath = HttpContext.Current.Server.MapPath(newFileName);

                //获取所有文件的物理地址
                List<string> allFilesPath = new List<string>();
                if (filesToZip != null && filesToZip.Any())
                {
                    filesToZip.ForEach(file =>
                    {
                        var serverPath = HttpContext.Current.Server.MapPath(file);
                        if (File.Exists(serverPath))
                        {
                            allFilesPath.Add(serverPath);
                        }
                    });
                }

                if (allFilesPath.Any())
                {
                    //创建临时目录
                    var directory = HttpContext.Current.Server.MapPath(@"~/UploadFiles/Temp");
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }

                    //创建压缩文件
                    ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFilePath));
                    zipStream.SetLevel(zipLevel);
                    zipStream.Password = zipPassword;

                    //写入所有文件到压缩文件
                    for (int i = 0; i < allFilesPath.Count; i++)
                    {
                        string strFilePath = allFilesPath[i];
                        try
                        {
                            //被压缩的文件名
                            string strFileName = strFilePath.Substring(strFilePath.LastIndexOf("\\") + 1);

                            ZipEntry entry = new ZipEntry(strFileName);
                            entry.DateTime = DateTime.Now;
                            zipStream.PutNextEntry(entry);

                            //读取文件
                            FileStream fs = File.OpenRead(strFilePath);

                            //缓冲区大小
                            byte[] buffer = new byte[blockSize];
                            int sizeRead = 0;
                            do
                            {
                                sizeRead = fs.Read(buffer, 0, buffer.Length);
                                zipStream.Write(buffer, 0, sizeRead);
                            }
                            while (sizeRead > 0);

                            fs.Close();
                            fs.Dispose();
                        }
                        catch
                        {
                            continue;
                        }
                    }

                    zipStream.Finish();
                    zipStream.Close();
            //返回压缩后的压缩文件相对路径
return newFileName; } return string.Empty; } catch { return string.Empty; } } } }

调用:

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

 

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

原文:http://www.cnblogs.com/zhuyongblogs/p/5178164.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!