问题
I am working on Extraction Code to Extract Zip file, Using C# in winrt.
I am getting File from Local Drive here:
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);
}
}
}
I am Extracting For folder here:
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);
}
}
I am Extracting For File Here:
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();
}
}
}
When I try to use this I get this exception: 'System.NullReferenceException' .
The Exception getting line is the Last line of await outputFileStream.FlushAsync();
Some times I am getting same exception when I try to pick file from Local Drive.
Before Getting Exception the Debugger value of await outputFileStream.FlushAsync()
looking like this.

Can you Help me out for this.
Thanks
回答1:
Finally, It is Worked for me. Why because I am getting Null value while extraction Because of the package where I am going to extract files. I am sure this is the perfect solution to Extract Zip file for windows store apps using c#. Thanks
来源:https://stackoverflow.com/questions/17215679/exception-on-extracting-the-zip-file-in-c-sharp