//************************************************************************ // https://github.com/yuzhengyang // author: yuzhengyang // date: 2017.6.10 - 2017.6.12 // desc: 文件打包工具 // Copyright (c) yuzhengyang. All rights reserved. //************************************************************************ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Resources; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using Y.Utils.DataUtils.Collections; namespace Y.Utils.IOUtils.FileUtils { /// /// 文件打包工具 /// public class FilePackageTool { private static string FileTypeDesc = "FilePackage [文件打包]"; private static int FileBuffer = 1024 * 1024; #region 类型单一,文件处理复杂,加载占用超大内存(这都是辣鸡) /// /// 批量打包任意对象到资源文件 /// /// 被打包对象的列表。键值对中键为其在资源文件中的唯一标示名。 /// 目标资源文件。默认参数为当前目录下的"MyRes.pck"文件。 /// 是否覆盖已存在的目标文件。默认=True public static void ResourcePackage(IDictionary objCollection, string targetFilePath, bool overwrite = true) { if (overwrite) File.Delete(targetFilePath); using (ResourceWriter rw = new ResourceWriter(targetFilePath)) { foreach (KeyValuePair pair in objCollection) //为了防传进来的资源名有数字开头,资源名都加了前缀_ rw.AddResource("_" + pair.Key, pair.Value); rw.Generate(); rw.Close(); } } /// /// 解包资源文件,返回所有资源及其资源名 /// /// 要解包的资源文件。默认为当前目录下的"MyRes.pck" /// 资源字典,键值为资源唯一标示名。若无资源返回空集合。 public static Dictionary ResourceUnpack(string targetFilePath) { Dictionary rtn = new Dictionary(); using (ResourceReader rr = new ResourceReader(targetFilePath)) { foreach (DictionaryEntry entry in rr) rtn.Add(((string)entry.Key).Substring(1), entry.Value); } return rtn; } /// /// 根据资源名在指定的资源文件中检索资源 /// /// 资源名 /// 要在其中检索的资源文件名,默认为"MyRes.pck" /// 资源名对应的资源 public static object ResourceSearch(string resName, string targetFilePath) { object rtn = null; using (ResourceReader rr = new ResourceReader(targetFilePath)) { foreach (DictionaryEntry entry in rr) if ((string)entry.Key == '_' + resName) { rtn = entry.Value; break; } } return rtn; } /// /// 将对象序列化 /// /// 文件(支持绝大多数数据类型) /// 要序列化的对象(如哈希表,数组等等) public static void FileSerialize(string FilePath, object obj) { if (File.Exists(FilePath)) { try { FileStream fs = new FileStream(FilePath, FileMode.Create); BinaryFormatter sl = new BinaryFormatter(); sl.Serialize(fs, obj); fs.Close(); } catch { //序列化存储失败! } } else { //您读取的文件对象不存在 } } /// /// 将文件反序列化 /// /// 文件路径(必须是经过当前序列化后的文件) /// 返回 null 表示序列反解失败或者目标文件不存在 public static object FileDeSerialize(string FilePath) { if (System.IO.File.Exists(FilePath)) { try { FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryFormatter sl = new BinaryFormatter(); object obg = sl.Deserialize(fs); fs.Close(); return obg; } catch { return null; } } else { return null; } } #endregion /// /// 打包 /// /// public static int Pack(string srcPath, string dstFile, bool overwrite = true) { DateTime beginTime = DateTime.Now; if (!Directory.Exists(srcPath)) return -11; //要打包的路径不存在 if (File.Exists(dstFile) && !overwrite) return -12;//打包后的目标文件已存在 List allfile = FileTool.GetAllFile(srcPath); if (ListTool.HasElements(allfile)) { using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create)) { allfile.ForEach(x => { using (FileStream fsRead = new FileStream(x, FileMode.Open)) { fsRead.Close(); } }); fsWrite.Close(); } } else { return -13;//要打包的路径中没有文件 } return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功 } /// /// 解包 /// /// public static int Unpack(string srcFile, string dstPath, bool overwrite = true) { DateTime beginTime = DateTime.Now; if (!File.Exists(srcFile)) return -11; //要解包的文件不存在 if (Directory.Exists(dstPath) && !overwrite) return -12;//要解包的目标文件夹已存在 return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功 } } }