RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2 新增解压缩工具类ZipHelper

简介:

在项目对文件进行解压缩是非常常用的功能,对文件进行压缩存储或传输可以节省流量与空间。压缩文件的格式与方法都比较多,比较常用的国际标准是zip格式。压缩与解压缩的方法也很多,在.NET 2.0开始,在System.IO.Compression中微软已经给我们提供了解压缩的方法GZipStream。对于GZipStream的使用以及优缺点网上已经有非常多的文章,本文主要讲的是利用三方开源组件ICSharpCode.SharpZipLib进行文件的解压缩。

  SharpZipLib地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

  SharpZipLib是一个使用C#编写的Zip操作类库,是一个开源的C#压缩解压库,应用非常广泛。在VB.NET、C#或其他的.NET语言中都可以使用它创建Zip文件、并进行读取和更新等操作。SharpZipLib是一个完全由c#编写的Zip, GZip, Tar and BZip2 library,可以方便地支持这几种格式的压缩解压缩。SharpZipLib目前的版本为0.86,我们可以直接从上面提供的网站下载dll文件再添加到项目引用中,也可以通过VS提供的包管理工具NuGet把SharpZipLib添加到项目中。NuGet能更方便地把一些dll和文件添加到项目中,而不需要从文件中复制拷贝,推荐使用。使用NuGet添加SharpZipLib到项目中的方法如下图所示,在我们需要SharpZipLib的项目中右键单击“引用”,在弹出的快捷菜单中选择“管理NuGet程序包(N)…”。

 

  在打开的“管理NuGet程序包”对话框,搜索SharpZipLib找到后单击安装即可。

  引用SharpZipLib到项目中后,我们就可以编写相应的加压缩方法,下面将对常用的方法一一分享。

  在使用前必须先添加引用如下:

1
2
using  ICSharpCode.SharpZipLib.Checksums;
using  ICSharpCode.SharpZipLib.Zip;  

  一、压缩文件夹 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/// <summary>
    /// 压缩文件夹
    /// </summary>
    /// <param name="dirToZip"></param>
    /// <param name="zipedFileName"></param>
    /// <param name="compressionLevel">压缩率0(无压缩)9(压缩率最高)</param>
    public  static  void  ZipDir( string  dirToZip,  string  zipedFileName,  int  compressionLevel = 9)
    {
        if  (Path.GetExtension(zipedFileName) !=  ".zip" )
        {
            zipedFileName = zipedFileName +  ".zip" ;
        }
        using  ( var  zipoutputstream =  new  ZipOutputStream(File.Create(zipedFileName)))
        {
            zipoutputstream.SetLevel(compressionLevel);
            Crc32 crc =  new  Crc32();
            Hashtable fileList = GetAllFies(dirToZip);
            foreach  (DictionaryEntry item  in  fileList)
            {
                FileStream fs =  new  FileStream(item.Key.ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                byte [] buffer =  new  byte [fs.Length];
                fs.Read(buffer, 0, buffer.Length);;
                ZipEntry entry =  new  ZipEntry(Path.GetFileName(item.Key.ToString()))
                {
                    DateTime = (DateTime)item.Value,
                    Size = fs.Length
                };
                fs.Close();
                crc.Reset();
                crc.Update(buffer);
                entry.Crc = crc.Value;
                zipoutputstream.PutNextEntry(entry);
                zipoutputstream.Write(buffer, 0, buffer.Length);
            }
        }
    }

  二、解压文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/// <summary> 
    /// 功能:解压zip格式的文件。 
    /// </summary> 
    /// <param name="zipFilePath">压缩文件路径</param> 
    /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param> 
    /// <returns>解压是否成功</returns> 
    public  static  void  UnZip( string  zipFilePath,  string  unZipDir)
    {
        if  (zipFilePath ==  string .Empty)
        {
            throw  new  Exception( "压缩文件不能为空!" );
        }
        if  (!File.Exists(zipFilePath))
        {
            throw  new  FileNotFoundException( "压缩文件不存在!" );
        }
        //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 
        if  (unZipDir ==  string .Empty)
            unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
        if  (!unZipDir.EndsWith( "/" ))
            unZipDir +=  "/" ;
        if  (!Directory.Exists(unZipDir))
            Directory.CreateDirectory(unZipDir);
 
        using  ( var  s =  new  ZipInputStream(File.OpenRead(zipFilePath)))
        {
 
            ZipEntry theEntry;
            while  ((theEntry = s.GetNextEntry()) !=  null )
            {
                string  directoryName = Path.GetDirectoryName(theEntry.Name);
                string  fileName = Path.GetFileName(theEntry.Name);
                if  (! string .IsNullOrEmpty(directoryName))
                {
                    Directory.CreateDirectory(unZipDir + directoryName);
                }
                if  (directoryName !=  null  && !directoryName.EndsWith( "/" ))
                {
                }
                if  (fileName != String.Empty)
                {
                    using  (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
                    {
 
                        int  size;
                        byte [] data =  new  byte [2048];
                        while  ( true )
                        {
                            size = s.Read(data, 0, data.Length);
                            if  (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break ;
                            }
                        }
                    }
                }
            }
        }
    }

  三、压缩单个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/// <summary>
   /// 压缩单个文件
   /// </summary>
   /// <param name="fileToZip">要进行压缩的文件名,全路径</param>
   /// <param name="zipedFile">压缩后生成的压缩文件名,全路径</param>
   public  static  void  ZipFile( string  fileToZip,  string  zipedFile)
   {
       // 如果文件没有找到,则报错
       if  (!File.Exists(fileToZip))
       {
           throw  new  FileNotFoundException( "指定要压缩的文件: "  + fileToZip +  " 不存在!" );
       }
       using  (FileStream fileStream = File.OpenRead(fileToZip))
       {
           byte [] buffer =  new  byte [fileStream.Length];
           fileStream.Read(buffer, 0, buffer.Length);
           fileStream.Close();
           using  (FileStream zipFile = File.Create(zipedFile))
           {
               using  (ZipOutputStream zipOutputStream =  new  ZipOutputStream(zipFile))
               {
                   // string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
                   string  fileName = Path.GetFileName(fileToZip);
                   var  zipEntry =  new  ZipEntry(fileName)
                   {
                       DateTime = DateTime.Now,
                       IsUnicodeText =  true
                   };
                   zipOutputStream.PutNextEntry(zipEntry);
                   zipOutputStream.SetLevel(5);
                   zipOutputStream.Write(buffer, 0, buffer.Length);
                   zipOutputStream.Finish();
                   zipOutputStream.Close();
               }
           }
       }
   }

  三、压缩单个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/// <summary>
    /// 压缩多个目录或文件
    /// </summary>
    /// <param name="folderOrFileList">待压缩的文件夹或者文件,全路径格式,是一个集合</param>
    /// <param name="zipedFile">压缩后的文件名,全路径格式</param>
    /// <param name="password">压宿密码</param>
    /// <returns></returns>
    public  static  bool  ZipManyFilesOrDictorys(IEnumerable< string > folderOrFileList,  string  zipedFile,  string  password)
    {
        bool  res =  true ;
        using  ( var  s =  new  ZipOutputStream(File.Create(zipedFile)))
        {
            s.SetLevel(6);
            if  (! string .IsNullOrEmpty(password))
            {
                s.Password = password;
            }
            foreach  ( string  fileOrDir  in  folderOrFileList)
            {
                //是文件夹
                if  (Directory.Exists(fileOrDir))
                {
                    res = ZipFileDictory(fileOrDir, s,  "" );
                }
                else
                {
                    //文件
                    res = ZipFileWithStream(fileOrDir, s);
                }
            }
            s.Finish();
            s.Close();
            return  res;
        }
    }

  五、递归压缩文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/// <summary>
    /// 递归压缩文件夹方法
    /// </summary>
    /// <param name="folderToZip"></param>
    /// <param name="s"></param>
    /// <param name="parentFolderName"></param>
    private  static  bool  ZipFileDictory( string  folderToZip, ZipOutputStream s,  string  parentFolderName)
    {
        bool  res =  true ;
        ZipEntry entry =  null ;
        FileStream fs =  null ;
        Crc32 crc =  new  Crc32();
        try
        {
            //创建当前文件夹
            entry =  new  ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) +  "/" ));  //加上 “/” 才会当成是文件夹创建
            s.PutNextEntry(entry);
            s.Flush();
            //先压缩文件,再递归压缩文件夹
            var  filenames = Directory.GetFiles(folderToZip);
            foreach  ( string  file  in  filenames)
            {
                //打开压缩文件
                fs = File.OpenRead(file);
                byte [] buffer =  new  byte [fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                entry =  new  ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) +  "/"  + Path.GetFileName(file)));
                entry.DateTime = DateTime.Now;
                entry.Size = fs.Length;
                fs.Close();
                crc.Reset();
                crc.Update(buffer);
                entry.Crc = crc.Value;
                s.PutNextEntry(entry);
                s.Write(buffer, 0, buffer.Length);
            }
        }
        catch
        {
            res =  false ;
        }
        finally
        {
            if  (fs !=  null )
            {
                fs.Close();
            }
            if  (entry !=  null )
            {
            }
            GC.Collect();
            GC.Collect(1);
        }
        var  folders = Directory.GetDirectories(folderToZip);
        foreach  ( string  folder  in  folders)
        {
            if  (!ZipFileDictory(folder, s, Path.Combine(parentFolderName, Path.GetFileName(folderToZip))))
            {
                return  false ;
            }
        }
        return  res;
    }

  利用ICSharpCode.SharpZipLib解压缩辅助类全部代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
