/// <summary> /// 查询FapAttachment记录,将多个文件打包成ZIP文件 /// </summary> /// <param name="attachmentFids"></param> /// <param name="handler">委托事件</param> public void DownloadZip(List <string> attachmentFids, DownloadZipEventHandler handler) { if (attachmentFids == null || attachmentFids.Count == 0) { return; } string sql = "select * from FapAttachment where Fid in (" + string.Join(',', attachmentFids.Select(a => $"'{a}'")) + ")"; IEnumerable <FapAttachment> attachments = _dataAccessor.Query <FapAttachment>(sql); this.DownloadZip(attachments, handler); }
/// <summary> /// 将FapAttachment文件对象的文件,打包成ZIP文件 /// </summary> /// <param name="attachments"></param> /// <param name="handler">委托事件</param> public void DownloadZip(IEnumerable <FapAttachment> attachments, DownloadZipEventHandler handler) { if (attachments == null || attachments.Count() == 0) { return; } //先创建临时文件夹,以便存放要打包的文件 string temporaryFolderName = UUIDUtils.Fid; string temporaryFolderPath = FileUtility.CreateTemporaryFolder(temporaryFolderName); //压缩文件路径 string zipFilePath = Path.Combine(temporaryFolderPath, temporaryFolderName + ".zip"); //将文件存放到临时文件夹中,并记录这些文件的路径 IList <string> filePathListToZip = new List <string>(); //记录要打包的所有文件的路径 foreach (var attachment in attachments) { string fileName = attachment.FileName; if (string.IsNullOrWhiteSpace(fileName)) { fileName = UUIDUtils.Fid + attachment.FileExtension; } string filePath = Path.Combine(temporaryFolderPath, fileName); Stream stream = this.GetFileStream(attachment); if (stream != null) { FileUtility.CreateFileFromStream(filePath, stream); filePathListToZip.Add(filePath); } } //将这些文件打包成ZIP文件,返回zip流 ZipHelper zipHelper = new ZipHelper(); zipHelper.ZipMultiFiles(filePathListToZip, zipFilePath); Stream zipStream = new FileStream(zipFilePath, FileMode.Open); if (handler != null) { handler(zipStream); } //删除临时文件夹,不再删除,任务调度删除 //FileUtility.DeleteFolderFromTemporaryFolder(temporaryFolderName); }
/// <summary> /// 根据Bid,将多个文件打包成ZIP文件 /// </summary> /// <param name="bid"></param> /// <param name="handler">委托事件</param> public void DownloadZip(string bid, DownloadZipEventHandler handler) { if (string.IsNullOrWhiteSpace(bid)) { return; } string sql = "select * from FapAttachment where Bid=@Bid"; DynamicParameters parameters = new DynamicParameters(); parameters.Add("Bid", bid); var attachments = _dataAccessor.Query <FapAttachment>(sql, parameters); this.DownloadZip(attachments, handler); }