C# 压缩和解压文件(SharpZipLib)_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C# 压缩和解压文件(SharpZipLib)

C# 压缩和解压文件(SharpZipLib)

 2014/9/17 14:38:31  小菜来报道  程序员俱乐部  我要评论(0)
  • 摘要:先从网上下载ICSharpCode.SharpZipLib.dll类库将文件或文件夹压缩为zip,函数如下1///<summary>2///压缩文件3///</summary>4///<paramname="fileName">压缩文件路径</param>5///<paramname="zipName">压缩的文件名称</param>6///<paramname="error">返回的错误信息<
  • 标签:C# 文件 压缩 ARP

先从网上下载ICSharpCode.SharpZipLib.dll类库

将文件或文件夹压缩为zip,函数如下

 1         /// <summary>
 2         /// 压缩文件
 3         /// </summary>
 4         /// <param name="fileName">压缩文件路径</param>
 5         /// <param name="zipName">压缩的文件名</param>
 6         /// <param name="error">返回的错误信息</param>
 7         /// <returns></returns>
 8         public bool FileToZip(string fileName, string zipName, out string error)
 9         {
10             error = string.Empty;
11             try
12             {
13                 ZipOutputStream s = new ZipOutputStream(File.Create(zipName));
14                 s.SetLevel(6); // 0 - store only to 9 - means best compression
15                 zip(fileName, s);
16                 s.Finish();
17                 s.Close();
18                 return true;
19             }
20             catch (Exception ex)
21             {
22                 error = ex.Message;
23                 return false;
24             }
25         }
26 
27 
28         private void zip(string fileName, ZipOutputStream s)
29         {
30             if (fileName[fileName.Length - 1] != Path.DirectorySeparatorChar)
31                 fileName += Path.DirectorySeparatorChar;
32             Crc32 crc = new Crc32();
33             string[] filenames = Directory.GetFileSystemEntries(fileName);
34             foreach (string file in filenames)
35             {
36                 if (Directory.Exists(file))
37                 {
38                     zip(file, s);
39                 }
40                 else // 否则直接压缩文件
41                 {
42                     //打开压缩文件
43                     FileStream fs = File.OpenRead(file);
44 
45                     byte[] buffer = new byte[fs.Length];
46                     fs.Read(buffer, 0, buffer.Length);
47                     string tempfile = Path.GetFileName(file);
48                     ZipEntry entry = new ZipEntry(tempfile);
49 
50                     entry.DateTime = DateTime.Now;
51                     entry.Size = fs.Length;
52                     fs.Close();
53                     crc.Reset();
54                     crc.Update(buffer);
55                     entry.Crc = crc.Value;
56                     s.PutNextEntry(entry);
57 
58                     s.Write(buffer, 0, buffer.Length);
59                 }
60             }
61         }

 

将zip解压为文件或文件夹,函数代码如下

 1         /// <summary>
 2         /// 解压文件
 3         /// </summary>
 4         /// <param name="zipName">解压文件路径</param>
 5         /// <param name="fileDirName">解压到文件夹的名称</param>
 6         /// <param name="error">返回的错误信息</param>
 7         /// <returns></returns>
 8         public bool ZipToFile(string zipName, string fileDirName, out string error)
 9         {
10             try
11             {
12                 error = string.Empty;
13                 //读取压缩文件(zip文件),准备解压缩
14                 ZipInputStream s = new ZipInputStream(File.Open(zipName.Trim(), FileMode.Open, FileAccess.Read));
15                 ZipEntry theEntry;
16 
17                 string rootDir = " ";
18                 while ((theEntry = s.GetNextEntry()) != null)
19                 {
20                     string path = fileDirName;
21                     //获取该文件在zip中目录
22                     rootDir = Path.GetDirectoryName(theEntry.Name);
23                     //获取文件名称
24                     string fileName = Path.GetFileName(theEntry.Name);
25                     if (string.IsNullOrEmpty(fileName))
26                         continue;
27                     //判断是否为顶层文件,是,将文件直接放在fileDirName下,否,创建目录
28                     if (string.IsNullOrEmpty(rootDir))
29                     {
30                         if (!Directory.Exists(path))
31                             Directory.CreateDirectory(path);
32                     }
33                     else
34                     {
35                         path += "\\" + rootDir;
36                         if (!Directory.Exists(path))
37                             Directory.CreateDirectory(path);
38                     }
39 
40                     //将文件流写入对应目录下的文件中
41                     if (fileName != String.Empty)
42                     {
43                         FileStream streamWriter = File.Create(path + "\\" + fileName);
44 
45                         int size = 2048;
46                         byte[] data = new byte[2048];
47                         while (true)
48                         {
49                             if (theEntry.Size == 0)
50                                 break;
51 
52                             size = s.Read(data, 0, data.Length);
53                             if (size > 0)
54                             {
55                                 streamWriter.Write(data, 0, size);
56                             }
57                             else
58                             {
59                                 break;
60                             }
61                         }
62                         streamWriter.Close();
63                     }
64                 }
65                 s.Close();
66                 return true;
67             }
68             catch (Exception ex)
69             {
70                 error = ex.Message;
71                 return false;
72             }
73         }

调用示例

class="code_img_closed" src="/Upload/Images/2014091714/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('1e33b592-87f4-4072-99d4-b64f71e0cd85',event)" src="/Upload/Images/2014091714/2B1B950FA3DF188F.gif" alt="" />
1 string error;
2             if (FileToZip(@"E:\文档", "文档.zip", out error))
3                 MessageBox.Show("Succee");
4             else
5                 MessageBox.Show(error);
压缩示例
1 string error;
2             if (ZipToFile(@"E:\文档.zip", "文档", out error))
3                 MessageBox.Show("Succee");
4             else
5                 MessageBox.Show(error);
解压示例

 

发表评论
用户名: 匿名