当前位置: 移动技术网 > IT编程>开发语言>.net > 使用微软自带解压类压缩文件夹

使用微软自带解压类压缩文件夹

2018年11月15日  | 移动技术网IT编程  | 我要评论

0度终极幻想2,3u8506,仙逆燃文

前言

因为即时通信项目中,需要同步oa中的用户头像,用户头像是通过文件夹保存的,文件夹内结构比较复杂。在即时通信中需要先将oa服务器上保存的用户头像文件夹下载下来,因为直接下载文件夹方法很难,所以需要先将文件夹压缩一下,然后在直接下载压缩文件。

 

他人雅慧

在网上找了不少了例子,几乎都是使用sharpziplib开源库的方式,大体看了一下,感觉比较复杂,后在某篇教程的评论中发现更好的方式,于.net  framework 4.5框架以后,可以直接使用微软官方的zipfile类就可,实践后发现真的好用,什么都不需要管,直接调用方法就行,果然还是微软爸爸比较强大。(不需要考虑递归文件夹)

 

使用方法

准备
添加dll:
右键项目添加程序集 system.io.compression.dll,system.io.compression.filesystem.dll

压缩
zipfile.createfromdirectory(@"f:\aps.datainterface\bin\debug", appdomain.currentdomain.basedirectory+"newdebug.zip");
解释一下两个参数,第一个参数是要 压缩的文件(文件夹),第二个参数是保存压缩内容的文件。

解压
zipfile.extracttodirectory(appdomain.currentdomain.basedirectory+"newdebug.zip", @"f:\aps.datainterface\bin\debug");

解释一下两个参数,第一个参数是要 解压的zip文件,第二个参数是保存解压内容的路径。

其他的一些操作
提取单个文件

using (ziparchive ziparchive =
zipfile.open(appdomain.currentdomain.basedirectory + "newdebug.zip", ziparchivemode.read))
{
foreach (ziparchiveentry entry in ziparchive.entries)
{
if (entry.name == "aps.ep.controls.dll")
{
using (stream stream = entry.open())
{
if (!directory.exists(appdomain.currentdomain.basedirectory + "oneone"))
{
directory.createdirectory(appdomain.currentdomain.basedirectory + "oneone");
}
entry.extracttofile(appdomain.currentdomain.basedirectory + "oneone\\aps.ep.controls.dll", true);
}
}
}
}

压缩指定文件类型文件
ienumerable<string> files =
directory.enumeratefiles@"f:\aps.datainterface\bin\debug", "*.dll");
using (ziparchive ziparchive =
zipfile.open(appdomain.currentdomain.basedirectory+"newdebug.zip", ziparchivemode.create))
{
foreach (string file in files)
{
var entryname = path.combine("dll", path.getfilename(file));
ziparchive.createentryfromfile(file, entryname);
}
}

当然还有其他的一些方法,暂时用不到,不多做研究。

 

项目实战中的简单通用类

 

  1 /// <summary>
  2     /// 压缩、解压帮助类
  3     /// </summary>
  4     public class ziphelper
  5     {
  6         #region 单例模式
  7         private volatile static ziphelper _instance = null;
  8         private static readonly object lockhelper = new object();//线程锁
  9         public static ziphelper instance
 10         {
 11             get
 12             {
 13                 if (_instance == null)
 14                 {
 15                     lock (lockhelper)
 16                     {
 17                         if (_instance == null)
 18                         {
 19                             _instance = new ziphelper();
 20                         }
 21                     }
 22                 }
 23                 return _instance;
 24             }
 25         }
 26         #endregion 单例模式
 27 
 28         #region 构造函数
 29         public ziphelper()
 30         {
 31 
 32         }
 33         #endregion 构造函数
 34 
 35         #region 方法
 36         /// <summary>
 37         /// 简单压缩方法
 38         /// </summary>
 39         /// <param name="filepath">压缩内容路径</param>
 40         /// <param name="zippath">压缩后文件保存路径</param>
 41         /// <returns></returns>
 42         public bool compress(string filepath,string zippath)
 43         {
 44             try
 45             {
 46                 if (!directory.exists(filepath)) return false;
 47                 createdirectory(zippath);
 48                 zipfile.createfromdirectory(filepath, zippath);
 49             }
 50             catch (exception ex)
 51             {
 52                 string errormes =loghelper.tomessage(ex);
 53                 string path = string.empty;
 54                 path += appdomain.currentdomain.basedirectory;
 55                 path += @"log\ziperrorlog\zip";
 56                 path += datetime.now.tostring("yyyymmddhhmm");
 57                 path += ".txt";
 58                 loghelper.instance.writelog(path, errormes);
 59                 return false;
 60             }
 61             return true;
 62         }
 63         /// <summary>
 64         /// 简单解压方法
 65         /// </summary>
 66         /// <param name="zippath">压缩文件所在路径</param>
 67         /// <param name="savepath">解压后保存路径</param>
 68         /// <returns></returns>
 69         public bool decompress(string zippath,string savepath)
 70         {
 71             try
 72             {
 73                 if (!directory.exists(zippath)) return false;
 74                 zipfile.extracttodirectory(zippath, savepath);
 75             }
 76             catch (exception ex)
 77             {
 78                 string errormes = loghelper.tomessage(ex);
 79                 string path = string.empty;
 80                 path += appdomain.currentdomain.basedirectory;
 81                 path += @"log\ziperrorlog\dezip";
 82                 path += datetime.now.tostring("yyyymmddhhmm");
 83                 path += ".txt";
 84                 loghelper.instance.writelog(path, errormes);
 85                 return false;
 86             }
 87             return true;
 88         }
 89 
 90         /// <summary>
 91         /// 指定目录下压缩指定类型文件
 92         /// </summary>
 93         /// <param name="filepath">指定目录</param>
 94         /// <param name="zippath">压缩后保存路径</param>
 95         /// <param name="foldername">压缩文件内部文件夹名</param>
 96         /// <param name="filetype">指定类型 格式如:*.dll</param>
 97         /// <returns></returns>
 98         public bool compress(string filepath, string zippath,string foldername,string filetype)
 99         {
100             try
101             {
102                 ienumerable<string> files =
103               directory.enumeratefiles(filepath, filetype);
104                 using (ziparchive ziparchive =
105                   zipfile.open(zippath, ziparchivemode.create))
106                 {
107                     foreach (string file in files)
108                     {
109                         var entryname = system.io.path.combine(foldername, system.io.path.getfilename(file));
110                         ziparchive.createentryfromfile(file, entryname);
111                     }
112                 }
113             }
114             catch (exception ex)
115             {
116                 string errormes = loghelper.tomessage(ex);
117                 string path = string.empty;
118                 path += appdomain.currentdomain.basedirectory;
119                 path += @"log\ziperrorlog\zip1";
120                 path += datetime.now.tostring("yyyymmddhhmm");
121                 path += ".txt";
122                 loghelper.instance.writelog(path, errormes);
123                 return false;
124             }
125             return true;
126         }
127 
128         #region 调用方法
129         /// <summary>
130         /// 创建父级路径
131         /// </summary>
132         /// <param name="infopath"></param>
133         private void createdirectory(string infopath)
134         {
135             directoryinfo directoryinfo = directory.getparent(infopath);
136             if (!directoryinfo.exists)
137             {
138                 directoryinfo.create();
139             }
140         }
141 
142         #endregion 
143         #endregion 方法
144     }
view code

 

尾声

解压压缩的方法还有很多,但既然可以使用微软自带的方法,何乐而不为呢。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网