.net压缩文件夹
1,引用:using System.IO.Packaging;
2,壓縮文件的方法:
/// <summary>/// 壓縮文件夾到制定的路徑/// </summary>/// <param name="folderName">要壓縮的文件物理路徑</param>/// <param name="compressedFileName">目標文件物理路徑和文件名</param>/// <param name="overrideExisting">是否覆蓋存在的文件</param>/// <returns></returns>static bool PackageFolder(string folderName, string compressedFileName, bool overrideExisting){if (folderName.EndsWith(@"\"))folderName = folderName.Remove(folderName.Length - 1);bool result = false;if (!Directory.Exists(folderName)){return result;}if (!overrideExisting && File.Exists(compressedFileName)){return result;}try{using (Package package = Package.Open(compressedFileName, FileMode.Create)){var fileList = Directory.EnumerateFiles(folderName, "*", SearchOption.AllDirectories);foreach (string fileName in fileList){//The path in the package is all of the subfolders after folderNamestring pathInPackage;pathInPackage = Path.GetDirectoryName(fileName).Replace(folderName, string.Empty) + "/"+ Path.GetFileName(fileName);Uri partUriDocument = PackUriHelper.CreatePartUri(new Uri(pathInPackage, UriKind.Relative));PackagePart packagePartDocument = package.CreatePart(partUriDocument, "", CompressionOption.Maximum);using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)){fileStream.CopyTo(packagePartDocument.GetStream());}}}result = true;}catch (Exception e){throw new Exception("Error zipping folder " + folderName, e);}return result;}?
轉載于:https://www.cnblogs.com/Angel-szl/p/9986952.html
總結
- 上一篇: Android Service、Inte
- 下一篇: Jira filter subscr