C#压缩解压帮助类
发布时间:2020-12-15 17:59:06 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using ICSharpCode.SharpZipLib.Zip; nam
|
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;
namespace GzRMIS.Main.Business
{
public class ZipHelper
{
/// <summary>
/// 存放待压缩的文件的绝对路径
/// </summary>
private List<string> AbsolutePaths { set; get; }
public string errorMsg { set; get; }
public ZipHelper()
{
errorMsg = "";
AbsolutePaths = new List<string>();
}
/// <summary>
/// 添加压缩文件或文件夹
/// </summary>
/// <param name="_fileAbsolutePath">文件或文件夹的绝对路径</param>
public void AddFile(string _fileAbsolutePath)
{
AbsolutePaths.Add(_fileAbsolutePath);
}
/// <summary>
/// 压缩文件或者文件夹
/// </summary>
/// <param name="_depositPath">压缩后文件的存放路径 如C:windowsabc.zip</param>
/// <param name="_Level">压缩级别0~9,数字越大压缩率越高,默认为5</param>
/// <returns></returns>
public bool CompressionZip(string _depositPath,int _Level=5)
{
bool result = true;
FileStream fs = null;
try
{
ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath));
ComStream.SetLevel(_Level); //压缩等级
foreach (string path in AbsolutePaths)
{
//如果是目录
if (Directory.Exists(path))
{
ZipFloder(path,ComStream,path);
}
else if (File.Exists(path))//如果是文件
{
fs = File.OpenRead(path);
byte[] bts = new byte[fs.Length];
fs.Read(bts,bts.Length);
ZipEntry ze = new ZipEntry(new FileInfo(path).Name);
ComStream.PutNextEntry(ze); //为压缩文件流提供一个容器
ComStream.Write(bts,bts.Length); //写入字节
}
}
ComStream.Finish(); // 结束压缩
ComStream.Close();
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
errorMsg = ex.Message;
result = false;
}
return result;
}
//压缩文件夹
private void ZipFloder(string _OfloderPath,ZipOutputStream zos,string _floderPath)
{
foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())
{
if (Directory.Exists(item.FullName))
{
ZipFloder(_OfloderPath,zos,item.FullName);
}
else if (File.Exists(item.FullName))//如果是文件
{
DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);
string fullName2 = new FileInfo(item.FullName).FullName;
string path = ODir.Name + fullName2.Substring(ODir.FullName.Length,fullName2.Length - ODir.FullName.Length);//获取相对目录
FileStream fs = File.OpenRead(fullName2);
byte[] bts = new byte[fs.Length];
fs.Read(bts,bts.Length);
ZipEntry ze = new ZipEntry(path);
zos.PutNextEntry(ze); //为压缩文件流提供一个容器
zos.Write(bts,bts.Length); //写入字节
}
}
}
/// <summary>
/// 解压
/// </summary>
/// <param name="_depositPath">压缩文件路径</param>
/// <param name="_floderPath">解压的路径</param>
/// <returns></returns>
public bool DeCompressionZip(string _depositPath,string _floderPath)
{
bool result = true;
FileStream fs = null;
try
{
ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath));
ZipEntry ze = InpStream.GetNextEntry();//获取压缩文件中的每一个文件
Directory.CreateDirectory(_floderPath);//创建解压文件夹
while (ze != null)//如果解压完ze则是null
{
if (ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹文件名
{
string[] strs = ze.Name.Split('');//如果文件名中包含’‘则表明有文件夹
if (strs.Length > 1)
{
//两层循环用于一层一层创建文件夹
for (int i = 0; i < strs.Length - 1; i++)
{
string floderPath = _floderPath;
for (int j = 0; j < i; j++)
{
floderPath = floderPath + "" + strs[j];
}
floderPath = floderPath + "" + strs[i];
Directory.CreateDirectory(floderPath);
}
}
fs = new FileStream(_floderPath + "" + ze.Name,FileMode.OpenOrCreate,FileAccess.Write);//创建文件
//循环读取文件到文件流中
while (true)
{
byte[] bts = new byte[1024];
int i = InpStream.Read(bts,bts.Length);
if (i > 0)
{
fs.Write(bts,i);
}
else
{
fs.Flush();
fs.Close();
break;
}
}
}
ze = InpStream.GetNextEntry();
}
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
errorMsg = ex.Message;
result = false;
}
return result;
}
}
}
以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- React Native之React速学教程(下)
- Ruby语法问题:Rational(a,b)和Rational.new!(a,b)
- iphone – Segue崩溃 – 调试器没有显示任何有用的错误
- Flex 应用开发实战笔记
- OpenCV中C++函数imread读取图片的问题及解决方法
- ruby-on-rails – 从Ubuntu中的ROR应用程序连接到SQL Serve
- ruby – 何时使用关键字参数和选项哈希?
- c# – Orchard的IContentManager.BuildDisplay方法中的grou
- c# – 每个密钥可以有多个值的集合的最佳.NET类型是什么?
- 从sqlite获取的数据空字符串的判断方式
