Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

pascalscript - Read bytes from file at desired position with Inno Setup

I want to open a 50 MB binary file and read only the last 4 bytes and convert it to string for some purpose.

The only way I found to do it now is using LoadStringFromFile, to load the file entirely on the memory then copy that last 4 bytes, however this method is very slow because the binary file is heavy.

Is there any better way to do it in Inno Setup script?

Update :


This is a final working function that I edited from Martin Prikryl's answer

function readlast4byte() : AnsiString;
var
  Stream: TFileStream;
  Buffer: string;
  Count: Integer;
  Index: Integer;
begin
  Count := 4;

  Stream := TFileStream.Create('C:est.txt', fmOpenRead);

  try
    Stream.Seek(-Count, soFromEnd);

    SetLength(Buffer, 1);
    SetLength(Result, Count);
    for Index := 1 to Count do
    begin
      Stream.ReadBuffer(Buffer, 1);
      Result[Index] := Chr(Ord(Buffer[1]))  ;
    end;
  finally
    Stream.Free;
  end;
end;

Update 2 :

Also this is another great working function that written by TLama and should mark as answer too:

[Code]
#IFNDEF Unicode
  #DEFINE CharSize 1
#ELSE
  #DEFINE CharSize 2
#ENDIF

type
  TSeekOrigin = (
    soBeginning,
    soCurrent,
    soEnd
  );

#IFDEF UNICODE
function BufferToAnsi(const Buffer: string): AnsiString;
var
  W: Word;
  I: Integer;
begin
  SetLength(Result, Length(Buffer) * 2);
  for I := 1 to Length(Buffer) do
  begin
    W := Ord(Buffer[I]);
    Result[(I * 2)] := Chr(W shr 8); // high byte
    Result[(I * 2) - 1] := Chr(Byte(W)); // low byte
  end;
end;
#ENDIF

function ReadStringFromFile(const FileName: string; Origin: TSeekOrigin; Offset, Length: Integer;
  var S: AnsiString): Boolean;
var
  Buffer: string;
  Stream: TFileStream;
begin
  Result := True;
  try
    Stream := TFileStream.Create(FileName, fmOpenRead);
    try
      Stream.Seek(Offset, Ord(Origin));
      SetLength(Buffer, Length div {#CharSize});
      Stream.ReadBuffer(Buffer, Length);
      #IFNDEF UNICODE
        S := Buffer;
      #ELSE
        S := BufferToAnsi(Buffer);
      #ENDIF
    finally
      Stream.Free;
    end;
  except
    Result := False;
  end;

    end;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use TFileStream support class:

TStream = class(TObject)
  function Read(Buffer: String; Count: Longint): Longint;
  function Write(Buffer: String; Count: Longint): Longint;
  function Seek(Offset: Longint; Origin: Word): Longint;
  procedure ReadBuffer(Buffer: String; Count: Longint);
  procedure WriteBuffer(Buffer: String; Count: Longint);
  function CopyFrom(Source: TStream; Count: Longint): Longint;
  property Position: Longint; read write;
  property Size: Longint; read write;
end;

THandleStream = class(TStream)
  constructor Create(AHandle: Integer);
  property Handle: Integer; read;
end;

TFileStream = class(THandleStream)
  constructor Create(Filename: String; Mode: Word);
end;

Use the .Seek(-4, soFromEnd) property to seek the read pointer to the desired position.

A complication is that the TStream works with characters not bytes, so you have to convert Unicode strings that your read back to bytes.

When reading just four bytes, it's way easier to read byte by byte, preventing any multibyte conversions:

procedure ReadFileEnd;
var
  Stream: TFileStream;
  Buffer: string;
  Count: Integer;
  Index: Integer;
begin
  Count := 4;

  Stream := TFileStream.Create('my_binary_file.dat', fmOpenRead);

  try
    Stream.Seek(-Count, soFromEnd);

    SetLength(Buffer, 1);
    for Index := 1 to Count do
    begin
      Stream.ReadBuffer(Buffer, 1);
      Log(Format('Byte %2.2x: %2.2x', [Index, Ord(Buffer[1])]));
    end;
  finally
    Stream.Free;
  end;
end;

Here's a generic alternative from by @TLama that efficiently works for arbitrary large read:
https://pastebin.com/nzGEdXVj


By the way, for me LoadStringFromFile function seems to be efficient enough to load 50 MB file. It takes only 40 ms.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...