using  System;
using  System.Collections;
using  System.Collections.Generic;
using  System.IO;
 
namespace  RDIFramework.Utilities
{
     using  ICSharpCode.SharpZipLib.Checksums;
     using  ICSharpCode.SharpZipLib.Zip;
 
     /// <summary>
     /// ZipHelper.cs
     /// Zip解压缩帮助类
     ///
     /// 修改纪录
     ///    
     ///     2017-03-05 EricHu   创建。
     ///
     /// 版本:1.0
     ///
     /// <author>
     ///        <name>EricHu</name>
     ///        <date>2017-03-05</date>
     /// </author>
     /// </summary>
     public  class  ZipHelper
     {
         /// <summary>
         /// 压缩文件夹
         /// </summary>
         /// <param name="dirToZip"></param>
         /// <param name="zipedFileName"></param>
         /// <param name="compressionLevel">压缩率0(无压缩)9(压缩率最高)</param>
         public  static  void  ZipDir( string  dirToZip,  string  zipedFileName,  int  compressionLevel = 9)
         {
             if  (Path.GetExtension(zipedFileName) !=  ".zip" )
             {
                 zipedFileName = zipedFileName +  ".zip" ;
             }
             using  ( var  zipoutputstream =  new  ZipOutputStream(File.Create(zipedFileName)))
             {
                 zipoutputstream.SetLevel(compressionLevel);
                 Crc32 crc =  new  Crc32();
                 Hashtable fileList = GetAllFies(dirToZip);
                 foreach  (DictionaryEntry item  in  fileList)
                 {
                     FileStream fs =  new  FileStream(item.Key.ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                     byte [] buffer =  new  byte [fs.Length];
                     fs.Read(buffer, 0, buffer.Length);;
                     ZipEntry entry =  new  ZipEntry(Path.GetFileName(item.Key.ToString()))
                     {
                         DateTime = (DateTime)item.Value,
                         Size = fs.Length
                     };
                     fs.Close();
                     crc.Reset();
                     crc.Update(buffer);
                     entry.Crc = crc.Value;
                     zipoutputstream.PutNextEntry(entry);
                     zipoutputstream.Write(buffer, 0, buffer.Length);
                 }
             }
         }
 
         /// <summary> 
         /// 获取所有文件 
         /// </summary> 
         /// <returns></returns> 
         public  static  Hashtable GetAllFies( string  dir)
         {
             Hashtable filesList =  new  Hashtable();
             DirectoryInfo fileDire =  new  DirectoryInfo(dir);
             if  (!fileDire.Exists)
             {
                 throw  new  FileNotFoundException( "目录:"  + fileDire.FullName +  "没有找到!" );
             }
 
             GetAllDirFiles(fileDire, filesList);
             GetAllDirsFiles(fileDire.GetDirectories(), filesList);
             return  filesList;
         }
 
         /// <summary> 
         /// 获取一个文件夹下的所有文件夹里的文件 
         /// </summary> 
         /// <param name="dirs"></param> 
         /// <param name="filesList"></param> 
         public  static  void  GetAllDirsFiles(IEnumerable<DirectoryInfo> dirs, Hashtable filesList)
         {
             foreach  (DirectoryInfo dir  in  dirs)
             {
                 foreach  (FileInfo file  in  dir.GetFiles( "*.*" ))
                 {
                     filesList.Add(file.FullName, file.LastWriteTime);
                 }
                 GetAllDirsFiles(dir.GetDirectories(), filesList);
             }
         }
 
         /// <summary> 
         /// 获取一个文件夹下的文件 
         /// </summary> 
         /// <param name="dir">目录名称</param>
         /// <param name="filesList">文件列表HastTable</param> 
         public  static  void  GetAllDirFiles(DirectoryInfo dir, Hashtable filesList)
         {
             foreach  (FileInfo file  in  dir.GetFiles( "*.*" ))
             {
                 filesList.Add(file.FullName, file.LastWriteTime);
             }
         }
 
         /// <summary> 
         /// 功能:解压zip格式的文件。 
         /// </summary> 
         /// <param name="zipFilePath">压缩文件路径</param> 
         /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param> 
         /// <returns>解压是否成功</returns> 
         public  static  void  UnZip( string  zipFilePath,  string  unZipDir)
         {
             if  (zipFilePath ==  string .Empty)
             {
                 throw  new  Exception( "压缩文件不能为空!" );
             }
             if  (!File.Exists(zipFilePath))
             {
                 throw  new  FileNotFoundException( "压缩文件不存在!" );
             }
             //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 
             if  (unZipDir ==  string .Empty)
                 unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
             if  (!unZipDir.EndsWith( "/" ))
                 unZipDir +=  "/" ;
             if  (!Directory.Exists(unZipDir))
                 Directory.CreateDirectory(unZipDir);
 
             using  ( var  s =  new  ZipInputStream(File.OpenRead(zipFilePath)))
             {
 
                 ZipEntry theEntry;
                 while  ((theEntry = s.GetNextEntry()) !=  null )
                 {
                     string  directoryName = Path.GetDirectoryName(theEntry.Name);
                     string  fileName = Path.GetFileName(theEntry.Name);
                     if  (! string .IsNullOrEmpty(directoryName))
                     {
                         Directory.CreateDirectory(unZipDir + directoryName);
                     }
                     if  (directoryName !=  null  && !directoryName.EndsWith( "/" ))
                     {
                     }
                     if  (fileName != String.Empty)
                     {
                         using  (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
                         {
 
                             int  size;
                             byte [] data =  new  byte [2048];
                             while  ( true )
                             {
                                 size = s.Read(data, 0, data.Length);
                                 if  (size > 0)
                                 {
                                     streamWriter.Write(data, 0, size);
                                 }
                                 else
                                 {
                                     break ;
                                 }
                             }
                         }
                     }
                 }
             }
         }
 
         /// <summary>
         /// 压缩单个文件
         /// </summary>
         /// <param name="filePath">被压缩的文件名称(包含文件路径),文件的全路径</param>
         /// <param name="zipedFileName">压缩后的文件名称(包含文件路径),保存的文件名称</param>
         /// <param name="compressionLevel">压缩率0(无压缩)到 9(压缩率最高)</param>
         public  static  void  ZipFile( string  filePath,  string  zipedFileName,  int  compressionLevel = 9)
         {
             // 如果文件没有找到,则报错
             if  (!File.Exists(filePath))
             {
                 throw  new  FileNotFoundException( "文件:"  + filePath +  "没有找到!" );
             }
             // 如果压缩后名字为空就默认使用源文件名称作为压缩文件名称
             if  ( string .IsNullOrEmpty(zipedFileName))
             {
                 string  oldValue = Path.GetFileName(filePath);
                 if  (oldValue !=  null )
                 {
                     zipedFileName = filePath.Replace(oldValue,  "" ) + Path.GetFileNameWithoutExtension(filePath) +  ".zip" ;
                 }
             }
             // 如果压缩后的文件名称后缀名不是zip,就是加上zip,防止是一个乱码文件
             if  (Path.GetExtension(zipedFileName) !=  ".zip" )
             {
                 zipedFileName = zipedFileName +  ".zip" ;
             }
             // 如果指定位置目录不存在,创建该目录  C:\Users\yhl\Desktop\大汉三通
             string  zipedDir = zipedFileName.Substring(0, zipedFileName.LastIndexOf( "\\" , StringComparison.Ordinal));
             if  (!Directory.Exists(zipedDir))
             {
                 Directory.CreateDirectory(zipedDir);
             }
             // 被压缩文件名称
             string  filename = filePath.Substring(filePath.LastIndexOf( "\\" , StringComparison.Ordinal) + 1);
             var  streamToZip =  new  FileStream(filePath, FileMode.Open, FileAccess.Read);
             var  zipFile = File.Create(zipedFileName);
             var  zipStream =  new  ZipOutputStream(zipFile);
             var  zipEntry =  new  ZipEntry(filename);
             zipStream.PutNextEntry(zipEntry);
             zipStream.SetLevel(compressionLevel);
             var  buffer =  new  byte [2048];
             Int32 size = streamToZip.Read(buffer, 0, buffer.Length);
             zipStream.Write(buffer, 0, size);
             try
             {
                 while  (size < streamToZip.Length)
                 {
                     int  sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                     zipStream.Write(buffer, 0, sizeRead);
                     size += sizeRead;
                 }
             }
             finally
             {
                 zipStream.Finish();
                 zipStream.Close();
                 streamToZip.Close();
             }
         }
 
         /// <summary>
         /// 压缩单个文件
         /// </summary>
         /// <param name="fileToZip">要进行压缩的文件名,全路径</param>
         /// <param name="zipedFile">压缩后生成的压缩文件名,全路径</param>
         public  static  void  ZipFile( string  fileToZip,  string  zipedFile)
         {
             // 如果文件没有找到,则报错
             if  (!File.Exists(fileToZip))
             {
                 throw  new  FileNotFoundException( "指定要压缩的文件: "  + fileToZip +  " 不存在!" );
             }
             using  (FileStream fileStream = File.OpenRead(fileToZip))
             {
                 byte [] buffer =  new  byte [fileStream.Length];
                 fileStream.Read(buffer, 0, buffer.Length);
                 fileStream.Close();
                 using  (FileStream zipFile = File.Create(zipedFile))
                 {
                     using  (ZipOutputStream zipOutputStream =  new  ZipOutputStream(zipFile))
                     {
                         // string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
                         string  fileName = Path.GetFileName(fileToZip);
                         var  zipEntry =  new  ZipEntry(fileName)
                         {
                             DateTime = DateTime.Now,
                             IsUnicodeText =  true
                         };
                         zipOutputStream.PutNextEntry(zipEntry);
                         zipOutputStream.SetLevel(5);
                         zipOutputStream.Write(buffer, 0, buffer.Length);
                         zipOutputStream.Finish();
                         zipOutputStream.Close();
                     }
                 }
             }
         }
 
         /// <summary>
         /// 压缩多个目录或文件
         /// </summary>
         /// <param name="folderOrFileList">待压缩的文件夹或者文件,全路径格式,是一个集合</param>
         /// <param name="zipedFile">压缩后的文件名,全路径格式</param>
         /// <param name="password">压宿密码</param>
         /// <returns></returns>
         public  static  bool  ZipManyFilesOrDictorys(IEnumerable< string > folderOrFileList,  string  zipedFile,  string  password)
         {
             bool  res =  true ;
             using  ( var  s =  new  ZipOutputStream(File.Create(zipedFile)))
             {
                 s.SetLevel(6);
                 if  (! string .IsNullOrEmpty(password))
                 {
                     s.Password = password;
                 }
                 foreach  ( string  fileOrDir  in  folderOrFileList)
                 {
                     //是文件夹
                     if  (Directory.Exists(fileOrDir))
                     {
                         res = ZipFileDictory(fileOrDir, s,  "" );
                     }
                     else
                     {
                         //文件
                         res = ZipFileWithStream(fileOrDir, s);
                     }
                 }
                 s.Finish();
                 s.Close();
                 return  res;
             }
         }
 
         /// <summary>
         /// 带压缩流压缩单个文件
         /// </summary>
         /// <param name="fileToZip">要进行压缩的文件名</param>
         /// <param name="zipStream"></param>
         /// <returns></returns>
         private  static  bool  ZipFileWithStream( string  fileToZip, ZipOutputStream zipStream)
         {
             //如果文件没有找到,则报错
             if  (!File.Exists(fileToZip))
             {
                 throw  new  FileNotFoundException( "指定要压缩的文件: "  + fileToZip +  " 不存在!" );
             }
             //FileStream fs = null;
             FileStream zipFile =  null ;
             ZipEntry zipEntry =  null ;
             bool  res =  true ;
             try
             {
                 zipFile = File.OpenRead(fileToZip);
                 byte [] buffer =  new  byte [zipFile.Length];
                 zipFile.Read(buffer, 0, buffer.Length);
                 zipFile.Close();
                 zipEntry =  new  ZipEntry(Path.GetFileName(fileToZip));
                 zipStream.PutNextEntry(zipEntry);
                 zipStream.Write(buffer, 0, buffer.Length);
             }
             catch
             {
                 res =  false ;
             }
             finally
             {
                 if  (zipEntry !=  null )
                 {
                 }
 
                 if  (zipFile !=  null )
                 {
                     zipFile.Close();
                 }
                 GC.Collect();
                 GC.Collect(1);
             }
             return  res;
 
         }
 
         /// <summary>
         /// 递归压缩文件夹方法
         /// </summary>
         /// <param name="folderToZip"></param>
         /// <param name="s"></param>
         /// <param name="parentFolderName"></param>
         private  static  bool  ZipFileDictory( string  folderToZip, ZipOutputStream s,  string  parentFolderName)
         {
             bool  res =  true ;
             ZipEntry entry =  null ;
             FileStream fs =  null ;
             Crc32 crc =  new  Crc32();
             try
             {
                 //创建当前文件夹
                 entry =  new  ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) +  "/" ));  //加上 “/” 才会当成是文件夹创建
                 s.PutNextEntry(entry);
                 s.Flush();
                 //先压缩文件,再递归压缩文件夹
                 var  filenames = Directory.GetFiles(folderToZip);
                 foreach  ( string  file  in  filenames)
                 {
                     //打开压缩文件
                     fs = File.OpenRead(file);
                     byte [] buffer =  new  byte [fs.Length];
                     fs.Read(buffer, 0, buffer.Length);
                     entry =  new  ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) +  "/"  + Path.GetFileName(file)));
                     entry.DateTime = DateTime.Now;
                     entry.Size = fs.Length;
                     fs.Close();
                     crc.Reset();
                     crc.Update(buffer);
                     entry.Crc = crc.Value;
                     s.PutNextEntry(entry);
                     s.Write(buffer, 0, buffer.Length);
                 }
             }
             catch
             {
                 res =  false ;
             }
             finally
             {
                 if  (fs !=  null )
                 {
                     fs.Close();
                 }
                 if  (entry !=  null )
                 {
                 }
                 GC.Collect();
                 GC.Collect(1);
             }
             var  folders = Directory.GetDirectories(folderToZip);
             foreach  ( string  folder  in  folders)
             {
                 if  (!ZipFileDictory(folder, s, Path.Combine(parentFolderName, Path.GetFileName(folderToZip))))
                 {
                     return  false ;
                 }
             }
             return  res;
         }
     }
}

  


本文转自yonghu86博客园博客,原文链接:http://www.cnblogs.com/huyong/p/6504561.html,如需转载请自行联系原作者


相关文章
|
3月前
|
前端开发 JavaScript 关系型数据库
使用 OpenAuth.Net 快速搭建 .NET 企业级权限工作流系统
使用 OpenAuth.Net 快速搭建 .NET 企业级权限工作流系统
126 0
|
6月前
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
229 0
|
9月前
|
C# Android开发 iOS开发
2025年全面的.NET跨平台应用框架推荐
2025年全面的.NET跨平台应用框架推荐
378 23
|
10月前
|
JSON 安全 API
.net 自定义日志类
在.NET中,创建自定义日志类有助于更好地管理日志信息。示例展示了如何创建、配置和使用日志记录功能,包括写入日志文件、设置日志级别、格式化消息等。注意事项涵盖时间戳、日志级别、JSON序列化、线程安全、日志格式、文件处理及示例使用。请根据需求调整代码。
160 14
|
10月前
|
前端开发 C# 开发者
.NET使用Umbraco CMS快速构建一个属于自己的内容管理系统
.NET使用Umbraco CMS快速构建一个属于自己的内容管理系统
139 12
|
10月前
|
Web App开发 前端开发 调度
一款基于 .NET + Blazor 开发的智能访客管理系统
一款基于 .NET + Blazor 开发的智能访客管理系统
156 8
|
10月前
|
前端开发 JavaScript C#
基于.NET8+Vue3开发的权限管理&个人博客系统
基于.NET8+Vue3开发的权限管理&个人博客系统
139 7
|
10月前
|
监控 前端开发 API
一款基于 .NET MVC 框架开发、功能全面的MES系统
一款基于 .NET MVC 框架开发、功能全面的MES系统
268 5
|
Java C# 开发工具
.Net码农学Android---系统架构和基本概念
原文:.Net码农学Android---系统架构和基本概念 至此,你应该已经完成以下前期准备事情: 1.安装完JDK 2.安装完SDK(并在Manager中进行相关版本的更新) 3.相关IDE(如eclipse) 4.安装完ADT 5.安装完AVD(如果你是真机模拟的话也可以不安装) 前期环境搭建基本完成,并按照网上的教程可以运行出HelloWorld,确保可以流程走的通。
1054 0
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
218 7

热门文章

最新文章