i'm trying to decompress a file in memory using GZipStream, copy the decompressed data to a MemoryStream, and then read the MemoryStream using BinaryReader (from Unity 3d). However, i get these errors when i try to run it:
EndOfStreamException: Failed to read past end of stream. System.IO.BinaryReader.FillBuffer (Int32 numBytes) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:119) System.IO.BinaryReader.ReadInt32 () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:432) LoadNii.LoadNifti (System.String fullPath, Boolean loadFromResources) (at Assets/scripts/LoadNii.cs:240) opengl_main.PrepareNewAnatomy (System.String fullPath, Boolean loadFromResources) (at Assets/scripts/opengl_main.cs:193) opengl_main.LoadFileUsingPath () (at Assets/scripts/opengl_main.cs:656) UnityEngine.Component:SendMessage(String, Object) SimpleFileBrowser.Scripts.GracesGames.FileBrowser:SendCallbackMessage(String) (at Assets/Resources/SimpleFileBrowser/Scripts/GracesGames/FileBrowser.cs:274) SimpleFileBrowser.Scripts.GracesGames.FileBrowser:SelectFile() (at Assets/Resources/SimpleFileBrowser/Scripts/GracesGames/FileBrowser.cs:267) UnityEngine.EventSystems.EventSystem:Update()
anyone know what the problem is? is there another way to unzip a file in memory, and transfer it to a binaryReader? thanks
code:
Stream stream = null;
if (loadFromResources == true)
{
TextAsset textAsset = Resources.Load(fullPath) as TextAsset;
Debug.Log(textAsset);
stream = new MemoryStream(textAsset.bytes);
}
else
{
FileInfo fi1 = new FileInfo(fullPath);
if (fi1.Extension.Equals(".gz"))
{
stream = new MemoryStream();
byte[] buffer = new byte[4096];
using (Stream inGzipStream = new GZipStream(File.Open(fullPath,FileMode.Open), CompressionMode.Decompress))
{
int bytesRead;
while ((bytesRead = inGzipStream.Read(buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, bytesRead);
}
}
}
else
stream = File.Open(fullPath, FileMode.Open);
}
using (BinaryReader reader = new BinaryReader(stream))
{
//header headerKey substruct:
headerKey.sizeof_hdr = reader.ReadInt32(); //ERROR
}