Amazon Glacier: Resume an Archive Download with Verified Chunks
See more Amazon Glacier Examples
For existing Amazon Glacier vault customers. The standalone vault service is closed to new customers. These examples use its REST API version 2012-06-01, not the S3 bucket APIs used by S3 Glacier storage classes. See AWS service guidance.
Downloads a completed full-archive retrieval job in fixed 8 MiB ranges. Set the job ID, cache directory, and final path. Start with an empty, private cache directory on the same filesystem as the final output, then rerun with exactly the same settings to resume. Only one process may use the directory. No new retrieval job is initiated, even if the old job expires.
A saved identity binds progress to region, vault, job, archive ID, size, tree hash, chunk size, and final path. Every cached chunk is rehashed and its length and range checked before reuse. New chunks require HTTP 206, exact Content-Range and Content-Length, actual length, and a matching response tree hash before their temporary file is committed. An interrupted chunk is downloaded again; already verified chunks are retained. A damaged checkpoint or cached chunk stops with an explanation. Local cache files and receipts must be protected from other writers.
After downloading, the example assembles one chunk at a time and checks the entire file's byte count and archive tree hash before renaming it. Interrupted assembly is rebuilt from the cache. Existing final output is accepted only if it matches; other files are not overwritten. Chunks remain for inspection or manual cleanup. Allow disk space for both the complete chunk cache and assembled archive, plus one temporary chunk. A disk-full, network, or checksum failure is not success.
Uses decimal strings and bounded 32-bit arithmetic for archive offsets through the 40 TiB archive limit, including beyond 2 GiB. This produces up to 5,242,880 chunk files plus receipts at that limit; larger power-of-two chunks are more practical for very large archives, but require adapting the arithmetic and checkpoint schema. Empty archives use the single-download example. For partial retrieval jobs use range output download instead.
Requires glacier:DescribeJob and glacier:GetJobOutput. Resume promptly while output is retained (at least 24 hours after successful completion). If it has expired, initiate a new retrieval deliberately and use a new cache directory; cached chunks are not automatically transferred to a different job.
Uses AWS Signature Version 4 with service glacier. Set the region once; the example builds the commercial AWS endpoint. Other partitions require the appropriate endpoint suffix. /-/ selects the signing account; change the vault name and IDs to your own resources. Load private qa_data/aws/glacier.json with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This sample does not discover profiles or refresh credentials. TLS certificate verification is enabled explicitly.
Checks transport completion, the operation-specific HTTP status, and parsed data separately. Inspect full diagnostics privately. Renew expired credentials; use bounded backoff with jitter for retryable reads. Uploads and job initiation can succeed before a connection fails, so do not blindly resubmit them. Configure IAM permissions for only the required operations and vault resources.
Chilkat Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
' Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
' Private configuration, not a file in public web content or source control.
' Load current IAM credentials authorized for the Glacier operation.
set credentials = Server.CreateObject("Chilkat.JsonObject")
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/glacier.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load private AWS credentials.") & "</pre>"
Response.End
End If
If ((credentials.JsonTypeOf("access_key") <> 1) Or (credentials.StringOf("access_key") = "")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid access_key.") & "</pre>"
Response.End
End If
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (credentials.StringOf("secret_key") = "")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid secret_key.") & "</pre>"
Response.End
End If
' Set this once to the region containing the vault.
region = "us-west-2"
set endpoint = Server.CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("glacier.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
set aws = Server.CreateObject("Chilkat.AuthAws")
aws.AccessKey = credentials.StringOf("access_key")
aws.SecretKey = credentials.StringOf("secret_key")
aws.Region = region
aws.ServiceName = "glacier"
aws.SignatureVersion = 4
set rest = Server.CreateObject("Chilkat.Rest")
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
success = rest.SetAuthAws(aws)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not configure AWS signing.") & "</pre>"
Response.End
End If
' Temporary IAM/STS credentials require their session token on every signed request.
' Include the token in the signed query. Keep request URLs and diagnostics private.
' Leave session_token absent or empty only for credentials that do not have one.
If (credentials.HasMember("session_token") = 1) Then
If (credentials.JsonTypeOf("session_token") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "session_token must be a string.") & "</pre>"
Response.End
End If
If (credentials.StringOf("session_token") <> "") Then
success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set the session-token parameter.") & "</pre>"
Response.End
End If
End If
End If
' Supply a Socket to explicitly enable TLS certificate verification.
set connection = Server.CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "TLS connection failed; inspect diagnostics privately.") & "</pre>"
Response.End
End If
success = rest.UseConnection(connection,1)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not attach the TLS connection.") & "</pre>"
Response.End
End If
success = rest.AddHeader("x-amz-glacier-version","2012-06-01")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set x-amz-glacier-version.") & "</pre>"
Response.End
End If
' A hyphen selects the account belonging to the signing credentials.
' Use the original, URL-safe vault name and IDs returned by Glacier.
success = rest.AddPathParam("{vault}","chilkat")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set vault name.") & "</pre>"
Response.End
End If
set result = Server.CreateObject("Chilkat.JsonObject")
result.Strict = 1
set error = Server.CreateObject("Chilkat.JsonObject")
error.Strict = 1
set responseBody = Server.CreateObject("Chilkat.StringBuilder")
jobId = "YOUR_JOB_ID"
success = rest.AddPathParam("{job}",jobId)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set job ID.") & "</pre>"
Response.End
End If
success = rest.FullRequestNoBodySb("GET","/-/vaults/{vault}/jobs/{job}",responseBody)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Request did not complete; inspect diagnostics privately. Check state before retrying a change.") & "</pre>"
Response.End
End If
If (rest.ResponseStatusCode <> 200) Then
Response.Write "<pre>" & Server.HTMLEncode( "Glacier HTTP status: " & rest.ResponseStatusCode) & "</pre>"
success = error.LoadSb(responseBody)
If (success = 1) Then
If (error.JsonTypeOf("code") = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "AWS error code: " & error.StringOf("code")) & "</pre>"
End If
End If
Response.Write "<pre>" & Server.HTMLEncode( "AWS request ID: " & rest.ResponseHdrByName("x-amzn-RequestId")) & "</pre>"
' Inspect full errors privately. An HTTP error is not a successful operation.
Response.End
End If
success = result.LoadSb(responseBody)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected valid JSON response.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("Completed") <> 5) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid Completed.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("StatusCode") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid StatusCode.") & "</pre>"
Response.End
End If
If (result.StringOf("StatusCode") = "") Then
Response.Write "<pre>" & Server.HTMLEncode( "Empty StatusCode.") & "</pre>"
Response.End
End If
If ((result.BoolOf("Completed") <> 1) Or (result.StringOf("StatusCode") <> "Succeeded")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Job is not successfully complete. Resume later only if InProgress.") & "</pre>"
Response.End
End If
If (result.StringOf("Action") <> "ArchiveRetrieval") Then
Response.Write "<pre>" & Server.HTMLEncode( "Wrong job type.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("SHA256TreeHash") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid SHA256TreeHash.") & "</pre>"
Response.End
End If
If (result.StringOf("SHA256TreeHash") = "") Then
Response.Write "<pre>" & Server.HTMLEncode( "Empty SHA256TreeHash.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("ArchiveSizeInBytes") <> 2) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid ArchiveSizeInBytes.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("ArchiveId") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid ArchiveId.") & "</pre>"
Response.End
End If
If (result.StringOf("ArchiveId") = "") Then
Response.Write "<pre>" & Server.HTMLEncode( "Empty ArchiveId.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("ArchiveSHA256TreeHash") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid ArchiveSHA256TreeHash.") & "</pre>"
Response.End
End If
If (result.StringOf("ArchiveSHA256TreeHash") = "") Then
Response.Write "<pre>" & Server.HTMLEncode( "Empty ArchiveSHA256TreeHash.") & "</pre>"
Response.End
End If
expectedSize = result.StringOf("ArchiveSizeInBytes")
expectedHash = result.StringOf("ArchiveSHA256TreeHash")
If (result.StringOf("SHA256TreeHash") <> expectedHash) Then
Response.Write "<pre>" & Server.HTMLEncode( "Use a full-archive retrieval job for this resumable example.") & "</pre>"
Response.End
End If
' Fixed 8 MiB chunks are power-of-two and tree-hash aligned.
' Decimal long division avoids 32-bit overflow in every generated language.
chunkSize = 8388608
chunkCount = 0
remainder = 0
position = 0
set sizeText = Server.CreateObject("Chilkat.StringBuilder")
set decimalDigits = Server.CreateObject("Chilkat.StringBuilder")
set digit = Server.CreateObject("Chilkat.StringBuilder")
success = sizeText.Append(expectedSize)
success = decimalDigits.Append("0123456789")
If ((sizeText.Length = 0) Or (sizeText.Length > 14)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid archive size.") & "</pre>"
Response.End
End If
Do While (position < sizeText.Length)
success = digit.SetString(sizeText.GetRange(position,1,0))
If (decimalDigits.Contains(digit.GetAsString(),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid decimal archive size.") & "</pre>"
Response.End
End If
remainder = remainder * 10 + digit.IntValue
' At most nine subtractions per digit; avoids language-specific division.
chunkCount = chunkCount * 10
Do While (remainder >= chunkSize)
chunkCount = chunkCount + 1
remainder = remainder - chunkSize
Loop
position = position + 1
Loop
If ((chunkCount > 5242880) Or ((chunkCount = 5242880) And (remainder <> 0))) Then
Response.Write "<pre>" & Server.HTMLEncode( "Archive exceeds the 40 TiB service limit.") & "</pre>"
Response.End
End If
If ((chunkCount = 0) And (remainder = 0)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Use the single-download example for an empty archive.") & "</pre>"
Response.End
End If
If (remainder <> 0) Then
chunkCount = chunkCount + 1
End If
' This dedicated directory belongs to ONE job. Use a new empty directory
' when selecting another job. Run only one process against it at a time.
cacheDir = "qa_output/glacier-download"
finalPath = "qa_output/archive-resumed.dat"
set files = Server.CreateObject("Chilkat.FileAccess")
success = files.DirEnsureExists(cacheDir)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not create private download directory.") & "</pre>"
Response.End
End If
set path = Server.CreateObject("Chilkat.StringBuilder")
success = path.Append(cacheDir)
success = path.Append("/manifest.json")
manifestPath = path.GetAsString()
set manifest = Server.CreateObject("Chilkat.JsonObject")
manifest.Strict = 1
set identity = Server.CreateObject("Chilkat.JsonObject")
success = identity.UpdateString("version","1")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set version.") & "</pre>"
Response.End
End If
success = identity.UpdateString("region",region)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set region.") & "</pre>"
Response.End
End If
success = identity.UpdateString("vault","chilkat")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set vault.") & "</pre>"
Response.End
End If
success = identity.UpdateString("jobId",jobId)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set jobId.") & "</pre>"
Response.End
End If
success = identity.UpdateString("archiveId",result.StringOf("ArchiveId"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set archiveId.") & "</pre>"
Response.End
End If
success = identity.UpdateString("size",expectedSize)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set size.") & "</pre>"
Response.End
End If
success = identity.UpdateString("treeHash",expectedHash)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set treeHash.") & "</pre>"
Response.End
End If
success = identity.UpdateString("chunkSize","8388608")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set chunkSize.") & "</pre>"
Response.End
End If
success = identity.UpdateString("finalPath",finalPath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set finalPath.") & "</pre>"
Response.End
End If
If (files.FileExists(manifestPath) = 1) Then
success = manifest.LoadFile(manifestPath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid checkpoint. Preserve the cache and inspect it before restarting.") & "</pre>"
Response.End
End If
If (manifest.StringOf("version") <> identity.StringOf("version")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Checkpoint belongs to different download settings. Use a new empty cache directory.") & "</pre>"
Response.End
End If
If (manifest.StringOf("region") <> identity.StringOf("region")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Checkpoint belongs to different download settings. Use a new empty cache directory.") & "</pre>"
Response.End
End If
If (manifest.StringOf("vault") <> identity.StringOf("vault")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Checkpoint belongs to different download settings. Use a new empty cache directory.") & "</pre>"
Response.End
End If
If (manifest.StringOf("jobId") <> identity.StringOf("jobId")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Checkpoint belongs to different download settings. Use a new empty cache directory.") & "</pre>"
Response.End
End If
If (manifest.StringOf("archiveId") <> identity.StringOf("archiveId")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Checkpoint belongs to different download settings. Use a new empty cache directory.") & "</pre>"
Response.End
End If
If (manifest.StringOf("size") <> identity.StringOf("size")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Checkpoint belongs to different download settings. Use a new empty cache directory.") & "</pre>"
Response.End
End If
If (manifest.StringOf("treeHash") <> identity.StringOf("treeHash")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Checkpoint belongs to different download settings. Use a new empty cache directory.") & "</pre>"
Response.End
End If
If (manifest.StringOf("chunkSize") <> identity.StringOf("chunkSize")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Checkpoint belongs to different download settings. Use a new empty cache directory.") & "</pre>"
Response.End
End If
If (manifest.StringOf("finalPath") <> identity.StringOf("finalPath")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Checkpoint belongs to different download settings. Use a new empty cache directory.") & "</pre>"
Response.End
End If
Else
set cacheContents = Server.CreateObject("Chilkat.DirTree")
cacheContents.BaseDir = cacheDir
cacheContents.Recurse = 0
success = cacheContents.BeginIterate()
If ((success = 1) Or (cacheContents.DoneIterating <> 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "First run requires an empty, readable cache directory.") & "</pre>"
Response.End
End If
success = identity.WriteFile(manifestPath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not save download identity.") & "</pre>"
Response.End
End If
End If
set crypt = Server.CreateObject("Chilkat.Crypt2")
crypt.HashAlgorithm = "sha256-tree-hash"
crypt.EncodingMode = "hexlower"
If (files.FileExists(finalPath) = 1) Then
finalSize = files.FileSizeStr(finalPath)
finalHash = crypt.HashFileENC(finalPath)
If ((crypt.LastMethodSuccess <> 1) Or (finalSize <> expectedSize) Or (finalHash <> expectedHash)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Existing final file differs; choose another final output path.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Archive already downloaded and verified.") & "</pre>"
Response.End
End If
set rangeHeader = Server.CreateObject("Chilkat.StringBuilder")
set contentRange = Server.CreateObject("Chilkat.StringBuilder")
set byteCount = Server.CreateObject("Chilkat.StringBuilder")
set receipt = Server.CreateObject("Chilkat.JsonObject")
receipt.Strict = 1
chunkIndex = 0
byteHigh = 0
byteLow = 0
set range = Server.CreateObject("Chilkat.StringBuilder")
set digits = Server.CreateObject("Chilkat.StringBuilder")
Do While (chunkIndex < chunkCount)
chunkBytes = chunkSize
If ((chunkIndex = chunkCount - 1) And (remainder <> 0)) Then
chunkBytes = remainder
End If
range.Clear
digits.Clear
success = digits.AppendInt(byteLow)
If (byteHigh > 0) Then
Do While (digits.Length < 9)
success = digits.Prepend("0")
Loop
success = range.AppendInt(byteHigh)
End If
success = range.Append(digits.GetAsString())
success = range.Append("-")
endHigh = byteHigh
endLow = byteLow + chunkBytes - 1
If (endLow >= 1000000000) Then
endLow = endLow - 1000000000
endHigh = endHigh + 1
End If
digits.Clear
success = digits.AppendInt(endLow)
If (endHigh > 0) Then
Do While (digits.Length < 9)
success = digits.Prepend("0")
Loop
success = range.AppendInt(endHigh)
End If
success = range.Append(digits.GetAsString())
rangeKey = range.GetAsString()
success = rangeHeader.SetString("bytes=")
success = rangeHeader.Append(rangeKey)
success = contentRange.SetString("bytes ")
success = contentRange.Append(rangeKey)
success = contentRange.Append("/")
success = contentRange.Append(expectedSize)
byteCount.Clear
success = byteCount.AppendInt(chunkBytes)
success = path.SetString(cacheDir)
success = path.Append("/chunk-")
success = path.AppendInt(chunkIndex)
chunkPath = path.GetAsString()
success = path.Append(".json")
receiptPath = path.GetAsString()
success = path.SetString(chunkPath)
success = path.Append(".part")
temporaryPath = path.GetAsString()
If (files.FileExists(chunkPath) = 1) Then
success = receipt.LoadFile(receiptPath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Cached chunk lacks a valid receipt. Preserve the cache and inspect it.") & "</pre>"
Response.End
End If
cachedHash = crypt.HashFileENC(chunkPath)
If ((crypt.LastMethodSuccess <> 1) Or (cachedHash <> receipt.StringOf("treeHash")) Or (receipt.StringOf("range") <> rangeKey) Or (files.FileSizeStr(chunkPath) <> byteCount.GetAsString())) Then
Response.Write "<pre>" & Server.HTMLEncode( "Cached chunk failed verification. Remove only that chunk and its receipt, then rerun.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Reusing verified chunk: " & chunkIndex) & "</pre>"
Else
success = rest.AddHeader("Range",rangeHeader.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set Range.") & "</pre>"
Response.End
End If
success = rest.SendReqNoBody("GET","/-/vaults/{vault}/jobs/{job}/output")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not send range request. Rerun to resume.") & "</pre>"
Response.End
End If
httpStatus = rest.ReadResponseHeader()
If (httpStatus <> 206) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected HTTP 206 for range download. Job may have expired; no new retrieval was started.") & "</pre>"
Response.End
End If
responseRange = rest.ResponseHdrByName("Content-Range")
If ((rest.LastMethodSuccess <> 1) Or (responseRange <> contentRange.GetAsString())) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected Content-Range; refusing download.") & "</pre>"
Response.End
End If
responseLength = rest.ResponseHdrByName("Content-Length")
If ((rest.LastMethodSuccess <> 1) Or (responseLength <> byteCount.GetAsString())) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected Content-Length; refusing download.") & "</pre>"
Response.End
End If
responseHash = rest.ResponseHdrByName("x-amz-sha256-tree-hash")
If ((rest.LastMethodSuccess <> 1) Or (responseHash = "")) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing range tree hash. Require a tree-hash-aligned retrieval and download range.") & "</pre>"
Response.End
End If
set output = Server.CreateObject("Chilkat.Stream")
output.SinkFile = temporaryPath
success = rest.ReadRespBodyStream(output,1)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Download interrupted; rerun to resume. Temporary bytes are not trusted.") & "</pre>"
Response.End
End If
actualSize = files.FileSizeStr(temporaryPath)
If ((files.LastMethodSuccess <> 1) Or (actualSize <> byteCount.GetAsString())) Then
Response.Write "<pre>" & Server.HTMLEncode( "Downloaded size mismatch.") & "</pre>"
Response.End
End If
actualHash = crypt.HashFileENC(temporaryPath)
If ((crypt.LastMethodSuccess <> 1) Or (actualHash <> responseHash)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Downloaded tree hash mismatch.") & "</pre>"
Response.End
End If
success = receipt.UpdateString("range",rangeKey)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set range.") & "</pre>"
Response.End
End If
success = receipt.UpdateString("treeHash",actualHash)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set treeHash.") & "</pre>"
Response.End
End If
success = receipt.WriteFile(receiptPath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not save chunk checksum; temporary bytes remain uncommitted.") & "</pre>"
Response.End
End If
success = files.FileRename(temporaryPath,chunkPath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not commit verified chunk.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Downloaded verified chunk: " & chunkIndex) & "</pre>"
End If
byteHigh = endHigh
byteLow = endLow + 1
If (byteLow = 1000000000) Then
byteLow = 0
byteHigh = byteHigh + 1
End If
chunkIndex = chunkIndex + 1
Loop
' Full-archive size plus hash protects against a partial retrieval job too.
' Assemble from verified chunks; interrupted assembly is rebuilt on rerun.
success = path.SetString(cacheDir)
success = path.Append("/assembled.part")
assembledPath = path.GetAsString()
set assembled = Server.CreateObject("Chilkat.FileAccess")
success = assembled.OpenForWrite(assembledPath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not open assembly file.") & "</pre>"
Response.End
End If
set data = Server.CreateObject("Chilkat.BinData")
chunkIndex = 0
Do While (chunkIndex < chunkCount)
success = path.SetString(cacheDir)
success = path.Append("/chunk-")
success = path.AppendInt(chunkIndex)
success = data.Clear()
success = data.LoadFile(path.GetAsString())
If (success = 1) Then
success = assembled.FileWriteBd(data,0,0)
End If
If (success <> 1) Then
assembled.FileClose
Response.Write "<pre>" & Server.HTMLEncode( "Assembly failed. Verified chunks are retained for resume.") & "</pre>"
Response.End
End If
chunkIndex = chunkIndex + 1
Loop
assembled.FileClose
assembledSize = files.FileSizeStr(assembledPath)
assembledHash = crypt.HashFileENC(assembledPath)
If ((crypt.LastMethodSuccess <> 1) Or (assembledSize <> expectedSize) Or (assembledHash <> expectedHash)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Assembled archive failed verification; final output was not created.") & "</pre>"
Response.End
End If
success = files.FileRename(assembledPath,finalPath)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Verified assembly could not be renamed; keep the cache.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Archive downloaded and verified. Retained chunks can now be removed manually.") & "</pre>"
%>
</body>
</html>