使用ZipArchive在Windows Store应用程序中提取.zip文件获得InvalidDataExceptio

本文关键字:文件 zip InvalidDataExceptio 提取 ZipArchive Windows Store 应用程序 使用 | 更新日期: 2023-09-27 17:49:31

我正在开发一个Windows Store应用程序的概念证明,该应用程序将打开一个。zip文件并将内容提取到应用程序特定的漫游文件夹。我研究了几个流行的库来提取内容,却发现这些库不支持Windows Store应用程序(至少现在还不支持)。所以,我决定用ZipArchive。我在一个按钮单击处理程序中有以下代码:

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);
                        }
                    }
                }
            }
        }
    }
}

然而,我在

上得到一个InvalidDataException
await deflateStream.CopyToAsync(decompressedStream, (int)entry.Length);

下面是异常的详细信息:

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: 

我试图导入的。zip文件是用Windows资源管理器的"发送到压缩(压缩)文件夹"选项创建的。我也试过在代码中创建一个。zip文件,使用ZipArchive,但我也得到了更多的异常代码。因为这不是我理想的用例,所以我不会包含我的代码来创建.zip,除非它对某人有用。

我希望有人可以看到我的方式的错误在上面的代码或提供一个链接到一个坚实的库与zip文件,最好是开源的工作。这个沮丧的开发人员将非常感谢任何帮助

使用ZipArchive在Windows Store应用程序中提取.zip文件获得InvalidDataExceptio

您不必解压entry.Open()的结果。已经解压。

你也可以使用ZipFileExtensions.ExtractToDirectory (http://msdn.microsoft.com/en-us/library/system.io.compression.zipfileextensions.extracttodirectory.aspx)来简化你的代码