C#压缩解压zip 文件
- /// <summary>
- /// Zip 压缩文件
- /// </summary>
- public class Zip
- {
- public Zip()
- {
- }
- #region 加压方法
- /// <summary>
- /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略)
- /// </summary>
- /// <param name="dirPath">被压缩的文件夹夹路径</param>
- /// <param name="zipFilePath">生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip</param>
- /// <param name="err">出错信息</param>
- /// <returns>是否压缩成功</returns>
- public static bool ZipFile(string dirPath, string zipFilePath, out string err)
- {
- err = "";
- if (dirPath == string.Empty)
- {
- err = "要压缩的文件夹不能为空!";
- return false;
- }
- if (!Directory.Exists(dirPath))
- {
- err = "要压缩的文件夹不存在!";
- return false;
- }
- //压缩文件名为空时使用文件夹名+.zip
- if (zipFilePath == string.Empty)
- {
- if (dirPath.EndsWith("//"))
- {
- dirPath = dirPath.Substring(, dirPath.Length - );
- }
- zipFilePath = dirPath + ".zip";
- }
- try
- {
- string[] filenames = Directory.GetFiles(dirPath);
- using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
- {
- s.SetLevel();
- byte[] buffer = new byte[];
- foreach (string file in filenames)
- {
- ZipEntry entry = new ZipEntry(Path.GetFileName(file));
- entry.DateTime = DateTime.Now;
- s.PutNextEntry(entry);
- using (FileStream fs = File.OpenRead(file))
- {
- int sourceBytes;
- do
- {
- sourceBytes = fs.Read(buffer, , buffer.Length);
- s.Write(buffer, , sourceBytes);
- } while (sourceBytes > );
- }
- }
- s.Finish();
- s.Close();
- }
- }
- catch (Exception ex)
- {
- err = ex.Message;
- return false;
- }
- return true;
- }
- #endregion
- #region 解压
- /// <summary>
- /// 功能:解压zip格式的文件。
- /// </summary>
- /// <param name="zipFilePath">压缩文件路径</param>
- /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
- /// <param name="err">出错信息</param>
- /// <returns>解压是否成功</returns>
- public static bool UnZipFile(string zipFilePath, string unZipDir, out string err)
- {
- err = "";
- if (zipFilePath == string.Empty)
- {
- err = "压缩文件不能为空!";
- return false;
- }
- if (!File.Exists(zipFilePath))
- {
- err = "压缩文件不存在!";
- return false;
- }
- //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
- if (unZipDir == string.Empty)
- unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
- if (!unZipDir.EndsWith("//"))
- unZipDir += "//";
- if (!Directory.Exists(unZipDir))
- Directory.CreateDirectory(unZipDir);
- try
- {
- using (ZipInputStream 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 (directoryName.Length > )
- {
- Directory.CreateDirectory(unZipDir + directoryName);
- }
- if (!directoryName.EndsWith("//"))
- directoryName += "//";
- if (fileName != String.Empty)
- {
- using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
- {
- int size = ;
- byte[] data = new byte[];
- while (true)
- {
- size = s.Read(data, , data.Length);
- if (size > )
- {
- streamWriter.Write(data, , size);
- }
- else
- {
- break;
- }
- }
- }
- }
- }//while
- }
- }
- catch (Exception ex)
- {
- err = ex.Message;
- return false;
- }
- return true;
- }//解压结束
- #endregion
C#压缩解压zip 文件的更多相关文章
- (转载)C#压缩解压zip 文件
转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...
- 原生java 压缩解压zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- android压缩解压zip文件
网上各种方法的收集: 1.上次写了个解压缩功能,但有局限性,比如压缩文件xx.zip 里包括子目录的情况下,执行上次解压缩的功能就不能实现我们想要的效果,于是在网上参考了一下java的解压缩功能.对上 ...
- JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包
package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...
- java中自己常用到的工具类-压缩解压zip文件
package com.ricoh.rapp.ezcx.admintoolweb.util; import java.io.File; import java.io.FileInputStream; ...
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载
文章转载自:https://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PH ...
- Android 解压zip文件(支持中文)
过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...
- Android 解压zip文件
过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...
随机推荐
- 一百零八、SAP的OO-ALV之二,创建屏幕Screen
一.在资源管理器,右键->创建屏幕 二.输入4位数字 三.输入屏幕的描述 四.在逻辑流里面PBO用于显示屏幕,PAI用于用户交互. 五.在元素清单里面,在屏幕中的所有元素都是在元素清单中的
- 二十一、SAP中通过内表输出数据库中数据
一.我们查看一个SCARR的一个数据库 二.数据库内容如下 三.我们写一个关于内表使用的代码,来显示这个数据库内容 四.输出如下
- 【CF1154G】Minimum Possible LCM
题意 给你 \(n\) 个数 \(a_i\) ,求出 \(\text{lcm}\) 最小的一对数. \(n\le 10^6, a_i\le 10^7\) 题解 直接枚举 ,找到当前数最小的两个倍数,统 ...
- NASA的10条编码规则
关于NASA的10条编程规则,他们曾表示:这些规则的作用就像汽车上的安全带:最初,它们可能有点不舒服,但过了一会儿,它们的使用就变成了第二天性,而没有使用它们就变得不可想象. Gerard J. Ho ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 文件和流
如何从文件读取流和向文件写入流.这就需要用到 C++ 中另一个标准库 fstream,它定义了三个新的数据类型: ofstream 该数据类型表示输出文件流,用于创建文件并向文件写入信息. ifstr ...
- Dynamic Route Matching Vue路由(1)
Dynamic Route Matching 动态的 路由 匹配 Very often we will need to map routes with the given pattern to the ...
- 前端基础之Html、CSS
Html.css相关 Html Html结构: 标签 描述 <!DOCTYPE html> 文档声明 <html> 根元素 <head> 头部 <body 主 ...
- css设置兄弟节点的样式(相邻的前一个节点)
产品需求:想在鼠标移动到“移除”的时候,“1.产品匹配测试”添加下划线和更改字体颜色 需求分析:从需求可以看出使用 :hover 就可以解决的问题,但是在实践中发现兄弟选择器(+)不好使,(+)只能是 ...
- Arduino - Nano针脚分配时需要注意的事项
0.1为Rx.Tx 针脚,这两个针脚一般作为串口使用,非串口设备尽量不占用该针脚.2.3为中断口,分别对应中断0.中断1,需要中断功能的设备,必须接入此.2-13.A0-A5,共18个针脚,都可以作为 ...
- 分享几个IntelliJ IDEA 2019 jihuo码(pojie码、zhuce码),亲测可用
文章转载自:https://www.jiweichengzhu.com/article/eb340e382d1d456c84a1d190db12755c 如果还有问题,加群交流:686430774(就 ...