马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#实现解压功能

  [复制链接]
查看11948 | 回复10 | 2012-12-1 22:26:02 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ICSharpCode.SharpZipLib.Zip;  //开源工具,可免费下载:下载地址:
  6. //http://files.cnblogs.com/xiaowei0705/SharpZipLib_0860_Bin.zip
  7. using System.IO;
  8. namespace Package
  9. {
  10.     class Class1
  11.     {
  12.         #region 加压解压方法
  13.         /// <summary>   
  14.         /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略)   
  15.         /// </summary>   
  16.         /// <param name="dirPath">被压缩的文件夹夹路径</param>   
  17.         /// <param name="zipFilePath">生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip</param>   
  18.         /// <param name="err">出错信息</param>   
  19.         /// <returns>是否压缩成功</returns>   
  20.         public bool ZipFile(string dirPath, string zipFilePath, out string err)
  21.         {
  22.             err = "";
  23.             if (dirPath == string.Empty)
  24.             {
  25.                 err = "要压缩的文件夹不能为空!";
  26.                 return false;
  27.             }
  28.             if (!Directory.Exists(dirPath))
  29.             {
  30.                 err = "要压缩的文件夹不存在!";
  31.                 return false;
  32.             }
  33.             //压缩文件名为空时使用文件夹名+.zip   
  34.             if (zipFilePath == string.Empty)
  35.             {
  36.                 if (dirPath.EndsWith("\"))
  37.                 {
  38.                     dirPath = dirPath.Substring(0, dirPath.Length - 1);
  39.                 }
  40.                 zipFilePath = dirPath + ".zip";
  41.             }
  42.             try
  43.             {
  44.                 string[] filenames = Directory.GetFiles(dirPath);
  45.                 using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
  46.                 {
  47.                     s.SetLevel(9);
  48.                     byte[] buffer = new byte[4096];
  49.                     foreach (string file in filenames)
  50.                     {
  51.                         ZipEntry entry = new ZipEntry(Path.GetFileName(file));
  52.                         entry.DateTime = DateTime.Now;
  53.                         s.PutNextEntry(entry);
  54.                         using (FileStream fs = File.OpenRead(file))
  55.                         {
  56.                             int sourceBytes;
  57.                             do
  58.                             {
  59.                                 sourceBytes = fs.Read(buffer, 0, buffer.Length);
  60.                                 s.Write(buffer, 0, sourceBytes);
  61.                             } while (sourceBytes > 0);
  62.                         }
  63.                     }
  64.                     s.Finish();
  65.                     s.Close();
  66.                 }
  67.             }
  68.             catch (Exception ex)
  69.             {
  70.                 err = ex.Message;
  71.                 return false;
  72.             }
  73.             return true;
  74.         }
  75.         /// <summary>   
  76.         /// 功能:解压zip格式的文件。   
  77.         /// </summary>   
  78.         /// <param name="zipFilePath">压缩文件路径</param>   
  79.         /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>   
  80.         /// <param name="err">出错信息</param>   
  81.         /// <returns>解压是否成功</returns>   
  82.         public bool UnZipFile(string zipFilePath, string unZipDir, out string err)
  83.         {
  84.             err = "";
  85.             if (zipFilePath == string.Empty)
  86.             {
  87.                 err = "压缩文件不能为空!";
  88.                 return false;
  89.             }
  90.             if (!File.Exists(zipFilePath))
  91.             {
  92.                 err = "压缩文件不存在!";
  93.                 return false;
  94.             }
  95.             //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹   
  96.             if (unZipDir == string.Empty)
  97.                 unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
  98.             if (!unZipDir.EndsWith("\"))
  99.                 unZipDir += "\";
  100.             if (!Directory.Exists(unZipDir))
  101.                 Directory.CreateDirectory(unZipDir);
  102.             try
  103.             {
  104.                 using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
  105.                 {
  106.                     ZipEntry theEntry;
  107.                     while ((theEntry = s.GetNextEntry()) != null)
  108.                     {
  109.                         string directoryName = Path.GetDirectoryName(theEntry.Name);
  110.                         string fileName = Path.GetFileName(theEntry.Name);
  111.                         if (directoryName.Length > 0)
  112.                         {
  113.                             Directory.CreateDirectory(unZipDir + directoryName);
  114.                         }
  115.                         if (!directoryName.EndsWith("\"))
  116.                             directoryName += "\";
  117.                         if (fileName != String.Empty)
  118.                         {
  119.                             using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
  120.                             {
  121.                                 int size = 2048;
  122.                                 byte[] data = new byte[2048];
  123.                                 while (true)
  124.                                 {
  125.                                     size = s.Read(data, 0, data.Length);
  126.                                     if (size > 0)
  127.                                     {
  128.                                         streamWriter.Write(data, 0, size);
  129.                                     }
  130.                                     else
  131.                                     {
  132.                                         break;
  133.                                     }
  134.                                 }
  135.                             }
  136.                         }
  137.                     }//while   
  138.                 }
  139.             }
  140.             catch (Exception ex)
  141.             {
  142.                 err = ex.Message;
  143.                 return false;
  144.             }
  145.             return true;
  146.         }//解压结束  
  147.         #endregion
  148.     }
  149. }
复制代码
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
金贤重 | 2012-12-9 02:17:51 | 显示全部楼层
yubx7007 | 2012-12-29 14:57:45 | 显示全部楼层
顶一下顶一下顶一下顶一下
miracle | 2013-3-11 16:25:33 | 显示全部楼层
顶顶!
chao2332601 | 2013-6-16 01:04:06 | 显示全部楼层
谢谢分享!!!
chao2332601 | 2013-6-16 04:52:22 | 显示全部楼层
谢谢分享!!!
破孩子 | 2013-7-31 10:07:31 | 显示全部楼层
新手,看不太明白,不过先顶一下吧
kkkkqwx | 2013-12-20 09:46:05 | 显示全部楼层
ching | 2014-9-22 14:21:36 | 显示全部楼层
C#领悟者 | 2014-9-24 11:09:52 | 显示全部楼层
这个开源插件不错 留了
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则