2017-11-22 4 views
0

GZipStream을 사용하여 메모리에서 파일 압축을 풀고 MemoryStream에 압축 해제 된 데이터를 복사 한 다음 Unity 3D에서 BinaryReader를 사용하여 MemoryStream을 읽으려고합니다. 그러나, 나는 그것을 실행하려고하면 이러한 오류가 발생합니다 :EndOfStreamException : 스트림 끝을 읽지 못했습니다. (Unity 3d)

EndOfStreamException : 스트림의 끝을 읽지 못했습니다. (System.IO.BinaryReader.ReadInt32) (/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:432) LoadUsers.LoadNifti (System.String fullPath, 부울 loadFromResources) (Assets/scripts/(자산/스크립트/opengl_main.cs : 656) OpenEngine.LoadFileUsingPath() .Component : SendMessage (String, Object) SimpleFileBrowser.Scripts.GracesGames.FileBrowser : SendCallbackMessage (String) (Assets/Resources/SimpleFileBrowser/Scripts/GracesGames/FileBrowser.cs : 274) 01 23,516,SimpleFileBrowser.Scripts.GracesGames.FileBrowser : SelectFile() (자산/자원/SimpleFileBrowser/스크립트/GracesGames/FileBrowser.cs에서 : 267) UnityEngine.EventSystems.EventSystem : 업데이트

사람이 어떤 문제를 알고() 뭐야? 메모리에있는 파일의 압축을 풀어서 binaryReader로 전송할 수있는 또 다른 방법이 있습니까? 감사

코드 :

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 
} 

답변

2

당신이 스트림에 쓸 때마다, 그 Position가 증가한다.

쓰기 후 stream.Position = 0으로 설정하면 이후에 첫 번째 바이트에서 다시 읽기 시작합니다.

+0

작동합니다! 감사 –