0

我正在研究提取代码以提取 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();

有时,当我尝试从本地驱动器中选择文件时,我会遇到同样的异常。在获取异常之前,Debugger 的值await outputFileStream.FlushAsync()看起来像这样。

在此处输入图像描述

你能帮我解决这个问题吗?

谢谢

4

1 回答 1

0

最后,它对我有用。为什么因为我在提取时得到 Null 值因为我要提取文件的包。我确信这是使用 c# 为 Windows 商店应用程序提取 Zip 文件的完美解决方案。谢谢

于 2013-06-25T06:11:28.553 回答