Have you checked whether ZipInputStream.ReadDataDescriptor is running correctly?
Bingo!
Here is the code that was running...
using (ZipInputStream zipInput = new ZipInputStream(fileStream))
{
ZipEntry entry = zipInput.GetNextEntry();
while ((null != entry) && !foundFile)
{
if (entry.IsFile && Regex.Match(entry.Name, "^" + containedFile + "$", RegexOptions.IgnoreCase).Success)
{
binaryFile = new byte[zipInput.Length]; //Exception occurs here
zipInput.Read(binaryFile, 0, binaryFile.Length);
entryName = entry.Name;
foundFile = true;
}
}
zipInput.Close();
}
Stepping through the code, the ReadDataDescriptor method was not called until the next GetNextEntry method was called. This filled out the missing details of the current entry member in the ZipInputStream class - and then sets it to null in readiness for the GetNextEntry processing. No use for what I'm trying to do.
The solution to my problem is to replace the above code with the following:
using (FileStream fileStream = new FileStream(targetFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (ZipFile zf = new ZipFile(fileStream))
{
ZipEntry entry = zf.GetEntry(containedFile);
if (entry != null)
{
using (Stream s = zf.GetInputStream(entry))
{
binaryFile = new byte[entry.Size];
s.Read(binaryFile, 0, binaryFile.Length);
}
}
}
fileStream.Close();
}
This ensures that the entry variable is fully populated when I come to use its members and works for both file formats (those with and without data descriptor sections.)
Thanks David, for pointing me in the right direction. Much appreciated.