// public bool Load(String filename) { Console.WriteLine("ResourcePack::Load - {0}", filename); using (FileStream fs = File.Open(filename, FileMode.Open)) { byte[] unpackBuffer = null; using (BinaryReader br = new BinaryReader(fs)) { UInt32 sizeUncompressed = ByteUtils.SwapUInt32(br.ReadUInt32()); unpackBuffer = new byte[sizeUncompressed]; byte[] packedBuf = new byte[br.BaseStream.Length - 4]; br.Read(packedBuf, 0, packedBuf.Length); Console.WriteLine(" ... uncompressed size is {0}", sizeUncompressed); using (MemoryStream ms = new MemoryStream(packedBuf)) { using (ZInputStream zStream = new ZInputStream(ms)) { Int32 bOffset = 0; while (bOffset < sizeUncompressed) { Int32 decS = zStream.read(unpackBuffer, bOffset, (Int32)sizeUncompressed - bOffset); bOffset += decS; } } } } fs.Close(); using (MemoryStream ms = new MemoryStream(unpackBuffer)) { using (BinaryReader ir = new BinaryReader(ms)) { UInt32 numChunks = ByteUtils.SwapUInt32(ir.ReadUInt32()); Console.WriteLine(" ... processing {0} chunks", numChunks); for (UInt32 i = 0; i < numChunks; i++) { String componentTypeName = ByteUtils.readNullTermString11(ir); UID componentUID = new UID(ir.ReadBytes(6)); String componentName = ByteUtils.readNullTermString11(ir); UInt32 componentDataLength = ByteUtils.SwapUInt32(ir.ReadUInt32()); // go see if we know how to build a component from this data type Type typeToBuild = null; if (mComponentFactory.TryGetValue(componentTypeName, out typeToBuild)) { // yes; so instantiate the returned type SiDComponent newComponent = Activator.CreateInstance(typeToBuild) as SiDComponent; newComponent.Name = componentName; // ask it to load from the bytestream newComponent.LoadFromByteStream(ir, (Int32)componentDataLength); // record it if (!Add(newComponent)) { Console.WriteLine("Warning! duplicate item in single resource pack? {0}", componentUID); } Console.WriteLine(" + {0}:{1}: - {2}", componentUID, SiDComponent.getResourceTypeName(typeToBuild), newComponent.Name); } else { // we don't know how to construct this type; store as a 'black box' blob Blob newBlob = new Blob(componentTypeName); newBlob.Name = componentName; // blobs just hold onto the data as raw bytes, ready to parrot it back out on Save newBlob.LoadFromByteStream(ir, (Int32)componentDataLength); // record it if (!Add(newBlob)) { Console.WriteLine("Warning! duplicate (blob) item in single resource pack? {0}", componentUID); } Console.WriteLine(" + {0}:{1}:BLOB: - {2}", componentUID, componentTypeName, componentName); } } } } } return true; }
/// <summary> /// 解压 /// </summary> /// <param name="param"></param> /// <returns></returns> static public void DecompressByteZipNet(byte[] inBytes, uint startPos, uint inLen, ref byte[] outBytes, ref uint outLen) { MemoryStream outStream = new MemoryStream(); MemoryStream outms = new MemoryStream(); outms.Write(inBytes, (int)startPos, (int)inLen); outms.Position = 0; ZInputStream outzipStream = new ZInputStream(outms); byte[] writeData = new byte[1024]; try { int size = 0; while ((size = outzipStream.read(writeData, 0, writeData.Length)) > 0) { if (size > 0) { outStream.Write(writeData, 0, size); } else { Ctx.m_instance.m_logSys.log("ZipNet Decompress Error"); } } outzipStream.Close(); // 一定要先 Close ZipOutputStream ,然后再获取 ToArray ,如果不关闭, ToArray 将不能返回正确的值 outBytes = outStream.ToArray(); outLen = (uint)outBytes.Length; outStream.Close(); outms.Close(); } catch { Ctx.m_instance.m_logSys.log("DecompressByteZipNet error"); } }