我有一个代码块,如下所示:
using (MemoryStream outputZip = new MemoryStream())
{
using (ZipOutputStream zipstream = new ZipOutputStream(outputZip))
{
zipstream.Password = req.password;
zipstream.SetLevel(3);
foreach (var doc in loadocs)
{
ZipEntry entry = new ZipEntry(doc.FileName);
zipstream.PutNextEntry(entry);
zipstream.Write(doc.FileData, 0, doc.FileData.Length);
}
zipstream.CloseEntry();
zipstream.IsStreamOwner = false;
}
response.result = new Document()
{
name = doc1.Custodian + DateTime.Now.Date.ToString().Replace('/', '_').Replace(':', '_') + ".zip",
content = outputZip.ToArray()
};
}
我一直回来
CWE 404 - 资源关闭或释放不当
指向第一行。使用声明是否应该在完成后处理对象?无论我做什么,我似乎无法缓解这个错误。我试图在try/catch/finally
中包装所有内容并在最终但同样的错误结果中处理outputZip
,指向outputZip
的声明。
我已将代码更新为:
MemoryStream zipOutput = new MemoryStream();
try
{
ZipOutputStream zipstream = new ZipOutputStream(zipOutput);
try
{
}
catch (Exception ex)
{
}
finally
{
zipstream = null;
zipstream.Close();
}
}
catch(Exception ex)
{
}
finally
{
zipOutput = null;
zipOutput.Close();
}
我仍然看到MemoryStream上的Errorper资源关闭或释放。虽然非常多余,但这段代码实际上不会释放资源吗?