decompress a ZIP file on windows 8 C#

2019-04-19 10:29发布

I am building a metro style app for windows 8 and I have a zip file that I am downloading from a web service, and I want to extract it.

I have seen the sample for compression and decompression, but that takes a single file an compresses/decompresses it. I have a whole directory structure that I need to extract.

Here is what I have so far:

var appData = ApplicationData.Current;
var file = await appData.LocalFolder.GetItemAsync("thezip.zip") as StorageFile;
var decompressedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("tempFileName", CreationCollisionOption.GenerateUniqueName);
using (var decompressor = new Decompressor(await file.OpenSequentialReadAsync()))
using (var decompressedOutput = await decompressedFile.OpenAsync(FileAccessMode.ReadWrite))
{
    var bytesDecompressed = await RandomAccessStream.CopyAsync(decompressor, decompressedOutput);
}

But this is no good, the bytesDecompressed variable is always zero size, but the zip File is 1.2MB

Any help here would be greatly appreciated.

EDIT: Answer, thanks to Mahantesh

Here is the code for unzipping a file:

private async void UnZipFile()
{
    var folder = ApplicationData.Current.LocalFolder;

    using (var zipStream = await folder.OpenStreamForReadAsync("thezip.zip"))
    {
        using (MemoryStream zipMemoryStream = new MemoryStream((int)zipStream.Length))
        {
            await zipStream.CopyToAsync(zipMemoryStream);

            using (var archive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Read))
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (entry.Name != "")
                    {
                        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();
                            }
                        }
                    }
                }
            }
        }
    }
}

2条回答
Viruses.
2楼-- · 2019-04-19 10:56

Based on your code and suggestions, I came up with one which supports folders extraction, which was one of my needs:

private async void UnZipFile(string file)
{
    var folder = ApplicationData.Current.LocalFolder;

    using (var zipStream = await folder.OpenStreamForReadAsync(file))
    {
        using (MemoryStream zipMemoryStream = new MemoryStream((int)zipStream.Length))
        {
            await zipStream.CopyToAsync(zipMemoryStream);

            using (var archive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Read))
            {
                foreach (ZipArchiveEntry entry in archive.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();
        }
    }
}
查看更多
不美不萌又怎样
3楼-- · 2019-04-19 11:06

In Metro style apps, you work with compressed files by using the methods in the ZipArchive, ZipArchiveEntry, DeflateStream, and GZipStream classes.

Refer : UnZip File in Metro

Refer : Folder zip/unzip in metro c#

查看更多
登录 后发表回答