【发布时间】:2016-10-17 01:47:08
【问题描述】:
我有一个包含 zip 文件的 URL。这些文件需要从 URL 中解压缩。使用 webclient 打开和读取 URL,然后将其添加到 Stream。然后在 ZipArchive 对象中使用它,该对象将解压缩文件并将它们存储在 D:\ 驱动器中。当文件大约 400Mb 时,我会收到 'System.OutOfMemoryException'。
必须使用 Stream,因为 webClient.OpenRead(Uri Address) 返回一个 Stream。以及使用 ZipArchive(Stream stream)。
我怎样才能停止收到此消息?
string zipFileUrl = "https://www.dropbox.com/s/clersbjdcshpdy6/oversize_zip_test_0.zip?dl=0"
string output_path = @"D:\";
using (WebClient webClient = new WebClient())
{
using (Stream streamFile = webClient.OpenRead(zipFileUrl))
{
using (ZipArchive archive = new ZipArchive(streamFile))//ERROR HERE
{
var entries = archive.Entries;
//Loops thru each file in Zip and adds it to directory
foreach (var entry in entries)
{
if (entry.FullName != "/" && entry.Name != "")
{
string completeFileName = Path.Combine(output_path, entry.FullName);
string directory = Path.GetDirectoryName(completeFileName);
//If directory does not exist then we create it.
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
//Extracts zip from URL to extract path, and overwrites if file exists.
entry.ExtractToFile(completeFileName, true);
}
}
}
}
【问题讨论】:
-
我很惊讶您在
OpenRead上收到错误,打开流进行读取不应该占用那么多内存。 -
什么是 StackTrace?
-
虽然我在下一行得到了
End of Central Directory record could not be found,但在我的最后没有得到这样的错误。 -
@ScottChamberlain 我在使用时遇到错误 (ZipArchive archive = new ZipArchive(streamFile)
-
这就是我的想法,请参阅我为解决该问题提供的答案。
标签: c# asp.net-mvc stream