/// <summary>快速压缩目录。</summary> /// <param name="dirName"></param> /// <param name="outputName"></param> public static void CompressDirectory(String dirName, String outputName = null) { if (String.IsNullOrEmpty(dirName)) { throw new ArgumentNullException(nameof(dirName)); } if (String.IsNullOrEmpty(outputName)) { outputName = Path.ChangeExtension(Path.GetFileName(dirName), ".zip"); } using var zf = new ZipArchive(); zf.AddDirectory(dirName); zf.Write(outputName); }
/// <summary>从目录创建压缩文档</summary> /// <param name="sourceDirectoryName"></param> /// <param name="destinationArchiveFileName"></param> /// <param name="compressionLevel"></param> /// <param name="includeBaseDirectory"></param> public static void CreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName, CompressionLevel compressionLevel, Boolean includeBaseDirectory) { if (String.IsNullOrEmpty(sourceDirectoryName)) { throw new ArgumentNullException(nameof(sourceDirectoryName)); } if (String.IsNullOrEmpty(destinationArchiveFileName)) { destinationArchiveFileName = Path.ChangeExtension(Path.GetFileName(destinationArchiveFileName), ".zip"); } using var zf = new ZipArchive(); zf.AddDirectory(sourceDirectoryName, null, compressionLevel == CompressionLevel.NoCompression); zf.Write(destinationArchiveFileName); }
/// <summary>快速压缩文件。</summary> /// <param name="fileName"></param> /// <param name="outputName"></param> public static void CompressFile(String fileName, String outputName = null) { if (String.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } if (String.IsNullOrEmpty(outputName)) { outputName = Path.ChangeExtension(fileName, ".zip"); } using var zf = new ZipArchive(); zf.CreateEntryFromFile(fileName); zf.Write(outputName); }