/// <summary> /// 2.获取第一块区域(资源包信息区域)的字节大小 /// </summary> /// <returns> 资源包信息区域的字节大小 </returns> private static int GetPackageInfoRegionSize() { //计算资源包类型名字符串所占字节 int packageTypeNameSize = MyConverter.String2Bytes(PackType).Length; //资源包版本(uint) + 资源包类型名字节大小(ushort) + 资源包类型名(UTF8) + 资源包大小(uint) int packageRegionSize = 4 + 2 + packageTypeNameSize + 4; return(packageRegionSize); }
/// <summary> /// 3.获取第二块区域(文件信息集合区域)的字节大小 /// </summary> /// <returns>文件信息集合区域的字节大小</returns> private static int GetFileInfosRegionSize() { //文件信息集合所占字节大小(int) + 文件信息集合里的文件信息个数(int) int size = 4 + 4; //单个文件信息的组合 //2.获取到信息块的字节长度 byte[] infoDatas = null; for (int n = 0; n < resInfoList.Count; n++) { byte[] strDatas = MyConverter.String2Bytes(resInfoList [n].fileName); ushort strLength = (ushort)strDatas.Length; AddToEnd(ref infoDatas, MyConverter.Ushort2Bytes(strLength)); //文件名字节大小(ushort) AddToEnd(ref infoDatas, strDatas); //文件名(UTF8) AddToEnd(ref infoDatas, MyConverter.Uint2Bytes(resInfoList [n].startPos)); //文件起始位置(uint) AddToEnd(ref infoDatas, MyConverter.Int2Bytes(resInfoList [n].size)); //文件大小(int) } size += infoDatas.Length; infoDatas = null; return(size); }
/// <summary> /// 写入所有数据到资源包文件中 /// </summary> /// <param name="packagePath">打包完成后资源包的路径</param> /// <param name="filePaths">要打包的文件路径集合</param> /// <param name="frontRegionsSize">第一块区域与第二块区域所占的字节大小和</param> private static void WriteAllDatas(string packagePath, string[] filePaths, ref int frontRegionsSize) { BinaryWriter totalWriter = null; FileStream totalStream = null; //初始化FileStream文件流 //totalStream = new FileStream(desFilePath, FileMode.Append); totalStream = new FileStream(packagePath, FileMode.Create); //以FileStream文件流来初始化BinaryWriter书写器,此用以合并分割的文件 totalWriter = new BinaryWriter(totalStream); //1.资源包信息区域 totalWriter.Write(MyConverter.Int2Bytes(PackVersion)); //1.1资源包版本(int) byte[] resTypeNameData = MyConverter.String2Bytes(PackType); ushort resTypeNameSize = (ushort)resTypeNameData.Length; totalWriter.Write(MyConverter.Ushort2Bytes(resTypeNameSize)); //1.2资源包类型名字节大小(ushort) totalWriter.Write(resTypeNameData); //1.3资源包类型名(UTF8) //获取第一块区域(资源包信息区域)的字节大小 int packageInfoRegionSize = GetPackageInfoRegionSize(); //获取第二块区域(文件信息集合区域)的字节大小 int fileInfosRegionSize = GetFileInfosRegionSize(); //第一块及第二区域所占字节和 frontRegionsSize = packageInfoRegionSize + fileInfosRegionSize; //获取文件在资源包中的位置信息及打包后整个资源包的字节大小 uint totalSize = GetFilePositionInfosAndTotalSize(frontRegionsSize); totalWriter.Write(MyConverter.Uint2Bytes(totalSize)); //1.4资源包大小(uint) //2.文件信息集合区域 totalWriter.Write(MyConverter.Int2Bytes(fileInfosRegionSize)); //2.1文件信息集合所占字节大小(int) totalWriter.Write(MyConverter.Int2Bytes(resInfoList.Count)); //2.2文件信息集合里的文件信息个数(int) byte [] infoDatas = null; for (int n = 0; n < resInfoList.Count; n++) //2.3各文件信息组合 { byte[] strDatas = MyConverter.String2Bytes(resInfoList [n].fileName); ushort strLength = (ushort)strDatas.Length; AddToEnd(ref infoDatas, MyConverter.Ushort2Bytes(strLength)); //2.3.1文件名字节长度 AddToEnd(ref infoDatas, strDatas); //2.3.2文件名 AddToEnd(ref infoDatas, MyConverter.Uint2Bytes(resInfoList [n].startPos)); //2.3.3文件起始位置 AddToEnd(ref infoDatas, MyConverter.Int2Bytes(resInfoList [n].size)); //2.3.4文件大小 } totalWriter.Write(infoDatas); //3.文件数据集合 FileStream tempStream = null; BinaryReader tempReader = null; for (int i = 0; i < resInfoList.Count; i++) { //1.写入文件头部间隙 if (resInfoList[i].beforeSpace > 0) { WriteEmptyBytes(ref totalWriter, resInfoList [i].beforeSpace); } //2.写入文件内容 //以小文件所对应的文件名称和打开模式来初始化FileStream文件流,起读取分割作用 tempStream = new FileStream(filePaths [i], FileMode.Open); tempReader = new BinaryReader(tempStream); int tempFileLength = (int)tempStream.Length; if (0 == tempFileLength) { Debug.LogError("错误:" + filePaths[i] + "这个文件内容为空"); } else { //读取分割文件中的数据,并生成合并后文件 totalWriter.Write(tempReader.ReadBytes(tempFileLength)); } //关闭BinaryReader文件阅读器 tempReader.Close(); //关闭FileStream文件流 tempStream.Close(); } //关闭BinaryWriter文件书写器 totalWriter.Close(); //关闭FileStream文件流 totalStream.Close(); }