I have a process in Azure that generates a large number of pdf report files and stores them in blob storage. Rather than send links to all these individually, I'm generating a zip file and sending this link to users.
This process is all done in a single process, and has been working fine. Lately, I've been getting OutOfMemory exception errors when adding files to the zip archive and I'm struggling to find a solution.
Below is the code I use to create the zip file (note: using the SharpLibZip library). Currently, it fails with an OutOfMemoryException after adding about 45 files of about 3.5Mb per file (PDF). The failure occurs when I hit the line: zipStream.PutNextEntry(newEntry).
Does anyone know how I could improve this process? It seems to small a zip file to fail at this level.
Using outputMemStream As New MemoryStream()
Using zipStream As New ICSharpCode.SharpZipLib.Zip.ZipOutputStream(outputMemStream)
zipStream.SetLevel(7)
Dim collD3 As UserSurveyReportCollection = GetFileList(RequestID)
For Each entityD2 As UserSurveyReport In collD3
Try
Dim strF As String = entityD2.FileLocation
'Download blob as memorystream and add this stream to the zip file
Dim msR As New MemoryStream
msR = objA.DownloadBlobAsMemoryStream(azureAccount, ReportFolder, entityD2.FileName)
msR.Seek(0, SeekOrigin.Begin)
'Determine file name used in zip file archive for item
Dim strZipFileName As String = DetermineZipSourceName(entityD2, strFolder, strFileName)
'Add MemoryStream to ZipFile Stream
Dim newEntry As ICSharpCode.SharpZipLib.Zip.ZipEntry = New ICSharpCode.SharpZipLib.Zip.ZipEntry(strZipFileName)
newEntry.DateTime = DateTime.Now
zipStream.PutNextEntry(newEntry)
msR.CopyTo(zipStream)
zipStream.CloseEntry()
msR = Nothing
zipStream.Flush()
intCounter += 1
End If
Catch exZip As Exception
End Try
Next
zipStream.IsStreamOwner = False
zipStream.Finish()
zipStream.Close()
outputMemStream.Position = 0
Dim bytes As Byte() = outputMemStream.ToArray()
result.Comment = objA.UploadBlob(bytes, azureAccount, ReportFolder, entityReport.FileName).AbsolutePath
End Using
End Using