Delphi FMX (Android) - 如何在不提取的情况下读取 zip 流中的文本文件?
Delphi FMX (Android) - How read text file inside zip stream without extract?
是否可以在不解压缩的情况下从 zip 文件中读取文件?我想从备忘录行中的压缩文件(作为 android 资产)读取文本文件。
ZipFile := TZipFile.Create; //Zipfile: TZipFile
try
ZipFile.Open('C:\Path\to\file.zip', zmRead);
for I := 0 to ZipFile.FileCount - 1 do
begin
if ZipFile.FileNames[I]='A1.txt' then //S: string
//My problem is here ? How load A1.txt to memo lines?
Memo1.Lines.Add(S);
end;
ZipFile.Close;
finally
ZipFile.Free;
end;
TZipFile
有 public Read()
方法可以让你获得一个 TBytes
完整的解压文件,或者一个 TStream
用于阅读动态解压缩字节。您可以根据需要使用这些字节写入 TMemo
。
例如,使用TStream
,您可以从文件中读取字节到本地缓冲区,直到遇到换行符,然后将缓冲区添加到TMemo
并清除缓冲区, 重复直到到达 TStream
.
的末尾
请注意,无论哪种情况,您都将访问文本文件的 raw 字节,而 TMemo
需要 Unicode 字符串,因此您必须转换根据文本文件的实际编码将字节转换为 Unicode,例如 SysUtils.TEncoding
。例如,如果文本文件是 UTF-8 编码,则使用 TEncoding.UTF8
。 TEncoding
有 GetString()
方法将 TBytes
数据转换为 UnicodeString
。
是否可以在不解压缩的情况下从 zip 文件中读取文件?我想从备忘录行中的压缩文件(作为 android 资产)读取文本文件。
ZipFile := TZipFile.Create; //Zipfile: TZipFile
try
ZipFile.Open('C:\Path\to\file.zip', zmRead);
for I := 0 to ZipFile.FileCount - 1 do
begin
if ZipFile.FileNames[I]='A1.txt' then //S: string
//My problem is here ? How load A1.txt to memo lines?
Memo1.Lines.Add(S);
end;
ZipFile.Close;
finally
ZipFile.Free;
end;
TZipFile
有 public Read()
方法可以让你获得一个 TBytes
完整的解压文件,或者一个 TStream
用于阅读动态解压缩字节。您可以根据需要使用这些字节写入 TMemo
。
例如,使用TStream
,您可以从文件中读取字节到本地缓冲区,直到遇到换行符,然后将缓冲区添加到TMemo
并清除缓冲区, 重复直到到达 TStream
.
请注意,无论哪种情况,您都将访问文本文件的 raw 字节,而 TMemo
需要 Unicode 字符串,因此您必须转换根据文本文件的实际编码将字节转换为 Unicode,例如 SysUtils.TEncoding
。例如,如果文本文件是 UTF-8 编码,则使用 TEncoding.UTF8
。 TEncoding
有 GetString()
方法将 TBytes
数据转换为 UnicodeString
。