在C#中提取Zip文件的例外情况

时间:2013-06-20 14:01:52

标签: c# microsoft-metro zip windows-store-apps extract

我正在研究提取代码以提取Zip文件,在winrt中使用C#。

我在这里从本地驱动器获取文件:

StorageFile file = await KnownFolders.PicturesLibrary.GetFileAsync("dostoyevsky-poor-folk.zip");
        Stream zipMemoryStream = await file.OpenStreamForReadAsync();
        var folder = ApplicationData.Current.LocalFolder;
        // Create zip archive to access compressed files in memory stream
        using (ZipArchive zipArchive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Read))
        {
            // For each compressed file...
            foreach (ZipArchiveEntry entry in zipArchive.Entries)
            {
                if (entry.Name == "")
                {
                    // Folder
                    await CreateRecursiveFolder(folder, entry);
                }
                else
                {
                    // File
                    await ExtractFile(folder, entry);
                }
            }
        }

我在这里提取文件夹:

private async Task CreateRecursiveFolder(StorageFolder folder, ZipArchiveEntry entry)
{
    var steps = entry.FullName.Split('/').ToList();

    steps.RemoveAt(steps.Count() - 1);

    foreach (var i in steps)
    {
        await folder.CreateFolderAsync(i, CreationCollisionOption.OpenIfExists);

        folder = await folder.GetFolderAsync(i);
    }
}

我在这里提取文件:

private async Task ExtractFile(StorageFolder folder, ZipArchiveEntry entry)
        {
        var steps = entry.FullName.Split('/').ToList();
        steps.RemoveAt(steps.Count() - 1);
        foreach (var i in steps)
        {
            folder = await folder.GetFolderAsync(i);
        }
        using (Stream fileData = entry.Open())
        {
            StorageFile outputFile = await folder.CreateFileAsync(entry.Name, CreationCollisionOption.ReplaceExisting);

            using (Stream outputFileStream = await outputFile.OpenStreamForWriteAsync())
            {
                await fileData.CopyToAsync(outputFileStream);
                await outputFileStream.FlushAsync();
            }
        }
    }

当我尝试使用它时,我得到以下异常:'System.NullReferenceException'。 异常获取行是await outputFileStream.FlushAsync();

的最后一行

有些时候,当我尝试从Local Drive中选择文件时,我会遇到相同的异常。 在获取异常之前,await outputFileStream.FlushAsync()的调试器值看起来像这样。

enter image description here

你能帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

最后,它为我工作。为什么因为我在提取时获得Null值因为我要提取文件的包。 我相信这是使用c#为Windows应用商店提取Zip文件的完美解决方案。 感谢