2012-07-27 61 views
2

當我使用SharpZipLib來解壓zip文件,該文件在windows phone 7上有5000個文件。完成它需要5分多鐘。 這裏是代碼:如何加快windows phone 7上的解壓縮操作?

using (StreamReader httpwebStreamReader = new StreamReader(ea.Result)) 
      { 
       //open isolated storage to save files 
       using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        using (ZipInputStream s = new ZipInputStream(httpwebStreamReader.BaseStream)) 
        { 
         //s.Password = "123456";//if archive is encrypted 
         ZipEntry theEntry; 
         while ((theEntry = s.GetNextEntry()) != null) 
         { 
          string directoryName = Path.GetDirectoryName(theEntry.Name); 
          string fileName = Path.GetFileName(theEntry.Name); 

          // create directory 
          if (directoryName.Length > 0) 
          { 
           isoStore.CreateDirectory(directoryName); 
          } 

          if (fileName != String.Empty) 
          { 
           //save file to isolated storage 
           using (BinaryWriter streamWriter = 
             new BinaryWriter(new IsolatedStorageFileStream(theEntry.Name, 
              FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, isoStore))) 
           { 

            int size = 2048; 
            byte[] data = new byte[2048]; 
            while (true) 
            { 
             size = s.Read(data, 0, data.Length); 
             if (size > 0) 
             { 
              streamWriter.Write(data, 0, size); 
             } 
             else 
             { 
              break; 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } 

爲什麼這麼慢? 如何加速解壓縮操作? 有誰知道?

+0

多線程是否可以在此工作? – wing 2012-07-27 09:20:35

+0

這是在模擬器還是在設備上?這兩者之間的時間如何? – 2012-07-27 13:03:18

+0

我的諾基亞800上的時間超過5分鐘,但在模擬器中只花了20秒! – wing 2012-07-30 03:45:45

回答

0

我認爲你需要增加你的緩衝區大小。更改線路

int size = 2048; 
byte[] data = new byte[2048]; 

並更改2048喜歡的東西32768(32 * 1024)。

2KB塊大小正在對閃存進行大量單獨寫入。根據我的經驗,這是一件有點慢的事情,可能因設備而異。 32KB的塊大小應該少16倍,但我不知道是否會導致16倍的加速。我有興趣聽回來。

+0

謝謝您的回答。我將尺寸更改爲32768,需要3分鐘。我認爲這個zip文件有很多小文件,例如2KB,3KB。這是否意味着什麼? – wing 2012-08-01 03:44:04

+0

即使它是zip文件中的一個大文件,您使用的2KB的小緩衝區大小也會減慢速度。接下來要嘗試的是在循環中讀取數據,但不要將其寫入磁盤。然後,您可以查看瓶頸是處理zip文件還是將文件寫入磁盤。我的猜測是把文件寫入磁盤是一個緩慢的部分,在這種情況下,zip中有許多文件會讓你更慢。讓我知道如何進行測試。 – 2012-08-01 12:19:59

+0

謝謝你的建議。我將文件流更改爲內存流。但它似乎不起作用...所以我將解壓縮操作更改爲另一個線程,似乎很好,但有時主線程會凍結...我如何處理解壓縮線程?@。@ – wing 2012-08-03 03:20:35