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包不能很好的解决解压问 ...
随机推荐
- JS 三大难点
1,作用域链 2,原型链 3,闭包
- 089-PHP数组运用 - 通过循环函数取出部分成员合并成新数组
<?php function myfunc($arr){ //定义过滤函数 $j=count($arr); for($i=0;$i<$j;$i++){ if($arr[$i]>=0& ...
- [ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: URLError: <urlopen error [Errno 10061] Connection refused>
[ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: URLError: <urlopen error ...
- 前端基础之AJAX
AJAX 什么是AJAX,简单来说就是利用JavaScript天生异步的特性,使用异步请求后台数据,从而达到不刷新网页也能局部更新页面的效果. 原生AJAX JavaScript中的AJAX依赖于XM ...
- CodeForces - 782B The Meeting Place Cannot Be Changed(精度二分)
题意:在一维坐标轴上,给定n个点的坐标以及他们的最大移动速度,问他们能聚到某一点处的最短时间. 分析: 1.二分枚举最短时间即可. 2.通过检查当前时间下,各点的最大移动范围之间是否有交集,不断缩小搜 ...
- HDU 1576:A/B
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- javascript面向对象编程的3种常见封装形式解析
javascript如何才能脱离函数式编程,拥抱面向对象编程呢,常见的有3种形式,其它形式可以说都是这3种的变种. 1.直接定义对象直接量的形式 var Util={ getType: ...
- API网关,让您和Serverless再近一步
从软件行业诞生之日起,技术和业务总是相辅相成.业务的创新促进着软件架构从最早期的单体架构.分布式架构,转变到了如今火热的微服务架构,以及云时代下诞生的Serverless架构.API网关作为单体应用和 ...
- Unity3d中渲染到RenderTexture的原理,几种方式以及一些问题
超级搬运工 http://blog.csdn.net/leonwei/article/details/54972653 ---------------------------------------- ...
- 读取cookie、写进cookie方法
整理 读取cookie.写进cookie方法. //设置cookies中的值 function setCookie(name, value) { var Days = 30; var exp = ne ...