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>文件或文件夹的绝对路径
public void AddFile(string _fileAbsolutePath)
{
AbsolutePaths.Add(_fileAbsolutePath);
}
/// <summary>
/// 压缩文件或者文件夹
/// </summary>
/// <param>压缩后文件的存放路径 如C:\windowsbc.zip
/// <param>压缩级别0~9,数字越大压缩率越高,默认为5
/// <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, 0, bts.Length);
ZipEntry ze = new ZipEntry(new FileInfo(path).Name);
ComStream.PutNextEntry(ze); //为压缩文件流提供一个容器
ComStream.Write(bts, 0, 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, 0, bts.Length);
ZipEntry ze = new ZipEntry(path);
zos.PutNextEntry(ze); //为压缩文件流提供一个容器
zos.Write(bts, 0, bts.Length); //写入字节
}
}
}
/// <summary>
/// 解压
/// </summary>
/// <param>压缩文件路径
/// <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 0)
{
fs.Write(bts, 0, 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;
}
}
}</string></string>
C#压缩解压帮助类
来源:这里教程网
时间:2026-02-21 13:01:23
作者:
编辑推荐:
- C#压缩解压帮助类02-21
- C# 如何调用 C++ DLL中的函数接口和回调函数02-21
- C#中下限非零的数组解析02-21
- 摩斯密码(Morse code) C#实现02-21
- C#使用记秒表检查程序运行时间02-21
- 使用Ajax生成的Excel文件并下載02-21
- C# DateTime与时间戳转换02-21
- C++程序员Protocol Buffers基础指南02-21
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- C#中下限非零的数组解析
C#中下限非零的数组解析
26-02-21 - 摩斯密码(Morse code) C#实现
摩斯密码(Morse code) C#实现
26-02-21 - C#使用记秒表检查程序运行时间
C#使用记秒表检查程序运行时间
26-02-21 - 基于C#的百度图片批量下载工具
基于C#的百度图片批量下载工具
26-02-21 - 《超级马力欧银河大电影》角色阵容泄露,瓦力欧有望联手库巴 Jr.登场
《超级马力欧银河大电影》角色阵容泄露,瓦力欧有望联手库巴 Jr.登场
26-02-21 - LibreOffice 抨击 OnlyOffice 是“伪开源”,联手微软裹挟用户
- 任天堂 GBA 经典复刻《宝可梦 火红/叶绿》上架 Switch 商店,2 月 27 日正式发售
- 索尼出手叫停粉丝重制《血源诅咒》项目,系列游戏前景渺茫
索尼出手叫停粉丝重制《血源诅咒》项目,系列游戏前景渺茫
26-02-21 - 游戏《尼尔:自动人形》累计销量突破 1000 万份
游戏《尼尔:自动人形》累计销量突破 1000 万份
26-02-21 - 育碧多伦多工作室裁员 40 人,《细胞分裂》重制版游戏仍在推进
育碧多伦多工作室裁员 40 人,《细胞分裂》重制版游戏仍在推进
26-02-21
