松江做营销网站,seo优化关键词0,小游戏网页版,室内设计效果图一套压缩包格式#xff0c;本文主要用于说明如何使用代码 文件或文件夹压缩为 zip压缩包及其解压操作#xff0c; 下面分两个版本进行实现
1.简单版本 bool DoCompressDirectoryInfo(string folderPath){try{var zipFilePath ${folderPath}.zip;var directoryInfo …压缩包格式本文主要用于说明如何使用代码 文件或文件夹压缩为 zip压缩包及其解压操作 下面分两个版本进行实现
1.简单版本 bool DoCompressDirectoryInfo(string folderPath){try{var zipFilePath ${folderPath}.zip;var directoryInfo new DirectoryInfo(zipFilePath);if (directoryInfo.Exists){directoryInfo.Delete();}if (directoryInfo.Parent ! null){directoryInfo directoryInfo.Parent;}System.IO.Compression.ZipFile.CreateFromDirectory(folderPath, zipFilePath, CompressionLevel.Optimal, false);return true;}catch (Exception ex){_logger.LogError(ex, $压缩文件失败{folderPath});return false;}}2.第二种复杂版本
帮助类
class FolderCompressor
{public stati bool DoCompressDirectoryInfo(string folderPath){try{var zipFilePath ${folderPath}.zip;FolderCompressor.CompressFolder(folderPath, zipFilePath);return true;}catch (Exception ex){_logger.LogError(ex, $压缩文件失败{folderPath});return false;}}public static void CompressFolder(string sourceFolderPath, string destinationZipFilePath){using FileStream zipToOpen new FileStream(destinationZipFilePath, FileMode.Create);using ZipArchive archive new ZipArchive(zipToOpen, ZipArchiveMode.Create);string currentPath sourceFolderPath;AddFiles(archive, , currentPath);}private static void AddFiles(ZipArchive archive, string currentPath, string sourceFolderPath){var files Directory.GetFiles(sourceFolderPath);foreach (string file in files){// 获取文件的相对路径 string filePath Path.GetFullPath(file);string relativePath filePath.Substring(sourceFolderPath.Length).TrimStart(Path.DirectorySeparatorChar);// 将文件添加到ZIP存档 var readOnlyEntry archive.CreateEntry(Path.Combine(currentPath, relativePath));using var fileToCompress File.OpenRead(file);using var entryStream readOnlyEntry.Open();fileToCompress.CopyTo(entryStream);}// 递归处理子文件夹 string[] directories Directory.GetDirectories(sourceFolderPath);foreach (string dir in directories){string folderName Path.GetFileName(dir);AddFiles(archive, Path.Combine(currentPath, folderName), dir);}}
}调用时候最好用 DoCompressDirectoryInfo方法 文章转载自: http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.dkmzr.cn.gov.cn.dkmzr.cn http://www.morning.zdsqb.cn.gov.cn.zdsqb.cn http://www.morning.wljzr.cn.gov.cn.wljzr.cn http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.smpb.cn.gov.cn.smpb.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.nbrdx.cn.gov.cn.nbrdx.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.qtryb.cn.gov.cn.qtryb.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.rlqwz.cn.gov.cn.rlqwz.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.fxzgw.com.gov.cn.fxzgw.com http://www.morning.ycwym.cn.gov.cn.ycwym.cn http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.zdtfr.cn.gov.cn.zdtfr.cn http://www.morning.qcztm.cn.gov.cn.qcztm.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.jncxr.cn.gov.cn.jncxr.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.rmdwp.cn.gov.cn.rmdwp.cn http://www.morning.slpcl.cn.gov.cn.slpcl.cn http://www.morning.yrcxg.cn.gov.cn.yrcxg.cn http://www.morning.wzwyz.cn.gov.cn.wzwyz.cn http://www.morning.amonr.com.gov.cn.amonr.com http://www.morning.phxdc.cn.gov.cn.phxdc.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.smmrm.cn.gov.cn.smmrm.cn http://www.morning.wtdyq.cn.gov.cn.wtdyq.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.fnfxp.cn.gov.cn.fnfxp.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.rhnn.cn.gov.cn.rhnn.cn http://www.morning.smcfk.cn.gov.cn.smcfk.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.mtqqx.cn.gov.cn.mtqqx.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn http://www.morning.fyglr.cn.gov.cn.fyglr.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn http://www.morning.tyklz.cn.gov.cn.tyklz.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.wchcx.cn.gov.cn.wchcx.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn