使用Inno Setup从所需位置读取文件中的字节

时间:2015-07-05 07:28:42

标签: inno-setup pascalscript

我想打开一个50 MB的二进制文件,只读取最后4个字节并将其转换为字符串用于某种目的。

我现在发现的唯一方法就是使用LoadStringFromFile,将文件完全加载到内存中,然后复制最后4个字节,但是这个方法非常慢,因为二进制文件很重。

在Inno Setup脚本中有没有更好的方法呢?

更新:

这是我从Martin Prikryl的回答

编辑的最终工作函数
function readlast4byte() : AnsiString;
var
  Stream: TFileStream;
  Buffer: string;
  Count: Integer;
  Index: Integer;
begin
  Count := 4;

  Stream := TFileStream.Create('C:\test.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;

更新2:

这也是由TLama编写的另一个伟大的工作功能,也应该标记为答案:

[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;

1 个答案:

答案 0 :(得分:3)

您可以使用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;

使用.Seek(-4, soFromEnd)属性查找指向所需位置的读指针。

复杂的是TStream使用字符而不是字节,因此您必须将读取的Unicode字符串转换为字节。

当只读取四个字节时,它更容易逐字节读取,防止任何多字节转换:

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;

这里是来自@TLama的通用替代品,可有效地用于任意大读取:
https://pastebin.com/nzGEdXVj

顺便说一句,对我来说,LoadStringFromFile function似乎足以加载50 MB文件。它只需要40毫秒。