Is it possible to read a file from the zip file without extract it? I want to read a text file from a compressed file(as an android assets) in a Memo lines.
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
has public Read()
methods that allow you to obtain either a TBytes
of the complete decompressed file, or a TStream
for reading the decompressed bytes dynamically. You can use those bytes to write to the TMemo
as needed.
For instance, using a TStream
, you can read bytes from the file into a local buffer until you encounter a line break, and then add the buffer to the TMemo
and clear the buffer, repeating until you reach the end of the TStream
.
Note that, in either case, you would be accessing the raw bytes of the text file, whereas TMemo
expects Unicode strings, so you would have to convert the bytes to Unicode, such as with SysUtils.TEncoding
, based on the actually encoding of the text file. For instance, using TEncoding.UTF8
if the text file is UTF-8 encoded. TEncoding
has GetString()
methods for converting TBytes
data to UnicodeString
.