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
{
///
/// 存放待压缩的文件的绝对路径
///
private List AbsolutePaths { set; get; }
public string errorMsg { set; get; }
public ZipHelper()
{
errorMsg = "";
AbsolutePaths = new List();
}
///
/// 添加压缩文件或文件夹
///
/// 文件或文件夹的绝对路径
public void AddFile(string _fileAbsolutePath)
{
AbsolutePaths.Add(_fileAbsolutePath);
}
///
/// 压缩文件或者文件夹
///
/// 压缩后文件的存放路径 如C:\\windows\abc.zip
/// 压缩级别0~9,数字越大压缩率越高,默认为5
///
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); //写入字节
}
}
}
///
/// 解压
///
/// 压缩文件路径
/// 解压的路径
///
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, 0, bts.Length);
if (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;
}
}
} 0
0
相关文章
Blazor 怎么实现分页功能
maui 是什么 .net maui开发入门
C# MAUI怎么使用C# Markup编写UI MAUI C# UI教程
c# 如何用C#代码触发一次GC回收 Full GC
c# C#中的幽灵读和不可重复读怎么在代码层面避免
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。
28
2026.01.26
在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。
8
2026.01.26
苹果官方查询网站主要通过 checkcoverage.apple.com/cn/zh/ 进行,可用于查询序列号(SN)对应的保修状态、激活日期及技术支持服务。此外,查找丢失设备请使用 iCloud.com/find,购买信息与物流可访问 Apple (中国大陆) 订单状态页面。
31
2026.01.26
NPD(Narcissistic Personality Disorder)即自恋型人格障碍,是一种心理健康问题,特点是极度夸大自我重要性、需要过度赞美与关注,同时极度缺乏共情能力,背后常掩藏着低自尊和不安全感,影响人际关系、工作和生活,通常在青少年时期开始显现,需由专业人士诊断。
3
2026.01.26
关闭Windows安全中心(Windows Defender)可通过系统设置暂时关闭,或使用组策略/注册表永久关闭。最简单的方法是:进入设置 > 隐私和安全性 > Windows安全中心 > 病毒和威胁防护 > 管理设置,将实时保护等选项关闭。
5
2026.01.26
铁路12306提供起售时间查询、起售提醒、购票预填、候补购票及误购限时免费退票五项服务,并强调官方渠道唯一性与信息安全。
35
2026.01.26
以工资薪金所得为例,应纳税额 = 应纳税所得额 × 税率 - 速算扣除数。应纳税所得额 = 月度收入 - 5000 元 - 专项扣除 - 专项附加扣除 - 依法确定的其他扣除。假设某员工月工资 10000 元,专项扣除 1000 元,专项附加扣除 2000 元,当月应纳税所得额为 10000 - 5000 - 1000 - 2000 = 2000 元,对应税率为 3%,速算扣除数为 0,则当月应纳税额为 2000×3% = 60 元。
12
2026.01.26
oppo云服务https://cloud.oppo.com/可以在云端安全存储您的照片、视频、联系人、便签等重要数据。当您的手机数据意外丢失或者需要更换手机时,可以随时将这些存储在云端的数据快速恢复到手机中。
40
2026.01.26
网页端充值步骤:打开浏览器,输入https://www.douyin.com,登录账号;点击右上角头像,选择“钱包”;进入“充值中心”,操作和APP端一致。注意:切勿通过第三方链接、二维码充值,谨防受骗
7
2026.01.26
热门下载
相关下载
精品课程
最新文章
