常用类-zip使用

1:ICSharpCode.SharpZipLib.Zip 插件

 1         /// <summary>
 2         /// 压缩文件夹/文件
 3         /// </summary>
 4         /// <param name="sourceFilePath"></param>
 5         /// <param name="destinationZipFilePath"></param>
 6         public static void CreateZip(string sourceFilePath, string destinationZipFilePath)
 7         {
 8             if (sourceFilePath[sourceFilePath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
 9                 sourceFilePath += System.IO.Path.DirectorySeparatorChar;
10 
11             ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));
12             zipStream.SetLevel(6);  // 压缩级别 0-9
13             CreateZipFiles(sourceFilePath, zipStream, sourceFilePath);
14 
15             zipStream.Finish();
16             zipStream.Close();
17         }

 

 

2:使用插件:System.IO.Compression.FileSystem 压缩文件

        public static void CreateZip(string sourceFilePath, string destinationZipFilePath)
        {

            if (File.Exists(destinationZipFilePath))
            {
                File.Delete(destinationZipFilePath);
            }
            System.IO.Compression.ZipFile.CreateFromDirectory(sourceFilePath, 
                destinationZipFilePath, System.IO.Compression.CompressionLevel.Optimal, true);

        }

  

 

posted @ 2019-03-19 22:09  毛毛球的书签  阅读(77)  评论(0编辑  收藏  举报