公用方法

 

//文件转换为字节  
public static byte[] File2Bytes(string path) { if (!System.IO.File.Exists(path)) { return new byte[0]; } FileInfo fi = new FileInfo(path); byte[] buff = new byte[fi.Length]; FileStream fs = fi.OpenRead(); fs.Read(buff, 0, Convert.ToInt32(fs.Length)); fs.Close(); return buff; }

  

//生成压缩文件
public static void CompressedFiles(string topDirectoryName, string zipedFileName, int compresssionLevel, string password, string comment) { using (ZipOutputStream zos = new ZipOutputStream(System.IO.File.Open(zipedFileName, FileMode.OpenOrCreate))) { if (compresssionLevel != 0) { zos.SetLevel(compresssionLevel);//设置压缩级别 } if (!string.IsNullOrEmpty(password)) { zos.Password = password;//设置zip包加密密码 } if (!string.IsNullOrEmpty(comment)) { zos.SetComment(comment);//设置zip包的注释 } //循环设置目录下所有的*.png文件(支持子目录搜索) foreach (string file in Directory.GetFiles(topDirectoryName, "*.txt", SearchOption.AllDirectories)) { if (System.IO.File.Exists(file)) { FileInfo item = new FileInfo(file); FileStream fs = System.IO.File.OpenRead(item.FullName); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(item.Name); zos.PutNextEntry(entry); zos.Write(buffer, 0, buffer.Length); } } } }

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值