using ICSharpCode.SharpZipLib.Checksum; using ICSharpCode.SharpZipLib.Zip; using System; using System.IO; using System.Text; using Znyc.Cloudcar.Admin.Commons.Extend; using Znyc.Cloudcar.Admin.Commons.Log; namespace Znyc.Cloudcar.Admin.Commons.Helpers { /// /// 文件处理帮助类 /// public class FileHelper { /// /// 制作压缩包(多个文件压缩到一个压缩包,支持加密、注释) /// /// 压缩文件目录 /// 压缩包文件名 /// 压缩级别 1-9 /// 密码 /// 注释 /// 文件类型 public static void ZipFiles(string topDirectoryName, string zipedFileName, int compresssionLevel, string password, string comment, string filetype) { using (ZipOutputStream zos = new ZipOutputStream(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包的注释 } //循环设置目录下所有的*.jpg文件(支持子目录搜索) foreach (string file in Directory.GetFiles(topDirectoryName, filetype, SearchOption.AllDirectories)) { if (File.Exists(file)) { FileInfo item = new FileInfo(file); FileStream fs = 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); fs.Close(); } } zos.Close(); } } /// /// 压缩多层目录 /// /// 压缩文件目录 /// 压缩包文件名 /// 压缩级别 1-9 /// 密码 /// 注释 /// 文件类型 public static void ZipFileDirectory(string topDirectoryName, string zipedFileName, int compresssionLevel, string password, string comment, string filetype) { using (FileStream ZipFile = File.Open(zipedFileName, FileMode.OpenOrCreate)) { using (ZipOutputStream zos = new ZipOutputStream(ZipFile)) { if (compresssionLevel != 0) { zos.SetLevel(compresssionLevel); //设置压缩级别 } if (!string.IsNullOrEmpty(password)) { zos.Password = password; //设置zip包加密密码 } if (!string.IsNullOrEmpty(comment)) { zos.SetComment(comment); //设置zip包的注释 } ZipSetp(topDirectoryName, zos, "", filetype); } } } /// /// 递归遍历目录 /// /// The directory. /// The ZipOutputStream Object. /// The parent path. private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath, string filetype) { if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar) { strDirectory += Path.DirectorySeparatorChar; } Crc32 crc = new Crc32(); string[] filenames = Directory.GetFileSystemEntries(strDirectory, filetype); foreach (string file in filenames) // 遍历所有的文件和目录 { if (Directory.Exists(file)) // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 { string pPath = parentPath; pPath += file.Substring(file.LastIndexOf("\\") + 1); pPath += "\\"; ZipSetp(file, s, pPath, filetype); } else // 否则直接压缩文件 { //打开压缩文件 using (FileStream fs = File.OpenRead(file)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1); ZipEntry entry = new ZipEntry(fileName) { DateTime = DateTime.Now, Size = fs.Length }; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } } } } /// /// 读文件 /// /// /// public static string ReadFile(string path) { path = path.ToFilePath(); if (!File.Exists(path)) { return ""; } using (StreamReader stream = new StreamReader(path)) { return stream.ReadToEnd(); // 读取文件 } } /// /// 写文件 /// /// 文件路径 /// 文件名称 /// 文件内容 /// 是否追加到最后 public static void WriteFile(string path, string fileName, string content, bool appendToLast = false) { if (!path.EndsWith("\\")) { path = path + "\\"; } path = path.ToFilePath(); if (!Directory.Exists(path)) //如果不存在就创建file文件夹 { Directory.CreateDirectory(path); } using (FileStream stream = File.Open(path + fileName, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] by = Encoding.Default.GetBytes(content); if (appendToLast) { stream.Position = stream.Length; } else { stream.SetLength(0); } stream.Write(by, 0, by.Length); } } #region 直接删除指定目录下的所有文件及文件夹(保留目录) /// /// 删除指定目录下的所有文件及文件夹(保留目录) /// /// 文件目录 public static void DeleteDirectory(string file) { try { //判断文件夹是否还存在 if (Directory.Exists(file)) { DirectoryInfo fileInfo = new DirectoryInfo(file) { //去除文件夹的只读属性 Attributes = FileAttributes.Normal & FileAttributes.Directory }; foreach (string f in Directory.GetFileSystemEntries(file)) { if (File.Exists(f)) { //去除文件的只读属性 File.SetAttributes(file, FileAttributes.Normal); //如果有子文件删除文件 File.Delete(f); } else { //循环递归删除子文件夹 DeleteDirectory(f); } } //删除空文件夹 Directory.Delete(file); } } catch (Exception ex) // 异常处理 { Log4NetHelper.Error("代码生成异常", ex); } } #endregion 直接删除指定目录下的所有文件及文件夹(保留目录) } }