Download a specific file from a ZIP using TLMDWebHTTPGet
From the most recent version of LMD WebPack on the TLMDWebHTTPGet control is able to resume file downloads from a specified position. 2 new properties were added: RangeStart and RangeEnd. Thus a programmer can setwith RangeStart position from which downloading should be started, whereby RangeEnd specifies end position. Another property is Resume: If set to true, the component will search in TLMDWebHTTPGet.DownloadDir for a file named with TLMDWebHTTPGet.DestinationName and fills RangeStart property automatically with the found file size.
So how can a programmer use described features? Possibilities are endless:
- Download a filelist and get a specified file from zip file only
- Multi-threaded file downloads
- Allow end-users to pause/resume downloads of large files
Here is simple code to implement first example (downloading a specified file from zip):
LMDWebHTTPGet1.URL := 'http://www.lmd.de/downloads/lmd2010vcl/setupse10d14.zip';
// "end of central directory" size.
// Minus shows that we read it from end of file
LMDWebHTTPGet1.RangeEnd := -17;
// Downloads last 17 bytes of file
if LMDWebHTTPGet1.Process(False, False) then
begin
LEndOfCentralRecord.LoadFromStream(LMDWebHTTPGet1.Data);
// Fills RangeStart by "Central Directory" structure begin offset
LMDWebHTTPGet1.RangeStart := LEndOfCentralRecord.OffsetOfCentralDirectory;
LMDWebHTTPGet1.RangeEnd := LMDWeHTTPGet1.RangeStart + LEndOfCentralRecord.SizeOfCentralDirectory;
// Downloads "Central Directory" structure
if LMDWebHTTPGet1.Process(False, False) then
begin
LCentralDirectory.LoadFromStream(LMDWebHTTPGet1.Data);
// Fills RangeStart by offset of compressed file in ZIP
LMDWebHTTPGet1.RangeStart := LCentralDirectory.Files['readme.txt'].FileOffset;
// Compressed size
LMDWebHTTPGet1.RangeEnd := LMDWebHTTPGet1.RangeStart + LCentralDirectory.Files['readme.txt'].CompressedSize;
// Downloads compressed file "readme.txt"
if LMDWebHTTPGet1.Process(False, False) then
begin
// Processing of compressed file
end;
end;
end;
LEndOfCentralRecord, LCentralDirectory are placeholders that schematically shows corresponding zip format structures – “Central directory structure” and “End of central directory record”. The full specification is described here.
Besides that WebPack includes a demo project in demos\lmdweb\LMDWebHTTPGetResume folder.
