I'm writing a program that is loading and extracting files from an archive. Currently with my code it is successfully extracting one of the archives I need, and with the problem archive, it reads and extracts 6 files before giving an exception.
The exception is because it is reading the wrong integer, instead it is reading an integer 12 bytes before the integer it is supposed to read.
Here is my code:
if (versionNumber.Equals("AULB"))
{
fileCount = binReader1.ReadInt32();
for (int i = 0; i < fileCount; i++)
{
int size = binReader1.ReadInt32();
String midValues = "" + binReader1.ReadInt64();
int nameLength = binReader1.ReadInt32();
String name =
System.Text.Encoding.Default.GetString(binReader1.ReadBytes(nameLength));
byte[] blueprint = binReader1.ReadBytes((size) - 12 - nameLength);
//Export blueprint for testing
MemoryStream blueprintStream = new MemoryStream(blueprint);
FileStream fileStream1 = new FileStream(
"C:\\Users\\Dan\\Desktop\\blueprints\\" + i + ".blueprint",
FileMode.Create, FileAccess.Write);
blueprintStream.WriteTo(fileStream1);
}
As a quick explanation, it is reading the 4 bytes to get the size of the file, 8 bytes of the header that is unused (but I have set to a local for potential validity checks because this value should always be the same), then 4 bytes to get the length of the string name, then reads that many bytes to get the name.
After that, it grabs the remaining data of the file, so I subtract the length of the name, string length, and the extra 8 bytes because it has already read them.
To illustrate what I'm trying to load, here is the binary data. I have highlighted in light blue the file size, in light green is the extra 8 bytes, in bright green is the string length, then in pink is the string.
I have also highlighted in read the byte that my program is trying to read thinking it is the file size.
I'm not quite sure what could cause this problem, as it reads the previous 6 files and another small archive with no issues.