当前位置: 移动技术网 > IT编程>开发语言>c# > C#使用iCSharpcode进行文件压缩实现方法

C#使用iCSharpcode进行文件压缩实现方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

卢斯侃,3u8861,丽水教育网中考成绩查询

本文所述为一个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<string> zip_filelist = new list<string>();
    /// <summary>
    /// 压缩后的文件名称
    /// </summary>
    public string zipname
    {
      get { return zip_name; }
      set { zip_name = value; }
    }
    /// <summary>
    /// 压缩的文件路径
    /// </summary>
    public string zipdirectoryname
    {
      get { return zip_directoryname; }
      set { zip_directoryname = value; }
    }
    /// <summary>
    /// 压缩的文件列表
    /// </summary>
    public list<string> 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
  {
    /// <summary>
    /// 压缩文件参数
    /// </summary>
    public zipparameter zipparameter { get; set; }

    /// <summary>
    /// 压缩文件返回压缩后的信息
    /// </summary>
    /// <returns>string 返回压缩后的提示信息</returns>
    public string compressreturnmsg()
    {
      filestream zip_file;
      zipoutputstream zipstream;
      zipentry zipentry;
      string rtnmessage = "";//返回的信息

      try
      {
        //循环文件,如果文件不存在就不添加的压缩里面
        for (int i = 0; i < zipparameter.zipfilelist.count; i++)
        {
          if (!file.exists(zipparameter.zipfilelist[i]))
          {
            zipparameter.zipfilelist.removeat(i);
            i--;
          }

        }
        //没有有文件下面的压缩不执行
        if (zipparameter.zipfilelist.count == 0)
        {
          return " file not find";
        }
        //没有目录进行创建
        if (!directory.exists(zipparameter.zipdirectoryname))
        {
          directory.createdirectory(zipparameter.zipdirectoryname);
        }

        // 解决文档名称乱码问题,出现乱码就是因为codepage不对
        encoding gbk = encoding.getencoding("gbk");
        icsharpcode.sharpziplib.zip.zipconstants.defaultcodepage = gbk.codepage;

        //文件路径,文档路径与文件名称
        string strpath = zipparameter.zipdirectoryname + zipparameter.zipname;

        zip_file = file.create(strpath);
        zipstream = new zipoutputstream(zip_file);
        foreach (string filetozip in zipparameter.zipfilelist)
        {
          zip_file = file.openread(filetozip);
          byte[] buffer = new byte[zip_file.length];
          zip_file.read(buffer, 0, buffer.length);
          zip_file.close();
          zipentry = new zipentry(path.getfilename(filetozip));
          zipstream.putnextentry(zipentry);
          zipstream.write(buffer, 0, buffer.length);
        }
        zipstream.finish();
        zipstream.close();
        zip_file.close();
        rtnmessage = "success";
      }
      catch (exception ex)
      {
        rtnmessage = "fail:" + ex.message;
      }
      finally
      {
        gc.collect();
        gc.collect(1);
      }
      return rtnmessage;
    }
  }
}

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#程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网