using Microsoft.AspNetCore.Hosting; using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; using System.Threading.Tasks; namespace New_College.Common.Helper { public static class FileZipHelper { /// /// 批量压缩上传 /// /// /// /// /// /// public static string FileMultipZipUpload(IWebHostEnvironment webHost, List files, string mangeName, bool isuploadOss = true) { string zipstr = string.Empty; try { var tmplist = new List(); string rootdir = webHost.WebRootPath + "/" + mangeName; string zipname = mangeName + ".zip"; string ossfileUrl = webHost.WebRootPath + "/" + zipname; if (!Directory.Exists(rootdir)) { Directory.CreateDirectory(rootdir); } int tmpid = 0; files.ForEach(a => { string userdir = rootdir + "/" + a.UserName; if (!Directory.Exists(userdir)) { Directory.CreateDirectory(userdir); } tmpid++; int k = 0; a.fileItems.ForEach(s => { if (s.ImgUrl.Contains("http")) { k++; string filename = string.Format("{0}/{1}.{2}-{3}-{4}{5}", userdir, tmpid, s.typeName, a.UserName, k, Path.GetExtension(s.ImgUrl)); // Wb_Client(s.ImgUrl, filename); tmplist.Add(filename); } }); }); if (File.Exists(ossfileUrl)) { File.Delete(ossfileUrl); } ZipFile.CreateFromDirectory(rootdir, ossfileUrl); if (isuploadOss) { zipstr = AliYunOssHelper.UploadFile(zipname, ossfileUrl); if (!string.IsNullOrEmpty(zipstr)) { tmplist.Add(ossfileUrl); tmplist.ForEach(d => { File.Delete(d); }); Directory.Delete(rootdir, true); } return zipstr; } else { tmplist.ForEach(d => { File.Delete(d); }); Directory.Delete(rootdir, true); return zipname; } } catch (Exception ex) { DingHookHelper.DingTalkHookMessage("zip压缩错误", ex.Message); return "zip压缩错误"; } } } public class FileModel { public string UserName { get; set; } public List fileItems { get; set; } } public class FileItemModel { public string typeName { get; set; } public string ImgUrl { get; set; } } }