作者:书友30905431 | 来源:互联网 | 2022-08-08 19:05
本文所述为一个C#使用iCSharpcode压缩的使用类,经测试效果不错。分享给大家供大家参考之用。具体方法如下:
1.参数类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ZipCompress
{
public class ZipParameter
{
private string zip_Name = "";
private string zip_DirectoryName = "";
private List zip_FileList = new List();
///
/// 压缩后的文件名称
///
public string ZIPName
{
get { return zip_Name; }
set { zip_Name = value; }
}
///
/// 压缩的文件路径
///
public string ZIPDirectoryName
{
get { return zip_DirectoryName; }
set { zip_DirectoryName = value; }
}
///
/// 压缩的文件列表
///
public List ZIPFileList
{
get { return zip_FileList; }
set { zip_FileList = value; }
}
}
}
2.工作类
//****************************************************************************************
//功能:实现文件压缩
//使用方法:设置参数进行压缩
//*****************************************************************************************
using System;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Text;
namespace ZipCompress
{
public class CompressFile
{
///
/// 压缩文件参数
///
public ZipParameter ZipParameter { get; set; }
///
/// 压缩文件返回压缩后的信息
///
/// string 返回压缩后的提示信息
public string CompressReturnMsg()
{
FileStream Zip_File;
ZipOutputStream ZipStream;
ZipEntry ZipEntry;
string rtnMessage = "";//返回的信息
try
{
//循环文件,如果文件不存在就不添加的压缩里面
for (int i = 0; i
3.使用类
ZipParameter zp = new ZipParameter();
zp.ZIPDirectoryName = @"C:\Users\Public\Pictures\Sample Pictures\";
zp.ZIPName = "Test.zip";
zp.ZIPFileList.Add(@"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
zp.ZIPFileList.Add(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg");
zp.ZIPFileList.Add(@"C:\Users\Public\Pictures\Sample Pictures\错误文件.jpg");
CompressFile cprFile = new CompressFile();
cprFile.ZipParameter = zp;
string strMessage = cprFile.CompressReturnMsg();
4.文件源码点此本站下载
希望本文所述对大家的C#程序设计有所帮助。