我正在開發一個Windows應用商店應用程序驗證概念,它將打開一個.zip文件並將內容解壓縮到特定於應用程序的漫遊文件夾。我研究了幾種流行的庫來提取內容,發現這些庫不支持Windows Store應用(至少現在還沒有)。所以,我決定和ZipArchive一起去。我有下面的代碼在按鈕單擊處理程序:在使用ZipArchive的Windows Store應用程序中獲取InvalidDataException提取.zip文件
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
var fileOpenPicker = new FileOpenPicker();
fileOpenPicker.FileTypeFilter.Add(".zip");
var openedFile = await fileOpenPicker.PickSingleFileAsync();
var booksFolder = await Windows.Storage.ApplicationData.Current.RoamingFolder.CreateFolderAsync("Stuff", CreationCollisionOption.OpenIfExists);
var folder = await booksFolder.CreateFolderAsync(openedFile.Name.Replace(".zip", string.Empty), CreationCollisionOption.ReplaceExisting);
using (var stream = await openedFile.OpenStreamForReadAsync())
{
using (var zip = new ZipArchive(stream, ZipArchiveMode.Read))
{
foreach (var entry in zip.Entries)
{
using (var entryStream = entry.Open())
{
var file = await folder.CreateFileAsync(entry.Name);
using (var decompressedStream = await file.OpenStreamForWriteAsync())
{
using (var deflateStream = new DeflateStream(entryStream, CompressionMode.Decompress))
{
await deflateStream.CopyToAsync(decompressedStream, (int)entry.Length);
}
}
}
}
}
}
}
但是,我上線
await deflateStream.CopyToAsync(decompressedStream, (int)entry.Length);
得到InvalidDataException
以下是異常的詳細信息:
System.IO.InvalidDataException was unhandled by user code
HResult=-2146233087
Message=Unknown block type. Stream might be corrupted.
Source=System
StackTrace:
at System.IO.Compression.DeflateStream.EndRead(IAsyncResult asyncResult)
at System.IO.Stream.<BeginEndReadAsync>b__e(Stream stream, IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncTrimPromise`1.Complete(TInstance thisRef, Func`3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.IO.Stream.<CopyToAsyncInternal>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at POC.MoviePlayer.GroupedItemsPage.<Button_Click_1>d__4.MoveNext() in c:\Users\Brent\Documents\Visual Studio 2012\Projects\POC.MoviePlayer\POC.MoviePlayer\GroupedItemsPage.xaml.cs:line 108
InnerException:
的。我嘗試導入的壓縮文件是使用Windows資源管理器的「發送到壓縮(壓縮)文件夾」選項創建的。我也嘗試使用ZipArchive在代碼中創建一個.zip文件,但我也得到了更多的例外代碼。既然這不是我理想的用例,我不會將我的代碼添加到.zip中,除非它對某人有用。
我希望有人能夠在上面的代碼中看到我的方式的錯誤,或提供一個鏈接到一個固定的庫來處理最好是開源的zip文件。這個沮喪的開發者會非常感謝任何幫助。
我發誓我沒有解壓就試過了,並且得到了其他異常,但是我在做這件事時一定弄錯了其他異常。我從上面的方法中取出了DeflateStream,它的功能就像一個魅力。 ExtractToDirectory聽起來也很棒,但它可以與ApplicationData.RoamingFolder和ApplicationData.LocalFolder一起使用嗎? –
這些文件夾應該位於文件系統的某個位置(不是100%舒適,因爲我沒有使用Win8的經驗),因此您可以使用'folder.Path'。 – Peter
看起來他們可能已經將ExtractToDirectory擴展方法故意保留在WinRT API之外(http://social.msdn.microsoft.com/Forums/en/winappswithcsharp/thread/7906fd37-3918-46c0-b698-920fa2cf0835) 。該線程已超過一年,但我沒有在對象瀏覽器中看到它。遊民。無論哪種方式,你解決了我的問題。謝謝! :) –