program Resume; uses Windows, WinInet; function _itoa(Value: Integer; lpBuffer: PChar; Radix: Integer): PChar; cdecl external 'ntdll'; var hInet, hConnect, hRequest: Pointer; hFile, Fail, Remainder, uBytes, dwReserved, Size: Cardinal; Buffer: array [1..16384] of Cardinal; // 64 Kb. (server is crazy?) begin Dec(Remainder); Size := SizeOf(Remainder); hInet := InternetOpen('agent', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); hConnect := InternetConnect(hInet, 'ufpr.dl.sourceforge.net', INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0); hFile := CreateFile('Portable_Ubuntu.exe', GENERIC_WRITE, 0, nil, OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, 0); // will be closed with the process Fail := 0; repeat Sleep(Fail * 3000); hRequest := HttpOpenRequest(hConnect, nil, 'sourceforge/portableubuntu/Portable_Ubuntu.exe', nil, nil, nil, INTERNET_FLAG_KEEP_CONNECTION or INTERNET_FLAG_DONT_CACHE, 0); lstrcat(_itoa(SetFilePointer(hFile, 0, nil, FILE_END), @lstrcpy(@Buffer, 'range: bytes=')[13], 10), '-'); HttpAddRequestHeaders(hRequest, @Buffer, lstrlen(@Buffer), HTTP_ADDREQ_FLAG_ADD_IF_NEW); if HttpSendRequest(hRequest, nil, 0, nil, 0) then begin HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @Remainder, Size, dwReserved); while (Remainder > 0) and InternetQueryDataAvailable(hRequest, Size, 0, 0) and (Size > 0) do begin InternetReadFile(hRequest, @Buffer, Size, uBytes); Dec(Remainder, _lwrite(hFile, @Buffer, uBytes)); Fail := 0; end; end; Inc(Fail); InternetCloseHandle(hRequest); until (Remainder = 0) or (Fail = 3); InternetCloseHandle(hConnect); InternetCloseHandle(hInet); if Remainder <> 0 then MessageBox(0, 'Download incomplete!'#13'Try again later.', nil, MB_ICONEXCLAMATION); end.
Lo comparto sin ningun maquillaje para que le extraigan la idea, pues es la semilla para crear un acelerador de descargas por multiples secciones.
Enjoy