.net压缩文件(ICSharpCode.SharpZipLib)

NuGet添加引用:ICSharpCode.SharpZipLib.dll
  1 /// <summary>
  2         /// ZIP:压缩单个文件
  3         /// </summary>
  4         /// <param name="server_filename">需要压缩的文件(绝对路径)</param>
  5         /// <param name="server_save_path">压缩后的文件路径(绝对路径)</param>
  6         /// <param name="zipName">压缩后的文件名称(文件名,默认 同源文件同名)</param>
  7         /// <param name="CompressionLevel">压缩等级(0 无 - 9 最高,默认 5)</param>
  8         /// <param name="BlockSize">缓存大小(每次写入文件大小,默认 2048)</param>
  9         /// <param name="IsEncrypt">是否加密(默认 加密)</param>
 10         public static void ZipFile(string server_filename, string server_save_path, string zipName = "", int CompressionLevel = 5, int BlockSize = 2048, bool IsEncrypt = false)
 11         {
 12             //如果文件没有找到,则报错
 13             if (!File.Exists(server_filename))
 14             {
 15                 throw new FileNotFoundException("指定要压缩的文件: " + server_filename + " 不存在!");
 16             }
 17 
 18             //文件名称(默认同源文件名称相同)
 19             string ZipFileName = string.IsNullOrEmpty(zipName) ? server_save_path + "\\" + new FileInfo(server_filename).Name.Substring(0, new FileInfo(server_filename).Name.LastIndexOf('.')) + ".zip" : server_save_path + "\\" + zipName + ".zip";
 20 
 21             using (FileStream ZipFile = File.Create(ZipFileName))
 22             {
 23                 using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
 24                 {
 25                     using (FileStream StreamToZip = new FileStream(server_filename, FileMode.Open, FileAccess.Read))
 26                     {
 27                         string fileName = server_filename.Substring(server_filename.LastIndexOf("\\") + 1);
 28 
 29                         ZipEntry ZipEntry = new ZipEntry(fileName);
 30 
 31                         if (IsEncrypt)
 32                             //压缩文件加密
 33                             ZipStream.Password = "你的密码";
 34 
 35                         ZipStream.PutNextEntry(ZipEntry);
 36 
 37                         //设置压缩级别
 38                         ZipStream.SetLevel(CompressionLevel);
 39 
 40                         //缓存大小
 41                         byte[] buffer = new byte[BlockSize];
 42 
 43                         int sizeRead = 0;
 44 
 45                         try
 46                         {
 47                             do
 48                             {
 49                                 sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
 50                                 ZipStream.Write(buffer, 0, sizeRead);
 51                             }
 52                             while (sizeRead > 0);
 53                         }
 54                         catch (System.Exception ex)
 55                         {
 56                             throw ex;
 57                         }
 58 
 59                         StreamToZip.Close();
 60                     }
 61 
 62                     ZipStream.Finish();
 63                     ZipStream.Close();
 64                 }
 65 
 66                 ZipFile.Close();
 67             }
 68         }
 69 
 70         /// <summary>
 71         /// ZIP:压缩多个文件
 72         /// </summary>
 73         /// <param name="list_server_fileName">需要压缩的文件(绝对路径)</param>
 74         /// <param name="server_zipPath">压缩后的文件路径(绝对路径)</param>
 75         /// <param name="zipFileName">压缩后的文件名称(文件名,默认 同源文件同名)</param>
 76         /// <param name="CompressionLevel">压缩等级(0 无 - 9 最高,默认 5)</param>
 77         /// <param name="BlockSize">缓存大小(每次写入文件大小,默认 2048)</param>
 78         /// <param name="IsEncrypt">是否加密(默认 加密)</param>
 79         public static void ZipFiles(List<string> list_server_fileName, string server_zipPath, string zipFileName = "", int CompressionLevel = 5, int BlockSize = 2048, bool IsEncrypt = false)
 80         {
 81             if (list_server_fileName != null && list_server_fileName.Count > 0)
 82             {
 83                 //压缩后的文件路径及名称
 84                 string ZipFileName = server_zipPath + "\\" + zipFileName + ".zip";
 85 
 86                 using (FileStream ZipFile = File.Create(ZipFileName))
 87                 {
 88                     using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
 89                     {
 90                         if (IsEncrypt)
 91                         {
 92                             //压缩文件加密
 93                             ZipStream.Password = "你的密码";
 94                         }
 95 
 96                         //设置压缩级别
 97                         ZipStream.SetLevel(CompressionLevel);
 98 
 99                         foreach (var server_fileName in list_server_fileName)
100                         {
101                             //如果文件没有找到,则报错
102                             if (!File.Exists(server_fileName))
103                                 throw new FileNotFoundException("指定要压缩的文件: " + list_server_fileName + " 不存在!");
104 
105                             using (FileStream StreamToZip = new FileStream(server_fileName, FileMode.Open, FileAccess.Read))
106                             {
107                                 string fileName = server_fileName.Substring(server_fileName.LastIndexOf("\\") + 1);
108 
109                                 ZipEntry ZipEntry = new ZipEntry(fileName);                                
110 
111                                 ZipStream.PutNextEntry(ZipEntry);
112 
113                                 //缓存大小
114                                 byte[] buffer = new byte[BlockSize];
115 
116                                 int sizeRead = 0;
117                                 try
118                                 {
119                                     do
120                                     {
121                                         sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
122                                         ZipStream.Write(buffer, 0, sizeRead);
123                                     }
124                                     while (sizeRead > 0);
125                                 }
126                                 catch (System.Exception ex)
127                                 {
128                                     throw ex;
129                                 }
130 
131                                 StreamToZip.Close();
132                             }
133                         }
134 
135                         ZipStream.Finish();
136                         ZipStream.Close();
137                     }
138 
139                     ZipFile.Close();
140                 }
141             }
142         }

 

posted @ 2023-05-30 11:39  张伟大  阅读(70)  评论(0编辑  收藏  举报