Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decompress a ZIP file on windows 8 C#

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();
                            }
                        }
                    }
                }
            }
        }
    }
}
like image 517
Mark Avatar asked Jul 19 '12 03:07

Mark


People also ask

How do I open a Compressed zip file in Windows 8?

To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.

Can Windows 8.1 open zip files?

Combine several files into a single zipped folder to more easily share a group of files. In Windows 8.1 Zip/unzip program is not required to compressed the file you may use the built in feature of Windows 8.1 in order to extract the files.

How do I unzip a file without WinZip on Windows 8?

If you are using Windows 7, 8 or 10, follow the following steps to open any zip files without WinZip or WinRAR. Double click the zip file you wish to extract to open the file explorer. At the top part of the explorer menu, find “Compressed folder tools” and click it. Select the “extract” option that appears below it.


2 Answers

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#

like image 94
Shashi Avatar answered Oct 29 '22 21:10

Shashi


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();
        }
    }
}
like image 24
Tuco Avatar answered Oct 29 '22 23:10

Tuco