/// ------------------------------------------------------------------------------------ /// <summary> /// Unzip a byte array and return unpacked data /// </summary> /// <param name="data">Byte array containing data to be unzipped</param> /// <returns>unpacked data</returns> /// ------------------------------------------------------------------------------------ public static byte[] UnpackData(byte[] data) { if (data == null) return null; try { MemoryStream memStream = new MemoryStream(data); ZipInputStream zipStream = new ZipInputStream(memStream); zipStream.GetNextEntry(); MemoryStream streamWriter = new MemoryStream(); int size = 2048; byte[] dat = new byte[2048]; while (true) { size = zipStream.Read(dat, 0, dat.Length); if (size > 0) { streamWriter.Write(dat, 0, size); } else { break; } } streamWriter.Close(); zipStream.CloseEntry();
/// <summary> /// This deserializes an adpack from a file. /// </summary> /// <param name="s">The stream to read from.</param> public void Deserialize(Stream s) { ZipInputStream zis = new ZipInputStream(s); ZipEntry entry; Metadata = new SortedList<string, string>(); Files = new SortedList<string, AdPackEntry>(); SortedList<String, byte[]> otherFiles = new SortedList<string, byte[]>(); while ((entry = zis.GetNextEntry()) != null) { if (entry.IsCrypted) { //MessageBox.Show("This adpack is encrypted, so it cannot be displayed. Please do not encrypt adpack files."); throw new Exception("Adpack is encrypted!"); } switch (entry.Name.ToLowerInvariant()) { case "metadata.dat": byte[] meta = new byte[entry.Size]; zis.Read(meta, 0, (int)entry.Size); HandleMetadata(meta); break; case "filelist.dat": byte[] fl = new byte[entry.Size]; zis.Read(fl, 0, (int)entry.Size); HandleFileList(fl); break; default: byte[] d = new byte[entry.Size]; zis.Read(d, 0, (int)entry.Size); otherFiles[entry.Name] = d; break; } zis.CloseEntry(); } // check product id, because it's not just for bf2142 if (Metadata["product"].ToLowerInvariant() != Common.AppInfos[AppID].AppNameShort) { String correctApp = ""; if (Common.AppShortNames.ContainsKey(Metadata["product"].ToLowerInvariant())) { correctApp = Common.AppInfos[Common.AppShortNames[Metadata["product"].ToLowerInvariant()]].AppName; } else { correctApp = "Unsupported (" + Metadata["product"].ToLowerInvariant() + ")"; } throw new Exception("Adpack is not for " + Common.AppInfos[AppID].AppName + ", it is for " + correctApp + "."); } // iterate through the other files foreach (KeyValuePair<String, byte[]> file in otherFiles) { if (Files.ContainsKey(file.Key)) { // referenced data. Files[file.Key].SetDDSData(file.Value); } } List<String> RemovalList = new List<string>(); foreach (KeyValuePair<String, AdPackEntry> file in Files) { // check each has data attached. if (file.Value.DDSData == null) { //MessageBox.Show("Warning: Missing DDS image file '" + file.Key + "'. The adpack file may be corrupt or encrypted."); RemovalList.Add(file.Key); } } foreach (String item in RemovalList) { Files.Remove(item); } }