public void OpenNew() // this stuff can generate a malformed .unity3d file that oblitarates your hard disk { var filesPath = GetFiles(Path.GetDirectoryName(FilePath)); var files = new WebArchiveFile[filesPath.Length]; var headerSize = 5; //int32 //read files for (int i = 0; i < filesPath.Length; i++) { files[i] = new WebArchiveFile(); files[i].Name = Path.GetFileName(filesPath[i]); files[i].Bytes = File.ReadAllBytes(filesPath[i]); files[i].Size = files[i].Bytes.Length; headerSize += files[i].Name.Length + 9; } files[0].Offset = headerSize; for (int i = 1; i < files.Length; i++) { files[i].Offset = files[i - 1].Offset + files[i - 1].Size; } var uncompressedSize = headerSize; for (int i = 0; i < files.Length; i++) { uncompressedSize += files[i].Bytes.Length; } Files = files; UncompressedSize = uncompressedSize; Opened = true; }
public void Open() { var file = File.ReadAllBytes(FilePath); var fileReader = new FileReader(file); //read compressed header var signature = fileReader.ReadString(); var buildVersion = fileReader.ReadInt32(); var webPlayerVersion = fileReader.ReadString(); var unityEngineVersion = fileReader.ReadString(); var compressedFileSize = fileReader.ReadInt32(); var compressedHeaderSize = fileReader.ReadInt32(); fileReader.SkipBytes(8); var compressedBodySize = fileReader.ReadInt32(); var uncompressedSize = fileReader.ReadInt32(); var compressedFileSize2 = fileReader.ReadInt32(); var uncompressedHeaderSize = fileReader.ReadInt32(); if (fileReader.ReadByte() != 0x00) //end of header { throw new InvalidDataException(string.Format("Expected 0x00 at: {0}.", fileReader.Position.ToString("X2"))); } fileReader.Position = compressedHeaderSize; //decompress compressed body var fileBody = fileReader.ReadByteArray(compressedBodySize); var decompressedFile = LzmaUtils.Decompress(fileBody); //read decompressed header fileReader = new FileReader(decompressedFile); var files = new WebArchiveFile[fileReader.ReadInt32()]; for (int i = 0; i < files.Length; i++) { files[i] = new WebArchiveFile(); files[i].Name = fileReader.ReadString(); files[i].Offset = fileReader.ReadInt32(); files[i].Size = fileReader.ReadInt32(); } //read decompressed body for (int i = 0; i < files.Length; i++) { fileReader.Position = files[i].Offset; files[i].Bytes = fileReader.ReadByteArray(files[i].Size); } Files = files; CompressedSize = compressedFileSize; UncompressedSize = uncompressedSize; Opened = true; }