簡體   English   中英

Delphi,FileStream,從第5個字符開始讀取

[英]Delphi, FileStream, read starting on 5th character

我有一個用zlib壓縮的tiff圖像,但是它們卡在文件開頭的4個字符標識符上。

我想開始在5位置讀取文件,跳過前4個字符,然后將其解壓縮。

我已經修改了以下代碼,但是當它到達“ LOutput.CopyFrom”行時,它告訴我DataError。

procedure TForm1.DecompressXE3 ;
var
  LInput, LOutput: TFileStream;
  LUnZip: TZDecompressionStream;
  FSize : int64 ;
begin
  { Create the Input, Output, and Decompressed streams. }
  LInput := TFileStream.Create(edtDecompressSrcFile.Text, fmOpenRead);

  FSize := LInput.Size ;

  LInput.Position := 5 ;

  LOutput := TFileStream.Create(ChangeFileExt(edtDecompressSrcFile.Text, '.tiff'), fmCreate);


  LUnZip := TZDecompressionStream.Create(LInput);


  { Decompress data. }
  LOutput.CopyFrom(LUnZip, FSize-4 );
//  LOutput.CopyFrom(LUnZip, 0 );

  { Free the streams. }
  LUnZip.Free;
  LInput.Free;
  LOutput.Free;


end;

位置是從零開始的,因此必須設置LInput.Position := 4; 跳過4個字節。

潛在的錯誤原因-TZDecompressionStream可能會使用整個輸入流,而忽略位置設置,因此多余的起始字節會破壞解壓縮過程。 在這種情況下,您最好將文件的有效部分復制到中間MemoryStream並將其提供為TZDecompressionStream.Create的輸入參數。

並請注意,在從解壓縮的流進行復制時,您正在使用FSize(壓縮文件的大小)。 你應該用

LOutput.CopyFrom(LUnZip, 0);
or
LOutput.CopyFrom(LUnZip, LUnZip.Size);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM