2013-03-20 48 views
3

我已經在Windows Phone應用程序中使用後臺傳輸服務下載了一個大型zip文件。解壓縮Windows手機中的大文件導致OutOfMemory異常

當我試圖使用解壓縮文件中的以下tutorial

private void LoadZipfile() 
{ 
    WebClient c = new WebClient(); 
    c.OpenReadCompleted += new OpenReadCompletedEventHandler(openReadCompleted); 
    c.OpenReadAsync(new Uri("http://www.mydomain.com/myZipFile.zip")); 
} 

private void openReadCompleted(object sender, OpenReadCompletedEventArgs e) 
{ 
    UnZipper unzip = new UnZipper(e.Result); 
    foreach (string filename in unzip.FileNamesInZip()) 
    { 
     Stream stream = unzip.GetFileStream(filename); 
     StreamReader reader = new StreamReader(stream); 
     string contents = reader.ReadToEnd(); 
     MessageBox.Show(contents); 
    } 
} 

不過,雖然unziping,它拋出內存不足例外。

請建議我如何解決這個問題

+3

該文件有多大? – 2013-03-20 13:20:05

+0

在手機上,這很容易成爲實際的硬件限制。我會建議嘗試解壓縮一些文件的大小增加,以查看是否存在導致內存異常發生的一點。 – Amicable 2013-03-20 13:23:28

回答

5

我認爲這裏的問題是這兩條線

string contents = reader.ReadToEnd(); 
     MessageBox.Show(contents); 

這兩行嘗試讀取一個字符串整個文件的變量,並嘗試展示它。這在手機上不是一個好習慣(即使在支持大容量內存和頁面文件的桌面上也不行)。

嘗試從流中讀取幾個字節並將其放入字符串中並將其顯示在消息框中。這應該可以解決你的問題。

+1

我在下面的行中出現錯誤流stream = unzip.GetFileStream(filename); – 2013-03-21 10:25:01

相關問題