首页 > 编程语言 >C# 通过ICSharpCode.SharpZipLib实现文件压缩下载

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

时间:2023-05-05 15:13:10浏览次数:46  
标签:文件 string C# System buffer IO ICSharpCode new 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";

            System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName);
            ZipOutputStream s = new ZipOutputStream(ZipFile);
            if (IsEncrypt)
            {
                //压缩文件加密
                s.Password = PassWord;
            }
            ZipSetp(DirectoryToZip, s, "");
        }
        /// <summary>
        /// 压缩文件(并保存至服务器)
        /// </summary>
        /// <param name="filePathList">要压缩的所有文件(完全路径)</param>
        /// <param name="fileNameList">要压缩名称名称</param>
        /// <param name="zipedFileName">压缩后文件夹</param>
        /// <param name="zipedFileName">压缩后文件名称</param>
        /// <param name="Level">压缩级别</param>
        public static bool ZipFileMain(List<string> filePathList, List<string> fileNameList, string zipedFilePath, string zipedFileName, int Level = 5)
        {
            FileNameListZip fileNameListZip = GetFileName(filePathList, fileNameList, zipedFilePath, zipedFileName);
            ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(fileNameListZip.fileZipName));
            Crc32 crc = new Crc32();
            //压缩级别
            s.SetLevel(Level);
            try
            {
                foreach (var file in fileNameListZip.fileDictionary)
                {
                    //打开压缩文件
                    FileStream fs = System.IO.File.OpenRead(file.Key);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    //建立压缩实体
                    ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(file.Value));
                    //时间
                    entry.DateTime = DateTime.Now;
                    //空间大小
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                s.Finish();
                s.Close();
            }
        }
        /// <summary>
        /// 压缩多个文件/文件夹
        /// </summary>
        /// <param name="sourceList">源文件/文件夹路径列表</param>
        /// <param name="zipFilePath">压缩文件路径</param>
        /// <param name="comment">注释信息</param>
        /// <param name="password">压缩密码</param>
        /// <param name="compressionLevel">压缩等级,范围从0到9,可选,默认为6</param>
        /// <param name="BufferSize">分段大小默认1024</param>
        /// <returns></returns>
        public static bool CompressFile(IEnumerable<string> sourceList, string zipFilePath, string comment = null, string password = null, int compressionLevel = 6, int BufferSize = 1024)
        {
            bool result = false;
            try
            {
                //检测目标文件所属的文件夹是否存在,如果不存在则建立
                string zipFileDirectory = Path.GetDirectoryName(zipFilePath);
                if (!Directory.Exists(zipFileDirectory))
                {
                    Directory.CreateDirectory(zipFileDirectory);
                }
                Dictionary<string, string> dictionaryList = PrepareFileSystementities(sourceList);
                using (ZipOutputStream zipStream = new ZipOutputStream(System.IO.File.Create(zipFilePath)))
                {
                    zipStream.Password = password;//设置密码
                    zipStream.SetComment(comment);//添加注释
                    zipStream.SetLevel(compressionLevel);//设置压缩等级
                    foreach (string key in dictionaryList.Keys)//从字典取文件添加到压缩文件
                    {
                        if (System.IO.File.Exists(key))//判断是文件还是文件夹
                        {
                            FileInfo fileItem = new FileInfo(key);

                            using (FileStream readStream = fileItem.Open(FileMode.Open,
                                FileAccess.Read, FileShare.Read))
                            {
                                ZipEntry zipEntry = new ZipEntry(dictionaryList[key]);
                                zipEntry.DateTime = fileItem.LastWriteTime;
                                zipEntry.Size = readStream.Length;
                                zipStream.PutNextEntry(zipEntry);
                                int readLength = 0;
                                byte[] buffer = new byte[BufferSize];

                                do
                                {
                                    readLength = readStream.Read(buffer, 0, BufferSize);
                                    zipStream.Write(buffer, 0, readLength);
                                } while (readLength == BufferSize);

                                readStream.Close();
                            }
                        }
                        else//对文件夹的处理
                        {
                            ZipEntry zipEntry = new ZipEntry(dictionaryList[key] + "/");
                            zipStream.PutNextEntry(zipEntry);
                        }
                    }
                    zipStream.Flush();
                    zipStream.Finish();
                    zipStream.Close();
                }
                result = true;
            }
            catch (System.Exception ex)
            {
                throw new Exception("压缩文件失败", ex);
            }
            return result;
        }
        #endregion 压缩文件

        #region 解压
        /// <summary>
        /// 解压一个zip文件
        /// </summary>
        /// <param name="ZipFile">需要解压的Zip文件(绝对路径)</param>
        /// <param name="TargetDirectory">解压到的目录</param>
        /// <param name="Password">解压密码</param>
        /// <param name="OverWrite">是否覆盖已存在的文件</param>
        public static void UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
        {
            //如果解压到的目录不存在,则报错
            if (!System.IO.Directory.Exists(TargetDirectory))
            {
                throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");
            }
            //目录结尾
            if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; }
            ZipInputStream zipfiles = new ZipInputStream(System.IO.File.OpenRead(ZipFile));
            if (!string.IsNullOrWhiteSpace(Password))
            {
                zipfiles.Password = Password;
            }
            ZipEntry theEntry;
            while ((theEntry = zipfiles.GetNextEntry()) != null)
            {
                string directoryName = "";
                string pathToZip = "";
                pathToZip = theEntry.Name;
                if (pathToZip != "")
                {
                    directoryName = Path.GetDirectoryName(pathToZip) + "\\";
                }
                string fileName = Path.GetFileName(pathToZip);
                Directory.CreateDirectory(TargetDirectory + directoryName);
                if (fileName != "")
                {
                    if ((System.IO.File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!System.IO.File.Exists(TargetDirectory + directoryName + fileName)))
                    {
                        using (FileStream streamWriter = System.IO.File.Create(TargetDirectory + directoryName + fileName))
                        {
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = zipfiles.Read(data, 0, data.Length);

                                if (size > 0)
                                    streamWriter.Write(data, 0, size);
                                else
                                    break;
                            }
                            streamWriter.Close();
                        }
                    }
                }
            }
            zipfiles.Close();

        }
        /// <summary>
        /// 解压文件到指定文件夹
        /// </summary>
        /// <param name="sourceFile">压缩文件</param>
        /// <param name="destinationDirectory">目标文件夹,如果为空则解压到当前文件夹下</param>
        /// <param name="password">密码</param>
        /// <param name="BufferSize">分段大小</param>
        /// <returns></returns>
        public static bool DecomparessFile(string sourceFile, string destinationDirectory = null, string password = null, int BufferSize = 1024)
        {
            bool result = false;
            if (!System.IO.File.Exists(sourceFile))
            {
                throw new FileNotFoundException("要解压的文件不存在", sourceFile);
            }
            if (string.IsNullOrWhiteSpace(destinationDirectory))
            {
                destinationDirectory = Path.GetDirectoryName(sourceFile);
            }
            try
            {
                if (!Directory.Exists(destinationDirectory))
                {
                    Directory.CreateDirectory(destinationDirectory);
                }
                using (ZipInputStream zipStream = new ZipInputStream(System.IO.File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    zipStream.Password = password;
                    ZipEntry zipEntry = zipStream.GetNextEntry();
                    while (zipEntry != null)
                    {
                        if (zipEntry.IsDirectory)//如果是文件夹则创建
                        {
                            Directory.CreateDirectory(Path.Combine(destinationDirectory,
                                Path.GetDirectoryName(zipEntry.Name)));
                        }
                        else
                        {
                            string fileName = Path.GetFileName(zipEntry.Name);
                            if (!string.IsNullOrEmpty(fileName) && fileName.Trim().Length > 0)
                            {
                                FileInfo fileItem = new FileInfo(Path.Combine(destinationDirectory, zipEntry.Name));
                                using (FileStream writeStream = fileItem.Create())
                                {
                                    byte[] buffer = new byte[BufferSize];
                                    int readLength = 0;
                                    do
                                    {
                                        readLength = zipStream.Read(buffer, 0, BufferSize);
                                        writeStream.Write(buffer, 0, readLength);
                                    } while (readLength == BufferSize);
                                    writeStream.Flush();
                                    writeStream.Close();
                                }
                                fileItem.LastWriteTime = zipEntry.DateTime;
                            }
                        }
                        zipEntry = zipStream.GetNextEntry();//获取下一个文件
                    }
                    zipStream.Close();
                }
                result = true;
            }
            catch (System.Exception ex)
            {
                throw new Exception("文件解压发生错误", ex);
            }
            return result;
        }
        #endregion

        #region 辅助方法
        /// <summary>
        /// 处理文件名
        /// </summary>
        /// <param name="filePathList">文件地址集合</param>
        /// <param name="fileNameList">文件名集合</param>
        /// <param name="zipFilePath">压缩包地址</param>
        /// <param name="zipedFile">压缩包名称</param>
        /// <returns></returns>
        public static FileNameListZip GetFileName(List<string> filePathList, List<string> fileNameList, string zipFilePath, string zipedFile)
        {
            ///文件重名处理
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            //文件路径与文件名字典
            Dictionary<string, string> fileDictionary = new Dictionary<string, string>();
            string fileZipName = string.Empty;
            FileNameListZip fileNameListZip = new FileNameListZip();
            if (filePathList != null && filePathList.Count > 0)
            {
                for (int i = 0; i < filePathList.Count; i++)
                {
                    if (System.IO.File.Exists(filePathList[i]))
                    {
                        string filename = Path.GetFileName(fileNameList[i]);//返回带扩展名的文件名 "default.avi"
                        string extension = Path.GetExtension(fileNameList[i]);//扩展名 ".aspx"
                        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileNameList[i]);// 没有扩展名的文件名 "default"
                        if (dictionary.ContainsKey(filename))
                        {
                            var count = dictionary[filename] + 1;
                            dictionary[filename] = count;
                            var name = fileNameWithoutExtension + "(" + count + ")" + extension;
                            fileDictionary.Add(filePathList[i], name);
                        }
                        else
                        {
                            dictionary.Add(filename, 1);
                            fileDictionary.Add(filePathList[i], filename);
                        }
                    }
                }
                fileNameListZip.fileDictionary = fileDictionary;
                if (!Directory.Exists(zipFilePath))
                {
                    Directory.CreateDirectory(zipFilePath);
                }
                fileNameListZip.fileZipName = zipFilePath + "\\" + zipedFile;
            }
            return fileNameListZip;
        }

        /// <summary>
        /// 递归遍历目录
        /// </summary>
        private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
        {
            if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
            {
                strDirectory += Path.DirectorySeparatorChar;
            }
            Crc32 crc = new Crc32();
            string[] filenames = Directory.GetFileSystemEntries(strDirectory);
            foreach (string file in filenames)// 遍历所有的文件和目录
            {
                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                {
                    string pPath = parentPath;
                    pPath += file.Substring(file.LastIndexOf("\\") + 1);
                    pPath += "\\";
                    ZipSetp(file, s, pPath);
                }
                else // 否则直接压缩文件
                {
                    //打开压缩文件
                    using (FileStream fs = System.IO.File.OpenRead(file))
                    {
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
                        ZipEntry entry = new ZipEntry(fileName);
                        entry.DateTime = DateTime.Now;
                        entry.Size = fs.Length;
                        fs.Close();
                        crc.Reset();
                        crc.Update(buffer);
                        entry.Crc = crc.Value;
                        s.PutNextEntry(entry);
                        s.Write(buffer, 0, buffer.Length);
                    }
                }
            }
        }
        /// <summary>
        /// 为压缩准备文件系统对象
        /// </summary>
        /// <param name="sourceFileEntityPathList"></param>
        /// <returns></returns>
        private static Dictionary<string, string> PrepareFileSystementities(IEnumerable<string> sourceFileEntityPathList)
        {
            Dictionary<string, string> fileEntityDictionary = new Dictionary<string, string>();//文件字典
            string parentDirectoryPath = "";
            foreach (string fileEntityPath in sourceFileEntityPathList)
            {
                string path = fileEntityPath;
                //保证传入的文件夹也被压缩进文件
                if (path.EndsWith(@"\"))
                {
                    path = path.Remove(path.LastIndexOf(@"\"));
                }

                parentDirectoryPath = Path.GetDirectoryName(path) + @"\";

                if (parentDirectoryPath.EndsWith(@":\\"))//防止根目录下把盘符压入的错误
                {
                    parentDirectoryPath = parentDirectoryPath.Replace(@"\\", @"\");
                }

                //获取目录中所有的文件系统对象
                Dictionary<string, string> subDictionary = GetAllFileSystemEntities(path, parentDirectoryPath);

                //将文件系统对象添加到总的文件字典中
                foreach (string key in subDictionary.Keys)
                {
                    if (!fileEntityDictionary.ContainsKey(key))//检测重复项
                    {
                        fileEntityDictionary.Add(key, subDictionary[key]);
                    }
                }
            }
            return fileEntityDictionary;
        }
        /// <summary>
        /// 获取所有文件系统对象
        /// </summary>
        /// <param name="source">源路径</param>
        /// <param name="topDirectory">顶级文件夹</param>
        /// <returns>字典中Key为完整路径,Value为文件(夹)名称</returns>
        private static Dictionary<string, string> GetAllFileSystemEntities(string source, string topDirectory)
        {
            Dictionary<string, string> entitiesDictionary = new Dictionary<string, string>();
            entitiesDictionary.Add(source, source.Replace(topDirectory, ""));

            if (Directory.Exists(source))
            {
                //一次性获取下级所有目录,避免递归
                string[] directories = Directory.GetDirectories(source, "*.*", SearchOption.AllDirectories);
                foreach (string directory in directories)
                {
                    entitiesDictionary.Add(directory, directory.Replace(topDirectory, ""));
                }

                string[] files = Directory.GetFiles(source, "*.*", SearchOption.AllDirectories);
                foreach (string file in files)
                {
                    entitiesDictionary.Add(file, file.Replace(topDirectory, ""));
                }
            }

            return entitiesDictionary;
        }
        #endregion
    }

3、文件下载帮助类,文件压缩下载方法为DownLoadoldZip

    /// <summary>
    /// 文件下载帮助类
    /// </summary>
    public class DownLoadoldHelper
    {
        #region 辅助方法
        /// <summary>
        /// 参数为虚拟路径
        /// </summary>
        public static string FileNameExtension(string FileName)
        {
            return Path.GetExtension(MapPathFile(FileName));
        }
        /// <summary>
        /// 获取物理地址
        /// </summary>
        public static string MapPathFile(string FileName)
        {
            return HttpContext.Current.Server.MapPath(FileName);
        }
        /// <summary>
        /// 验证文件是否存在
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static bool FileExists(string FileName)
        {
            string destFileName = FileName;
            if (System.IO.File.Exists(destFileName))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 普通下载
        /// <summary>
        /// 普通下载
        /// </summary>
        /// <param name="FileName">文件虚拟路径</param>
        ///  /// <param name="name">返回客户端名称</param>
        public static void DownLoadold(string FileName, string name)
        {
            string destFileName = FileName;
            if (System.IO.File.Exists(destFileName))
            {
                FileInfo fi = new FileInfo(destFileName);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Buffer = false;
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
                HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.WriteFile(destFileName);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
        }
        #endregion

        #region 多文件压缩并下载
        /// <summary>
        /// 批量下载(压缩包形式)
        /// </summary>
        /// <param name="filePathList">路径</param>
        /// <param name="fileNameList">文件名</param>
        /// <param name="fileSizeList">文件大小(字节)</param>
        /// <param name="zipFilePath">压缩包地址</param>
        /// <param name="zipFileName">压缩包名称</param>
        public static void DownLoadoldZip(List<string> filePathList, List<string> fileNameList, List<string> fileSizeList, string zipFilePath, string zipFileName)
        {
            //处理文件地址名,文件名,压缩后文件名
            FileNameListZip fileNameListZip = ZipHelper.GetFileName(filePathList, fileNameList, zipFilePath, zipFileName);

            #region 清除对应压缩包文件夹下,时间早于3天前的所有压缩包
            //获取文件夹下所有文件及子文件夹
            string[] zipFilePathList = Directory.GetFiles(zipFilePath);
            if (zipFilePathList != null && zipFilePathList.Length > 0)
            {
                //循环该文件夹下所有文件
                foreach (var file in zipFilePathList)
                {
                    //判断文件是否存在
                    if (System.IO.File.Exists(file))
                    {
                        FileInfo fileInfo = new FileInfo(file);
                        var createTime = fileInfo.CreationTime;//获取文件创建时间
                        //var firstWork = DateTime.Now.AddDays(0 - Convert.ToInt32(DateTime.Now.DayOfWeek) - 7);//获取当前时间的上周日
                        var firstWork = DateTime.Now.AddDays(-3);//获取当前时间的3天前当前时间
                        var compNum = DateTime.Compare(createTime, firstWork);//比较文件创建时间与当前时间的3天前时间
                        if (compNum < 0)//创建时间早于3天前
                        {
                            fileInfo.Delete();//永久删除文件
                        }
                    }
                }
            }
            #endregion 清除对应压缩包文件夹下,时间早于3天前的所有压缩包


            #region 生产压缩包实体文件并下载
            ZipHelper.ZipFileList(filePathList, fileNameList, zipFilePath, zipFileName);
            DownLoadByPath(fileNameListZip.fileZipName);
            #endregion 生产压缩包实体文件并下载
        }
        /// <summary>
        /// 下载文件,根据路径
        /// </summary>
        /// <param name="filePath">文件物理地址</param>
        public static void DownLoadByPath(string filePath)
        {
            long chunkSize = 204800;             //指定块大小 
            byte[] buffer = new byte[chunkSize]; //建立一个200K的缓冲区 
            long dataToRead = 0;                 //已读的字节数   
            FileStream stream = null;
            try
            {
                //打开文件   
                stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                dataToRead = stream.Length;

                //添加Http头   
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath), System.Text.Encoding.UTF8));
                HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString());
                while (dataToRead > 0)
                {
                    if (HttpContext.Current.Response.IsClientConnected)
                    {
                        int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
                        HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
                        HttpContext.Current.Response.Flush();
                        HttpContext.Current.Response.Clear();
                        dataToRead -= length;
                    }
                    else
                    {
                        dataToRead = -1; //防止client失去连接 
                    }
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write("Error:" + ex.Message);
            }
            finally
            {
                if (stream != null) stream.Close();
                HttpContext.Current.Response.Close();
            }
        }
        #endregion
    }

具体调用如图所示

生成压缩文件如图:

 并在压缩下载DownLoadoldZip方法中,我这里做了只保留三天内的压缩文件,当文件是在当前时间的三天前生成的则删掉,以免后续文件越来越多占用不必要的内存

 

标签:文件,string,C#,System,buffer,IO,ICSharpCode,new,SharpZipLib
From: https://www.cnblogs.com/lwk9527/p/17374168.html

相关文章

  • Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified
    导入一个新的springbootmaven项目启动一直报这个错,查出来的答案都说是加注解把数据库扫描给排除掉,这种方式其实有点鸵鸟,项目原先是没问题的,现在导入到自己的环境启动不起来,那肯定是不能去改动代码的。排查了一遍,发现是项目中的resources文件没有指定成资源文件,所以找不到数据库......
  • C#生成二维码(【ThoughtWorks.QRCode】【QRCoder】【ZXing.Net】)
    1、通过ThoughtWorks.QRCode实现生成二维码,可直接通过添加Nuget包引用///<summary>///ThoughtWorks.QRCode生成二维码///</summary>///<paramname="filePath">二维码生成后保存地址</param>///<paramname="qrCo......
  • Vue3 开发必备的 VSCode 插件
    分享6个Vue3开发必备的VSCode插件,可以直接用过VSCode的插件中心直接安装使用。1、Volar相信使用VSCode开发Vue2的同学一定对Vetur插件不会陌生,作为Vue2配套的VSCode插件,它的主要作用是对Vue单文件组件提供高亮、语法支持以及语法检测。而随着Vue3正式......
  • 《花雕学AI》31:ChatGPT--用关键词/咒语/提示词Prompt激发AI绘画的无限创意!
    你有没有想过用AI来画画?ChatGPT是一款基于GPT-3的聊天模式的AI绘画工具,它可以根据你输入的关键词/咒语/提示词Prompt来生成不同风格和主题的画作。Prompt是一些简短的文字,可以用来指导ChatGPT的创作过程。在这篇文章中,我将展示一些用ChatGPT和不同的Prompt创造出来的有趣和创意的A......
  • C# Spire.PDF 实现pdf文件盖章
    1、添加引用通过Spire.PDF实现合同盖章,社区版dll(免费,但是只支持10页以内的pdf文档),也可以直接通过VS管理NuGet包添加dll引用,收费版直接搜索Spire.PDF安装,免费社区版搜索FreeSpire.PDF安装2、参数定义与调用stringpdfPath="C:\\Users\\Administrator\\Desktop\\2月份工作......
  • 《CTFshow-Web入门》08. Web 71~80
    目录web71知识点题解web72知识点题解web73题解web74题解web75知识点题解web76题解web77知识点题解web78知识点题解web79题解web80知识点题解ctf-web入门web71知识点ob_get_contents():得到输出缓冲区的内容。ob_end_clean():清除缓冲区的内容,并将缓冲区关闭,但不会输出内......
  • C# 通过iTextSharp实现pdf文件盖章(通过在内容中插入盖章图片的形式)
    具体盖章方法实现///<summary>///第一页盖章///</summary>///<paramname="pdfPath">源pdf地址</param>///<paramname="outPdfPath">盖章后生成pdf地址</param>///<paramna......
  • C# 生成印章
    1、界面实现及按钮事件 ///点击按钮事件privatevoidbutton2_Click(objectsender,EventArgse){try{stringimageUrl="C:\\Users\\Administrator\\Desktop\\新建文件夹(2)";stringimageForm......
  • bootstrap-select组件
    bootstrap-select组件mmkkuoi于2021-10-1312:08:55发布10178收藏19分类专栏:js文章标签:bootstrapselect版权华为云开发者联盟该内容已被华为云开发者联盟社区收录加入社区js专栏收录该内容2篇文章0订阅订阅专栏阅读目录一、组件开源地址以及API说......
  • ArcGIS Pro创建、发布、调用GP服务全过程示例(等高线分析)
    在之前的文章介绍过使用ArcMap发布GP分析服务,由于ArcGIS后续不在更新ArcMap,改用ArcGISPro,本文对ArcGISPro发布GP分析服务进行说明。本文以等高线分析为例,使用ArcGISPro软件,从GP分析服务的创建、发布、调用全过程进行演示。使用ArcMap发布GP服务请跳转:本文示例使用(因为本人po......