2014-04-06 91 views
4

在我的應用程序中,我試圖一次下載大約180個小音頻文件。我嘗試了BackgroundTransferService,但對於如此多的小文件來說似乎並不穩定。所以,現在我正在下載所有這些音頻的ZIP文件,並希望將它們提取到「音頻」文件夾中。我想在這個線程的方法:如何在Windows Phone 8應用程序中的IsolatedStorage中解壓縮文件?

How to unzip files in Windows Phone 8

但我得到這個錯誤:在下面的代碼'System.IO.IOException' occurred in mscorlib.ni.dll...。我怎樣才能克服這個問題?

while (reader.ReadInt32() != 101010256) 
{ 
    reader.BaseStream.Seek(-5, SeekOrigin.Current); // this line causes error 
}... 

另外,我需要在哪裏放置這段代碼,以及我在哪裏給它的目標目錄?

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); 
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(@"audio.rar", FileMode.Open, FileAccess.ReadWrite)) 
{ 
    UnZipper unzip = new UnZipper(fileStream);        
    foreach (string filename in unzip.FileNamesInZip()) 
    { 
     string FileName = filename; 
    } 
} 
+0

始終包含你正在使用或您的問題將不會被大多數主題專家可以看到語言的標籤。 –

+0

對不起。這裏提出問題的新手。將來要記住:-)謝謝。 – hnabbasi

+0

爲什麼尋找落後直到你找到「魔法值」?您可能會遇到負面的查找位置,這是導致錯誤的原因。 –

回答

0

使用Silverlight SharpZipLib。將SharpZipLib.WindowsPhone7.dll添加到您的項目中(也適用於WP8 silverlight)。

private void Unzip() 
    { 
     using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      ZipEntry entry; 
      int size; 
      byte[] data = new byte[2048]; 

      using (ZipInputStream zip = new ZipInputStream(store.OpenFile("YourZipFile.zip", FileMode.Open))) 
      { 
       // retrieve each file/folder 
       while ((entry = zip.GetNextEntry()) != null) 
       { 
        if (!entry.IsFile) 
         continue; 

        // if file name is music/rock/new.mp3, we need only new.mp3 
        // also you must make sure file name doesn't have unsupported chars like /,\,etc. 
        int index = entry.Name.LastIndexOf('/'); 

        string name = entry.Name.Substring(index + 1); 

        // create file in isolated storage 
        using (var writer = store.OpenFile(name, FileMode.Create)) 
        { 
         while (true) 
         { 
          size = zip.Read(data, 0, data.Length); 
          if (size > 0) 
           writer.Write(data, 0, size); 
          else 
           break; 
         } 
        } 
       } 
      } 
     } 

    } 
0

有幾個第三方庫,以提取ZIP文件WP8像ZipLib你可以從@http://slsharpziplib.codeplex.com/ 然而DotNetZip父ZipLib和更穩定的下載。 這裏是一個示例代碼。不檢查它是否有效,但這是你如何去做。

 ZipFile zip = ZipFile.Read(ZipFileToUnzip); 

foreach (ZipEntry ent in zip) 
{ 
    ent.Extract(DirectoryWhereToUnizp, ExtractExistingFileAction.OverwriteSilently); 
} 
0

我剛剛解決了這個問題。你可以做的就是使用這種方法,你的文件將保存在你的zip文件中的適當文件夾結構中的獨立存儲中。您可以根據需要更改要存儲數據的位置。

我剛剛讀了一個sample.zip文件。從你的應用文件夾。

private async Task UnZipFile() 
    { 
     var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); 
     using (var fileStream = Application.GetResourceStream(new Uri("sample.zip", UriKind.Relative)).Stream) 
     { 
      var unzip = new UnZipper(fileStream); 
      foreach (string filename in unzip.FileNamesInZip) 
      { 
       if (!string.IsNullOrEmpty(filename)) 
       { 
        if (filename.Any(m => m.Equals('/'))) 
        { 
         myIsolatedStorage.CreateDirectory(filename.Substring(0, filename.LastIndexOfAny(new char[] { '/' }))); 
        } 

        //save file entry to storage 
        using (var streamWriter = 
         new StreamWriter(new IsolatedStorageFileStream(filename, 
          FileMode.Create, 
          FileAccess.ReadWrite, 
          myIsolatedStorage))) 
        { 
         streamWriter.Write(unzip.GetFileStream(filename)); 
        } 
       } 
      } 
     } 
    } 

歡呼:)

相關問題