1. /// <summary>
  2. /// Zip 压缩文件
  3. /// </summary>
  4. public class Zip
  5. {
  6. public Zip()
  7. {
  8.  
  9. }
  10. #region 加压方法
  11. /// <summary>
  12. /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略)
  13. /// </summary>
  14. /// <param name="dirPath">被压缩的文件夹夹路径</param>
  15. /// <param name="zipFilePath">生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip</param>
  16. /// <param name="err">出错信息</param>
  17. /// <returns>是否压缩成功</returns>
  18. public static bool ZipFile(string dirPath, string zipFilePath, out string err)
  19. {
  20. err = "";
  21. if (dirPath == string.Empty)
  22. {
  23. err = "要压缩的文件夹不能为空!";
  24. return false;
  25. }
  26. if (!Directory.Exists(dirPath))
  27. {
  28. err = "要压缩的文件夹不存在!";
  29. return false;
  30. }
  31. //压缩文件名为空时使用文件夹名+.zip
  32. if (zipFilePath == string.Empty)
  33. {
  34. if (dirPath.EndsWith("//"))
  35. {
  36. dirPath = dirPath.Substring(, dirPath.Length - );
  37. }
  38. zipFilePath = dirPath + ".zip";
  39. }
  40.  
  41. try
  42. {
  43. string[] filenames = Directory.GetFiles(dirPath);
  44. using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
  45. {
  46. s.SetLevel();
  47. byte[] buffer = new byte[];
  48. foreach (string file in filenames)
  49. {
  50. ZipEntry entry = new ZipEntry(Path.GetFileName(file));
  51. entry.DateTime = DateTime.Now;
  52. s.PutNextEntry(entry);
  53. using (FileStream fs = File.OpenRead(file))
  54. {
  55. int sourceBytes;
  56. do
  57. {
  58. sourceBytes = fs.Read(buffer, , buffer.Length);
  59. s.Write(buffer, , sourceBytes);
  60. } while (sourceBytes > );
  61. }
  62. }
  63. s.Finish();
  64. s.Close();
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. err = ex.Message;
  70. return false;
  71. }
  72. return true;
  73. }
  74. #endregion
  75.  
  76. #region 解压
  77. /// <summary>
  78. /// 功能:解压zip格式的文件。
  79. /// </summary>
  80. /// <param name="zipFilePath">压缩文件路径</param>
  81. /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
  82. /// <param name="err">出错信息</param>
  83. /// <returns>解压是否成功</returns>
  84. public static bool UnZipFile(string zipFilePath, string unZipDir, out string err)
  85. {
  86. err = "";
  87. if (zipFilePath == string.Empty)
  88. {
  89. err = "压缩文件不能为空!";
  90. return false;
  91. }
  92. if (!File.Exists(zipFilePath))
  93. {
  94. err = "压缩文件不存在!";
  95. return false;
  96. }
  97. //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
  98. if (unZipDir == string.Empty)
  99. unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
  100. if (!unZipDir.EndsWith("//"))
  101. unZipDir += "//";
  102. if (!Directory.Exists(unZipDir))
  103. Directory.CreateDirectory(unZipDir);
  104.  
  105. try
  106. {
  107. using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
  108. {
  109.  
  110. ZipEntry theEntry;
  111. while ((theEntry = s.GetNextEntry()) != null)
  112. {
  113. string directoryName = Path.GetDirectoryName(theEntry.Name);
  114. string fileName = Path.GetFileName(theEntry.Name);
  115. if (directoryName.Length > )
  116. {
  117. Directory.CreateDirectory(unZipDir + directoryName);
  118. }
  119. if (!directoryName.EndsWith("//"))
  120. directoryName += "//";
  121. if (fileName != String.Empty)
  122. {
  123. using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
  124. {
  125.  
  126. int size = ;
  127. byte[] data = new byte[];
  128. while (true)
  129. {
  130. size = s.Read(data, , data.Length);
  131. if (size > )
  132. {
  133. streamWriter.Write(data, , size);
  134. }
  135. else
  136. {
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. }//while
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. err = ex.Message;
  148. return false;
  149. }
  150. return true;
  151. }//解压结束
  152. #endregion

C#压缩解压zip 文件的更多相关文章

  1. (转载)C#压缩解压zip 文件

    转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...

  2. 原生java 压缩解压zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  3. android压缩解压zip文件

    网上各种方法的收集: 1.上次写了个解压缩功能,但有局限性,比如压缩文件xx.zip 里包括子目录的情况下,执行上次解压缩的功能就不能实现我们想要的效果,于是在网上参考了一下java的解压缩功能.对上 ...

  4. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  5. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天

    PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...

  6. java中自己常用到的工具类-压缩解压zip文件

    package com.ricoh.rapp.ezcx.admintoolweb.util; import java.io.File; import java.io.FileInputStream; ...

  7. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载

    文章转载自:https://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PH ...

  8. Android 解压zip文件(支持中文)

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

  9. Android 解压zip文件

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

随机推荐

  1. 一百零八、SAP的OO-ALV之二,创建屏幕Screen

    一.在资源管理器,右键->创建屏幕 二.输入4位数字 三.输入屏幕的描述 四.在逻辑流里面PBO用于显示屏幕,PAI用于用户交互. 五.在元素清单里面,在屏幕中的所有元素都是在元素清单中的

  2. 二十一、SAP中通过内表输出数据库中数据

    一.我们查看一个SCARR的一个数据库 二.数据库内容如下 三.我们写一个关于内表使用的代码,来显示这个数据库内容 四.输出如下

  3. 【CF1154G】Minimum Possible LCM

    题意 给你 \(n\) 个数 \(a_i\) ,求出 \(\text{lcm}\) 最小的一对数. \(n\le 10^6, a_i\le 10^7\) 题解 直接枚举 ,找到当前数最小的两个倍数,统 ...

  4. NASA的10条编码规则

    关于NASA的10条编程规则,他们曾表示:这些规则的作用就像汽车上的安全带:最初,它们可能有点不舒服,但过了一会儿,它们的使用就变成了第二天性,而没有使用它们就变得不可想象. Gerard J. Ho ...

  5. 吴裕雄--天生自然C++语言学习笔记:C++ 文件和流

    如何从文件读取流和向文件写入流.这就需要用到 C++ 中另一个标准库 fstream,它定义了三个新的数据类型: ofstream 该数据类型表示输出文件流,用于创建文件并向文件写入信息. ifstr ...

  6. Dynamic Route Matching Vue路由(1)

    Dynamic Route Matching 动态的 路由 匹配 Very often we will need to map routes with the given pattern to the ...

  7. 前端基础之Html、CSS

    Html.css相关 Html Html结构: 标签 描述 <!DOCTYPE html> 文档声明 <html> 根元素 <head> 头部 <body 主 ...

  8. css设置兄弟节点的样式(相邻的前一个节点)

    产品需求:想在鼠标移动到“移除”的时候,“1.产品匹配测试”添加下划线和更改字体颜色 需求分析:从需求可以看出使用 :hover 就可以解决的问题,但是在实践中发现兄弟选择器(+)不好使,(+)只能是 ...

  9. Arduino - Nano针脚分配时需要注意的事项

    0.1为Rx.Tx 针脚,这两个针脚一般作为串口使用,非串口设备尽量不占用该针脚.2.3为中断口,分别对应中断0.中断1,需要中断功能的设备,必须接入此.2-13.A0-A5,共18个针脚,都可以作为 ...

  10. 分享几个IntelliJ IDEA 2019 jihuo码(pojie码、zhuce码),亲测可用

    文章转载自:https://www.jiweichengzhu.com/article/eb340e382d1d456c84a1d190db12755c 如果还有问题,加群交流:686430774(就 ...