最近项目需要实现多文件打包的功能。在尝试了一些方法后,终于发现使用ICSharpCode.SharpZipLib最符合项目的要求。具体实现如下:1.在Nuget中安装ICSharpCode.SharpZipLib2.将要打包的文件放到同一个文件夹下进行压缩:①压缩文件夹//////压缩文件//////压缩后得到的文件名publicstaticboolCompressFile(stringdir,outstringfileName){stringdest=System.Environment.GetFolderPath(System.Environment.SpecialFolder.桌面)+"\"+string.Format("{0:yyyyMMddHHmmss}",DateTime.Now)+".zip";//桌面默认压缩if(!Directory.Exists(Path.GetDirectoryName(dest)))//如果文件不存在则根据路径创建E:\testDirectory.CreateDirectory(Path.GetDirectoryName(dest));使用(ZipOutputStreamzipStream=newZipOutputStream(File.Create(dest))){zipStream.SetLevel(6);//压缩级别0-9CreateZip(dir,zipStream);文件名=目标;zipStream.Finish();zipStream.Close();}返回真;}//////将内容压缩到zipStream中//////sourcefile///目标文件流(完整路径+文件名+.zip)privatestaticvoidCreateZip(stringsource,ZipOutputStreamzipStream){Crc32crc=newCrc32();字符串[]文件=Directory.GetFileSystemEntries(源);//获取所有文件名和目录名foreach(varfileinfiles){if(Directory.Exists(file))//如果文件夹中有文件则递归{CreateZip(file,zipStream);}else//Compressifnot{using(FileStreamfs=File.OpenRead(file)){byte[]buffer=newbyte[fs.Length];fs.Read(buffer,0,buffer.Length);字符串临时文件名=file.Substring(file.LastIndexOf("\")+1);//获取当前文件路径的文件名ZipEntryentry=newZipEntry(tempFileName);entry.DateTime=DateTime.Now;entry.Size=fs.Length;fs.Close();crc.重置();crc.更新(缓冲区);entry.Crc=crc.Value;zipStream.PutNextEntry(条目);zipStream.Write(buffer,0,buffer.Length);}}}}②打包压缩指定文件(可以打包在线文件)//////打包在线和离线文件//////FileList///保存路径publicstaticvoidZipOnlineFile3(ListfileList,stringsavepath){//判断保存的文件目录是否存在if(!File.Exists(保存路径)){varfile=newFileInfo(保存路径);如果(!file.Directory.Exists){file.Directory.Create();}}Crc32crc=newCrc32();使用(ZipOutputStreamzipStream=newZipOutputStream(File.Create(savepath))){zipStream.SetLevel(9);//压缩等级0-9foreach(varurlinfileList){byte[]buffer=newWebClient().DownloadData(url);字符串tempFileName=GetFileNameByUrl(url);//获得当前文件路径的文件名ZipEntryentry=newZipEntry(tempFileName);entry.DateTime=DateTime.Now;entry.Size=buffer.Length;crc.重置();crc.更新(缓冲区);zipStream.PutNextEntry(条目);zipStream.Write(缓冲区,0,buffer.Length);}}}从文件路径中读取文件名的方法:publicstaticstringGetFileNameByUrl(stringurl){//判断路径是否为空if(string.IsNullOrWhiteSpace(url))returnnull;//判断是否是一行If(url.ToLower().StartsWith("http")){returnurl.Substring(url.LastIndexOf("/")+1);}else{returnurl.Substring(url.LastIndexOf("\")+1);}}该方法生成的压缩包中,所有文件会在同一层显示③如果需要在文件中创建目录,需要在文件名上指定文件路径来添加工具类://////fileobject///publicclassFileItem{/////文件名///publicstringFileName{get;放;}//////文件路径///publicstringFileUrl{get;放;}}压缩文件方法://////打包在线和离线文件//////压缩文件名///文件列表///保存路径publicstaticstringZipFiles(stringzipName,ListfileList,outstringerror){error=字符串.空;stringpath=string.Format("/files/zipFiles/{0}/{1}/{2}/",DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day);//文件保存目录stringdirectory=文件保存路径+路径;字符串url=FileHostUrl.TrimEnd('/')+路径+zipName;字符串savePath=目录+zipName;尝试{如果(!Directory.Exists(目录)){Directory.CreateDirectory(目录);}使用(ZipOutputStreamzipStream=newZipOutputStream(File.Create(savePath))){zipStream.SetLevel(9);//压缩等级0-9foreach(variteminfileList){byte[]buffer=newWebClient().DownloadData(item.FileUrl);ZipEntryentry=newZipEntry(item.FileName);entry.DateTime=DateTime.Now;entry.Size=buffer.Length;zipStream.PutNextEntry(条目);zipStream.Write(buffer,0,buffer.Length);}}}catch(Exceptionex){error="打包文件失败:"+ex.Message;}返回网址;}调用参数示例:{"zipName":"test.zip","fileList":[{"fileName":"123.png","fileUrl":"https://file.yidongcha.cn/files/uploadfiles/image/2021/11/15/11c6de395fcc484faf4745ade62cf6e6.png"},{"fileName":"123/456/789.jpg","fileUrl":"https://file.yidongcha.cn/files/uploadfiles/image/2021/11/15/fe922b250acf4344b8ca4d2aad6e0355.jpg"}]}生成结果:本文收集自网络,不代表立场,如涉及侵权,请点击右侧联系管理员删除,如需转载,请注明出处: