首页 > 解决方案 > Delphi FMX (Android) - 如何在没有提取的情况下读取 zip 流中的文本文件?

问题描述

是否可以从 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;

标签: delphifiremonkeyzipfile

解决方案


TZipFileRead()具有允许您获取TBytes完整解压缩文件或TStream动态读取解压缩字节的公共方法。您可以根据需要使用这些字节写入TMemo

例如,使用 a TStream,您可以将文件中的字节读入本地缓冲区,直到遇到换行符,然后将缓冲区添加到TMemo并清除缓冲区,重复直到到达TStream.

请注意,在任何一种情况下,您都将访问文本文件的原始字节,而需要 Unicode 字符串,因此您必须根据文本文件的实际编码TMemo将字节转换为 Unicode,例如 with 。SysUtils.TEncoding例如,TEncoding.UTF8如果文本文件是 UTF-8 编码,则使用。 TEncoding具有GetString()TBytes数据转换为UnicodeString.


推荐阅读