gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:13:08 24 4
gpt4 key购买 nike

我想打开一个 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;

最佳答案

您可以使用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 毫秒。

关于inno-setup - 使用 Inno Setup 从文件中读取所需位置的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31228103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com