【发布时间】:2018-06-04 12:11:30
【问题描述】:
我在 Azure 中有一个进程,它生成大量 pdf 报告文件并将它们存储在 blob 存储中。我没有单独发送所有这些链接,而是生成一个 zip 文件并将此链接发送给用户。
这个过程全部在一个进程中完成,并且一直运行良好。最近,我在将文件添加到 zip 存档时遇到 OutOfMemory 异常错误,我正在努力寻找解决方案。
下面是我用来创建 zip 文件的代码(注意:使用 SharpLibZip 库)。目前,它在添加大约 45 个每个文件(PDF)约 3.5Mb 的文件后失败并出现 OutOfMemoryException。当我打线时发生故障:zipStream.PutNextEntry(newEntry)。
有谁知道我可以如何改进这个过程?在这个级别上失败似乎很小的 zip 文件。
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
【问题讨论】:
-
只是一个观察,但您遇到的错误似乎与 Azure 无关,而与 zip 库有关。还是我读错了?您是否正在寻找一种无需使用该 zip 库即可获得类似结果的方法?
-
我认为您可能是对的@KWilson - 这是关于在不接触磁盘的情况下从大量文件创建一个 zip 存档。然后将结果持久化到 blob 存储。我不喜欢任何特定的 zip 库,所以如果您有其他建议,我会全力以赴 :)
标签: c# azure zip azure-storage sharpziplib