码迷,mamicode.com
首页 > 其他好文 > 详细

公用方法

时间:2020-07-03 17:09:28      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:方法   public   子目录   密码   res   isnull   files   stream   filename   

 

//文件转换为字节  
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); } } } }

  

公用方法

标签:方法   public   子目录   密码   res   isnull   files   stream   filename   

原文地址:https://www.cnblogs.com/opts/p/13231325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!