azure storage upload zip file, then download it again and it gets corrupted

1.6k Views Asked by At

I created a zip file using WinZip. I can open this using both WinZip and Windows Explorer.

I then upload this file to Azure Storage, and download it again.

I can open the downloaded file in Windows Explorer, but WinZip says it is corrupted.

I am using Windows 8.1 and the latest version of Winzip. This happens in both the development and live environment. What is wrong here?

UPDATE 14/01/2014 Here is the code I use

Private Sub UploadDocumentToAzure(filename As String, _
                                  ByRef stream As Stream)
    Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("StorageConnectionString"))
    Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient
    Dim container As CloudBlobContainer = blobClient.GetContainerReference("cont")

    container.CreateIfNotExists()

    Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(filename)
    blockBlob.UploadFromStream(stream)
End Sub

Public Sub DownloadDocumentFromAzure(documentName As String, ByRef response As HttpResponse)

    Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("StorageConnectionString"))
    Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient
    Dim container As CloudBlobContainer = blobClient.GetContainerReference("cont")

    Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(documentName)

    Dim memStream As New MemoryStream
    blockBlob.DownloadToStream(memStream)

    response.ContentType = blockBlob.Properties.ContentType
    response.AddHeader("Content-Disposition", "Attachment; filename=""" & blockBlob.Name.ToString() & """")

    response.AddHeader("Content-Length", (blockBlob.Properties.Length - 1).ToString())
    response.BinaryWrite(memStream.ToArray())
    response.End()
End Sub
1

There are 1 best solutions below

0
On

Please change the following line of code:

response.AddHeader("Content-Length", (blockBlob.Properties.Length - 1).ToString())

to

response.AddHeader("Content-Length", (blockBlob.Properties.Length).ToString())

Because you miss the last byte, your blob is not downloading completely.