解压缩ZIP文件在Windows 8上C# [英] decompress a ZIP file on windows 8 C#

查看:200
本文介绍了解压缩ZIP文件在Windows 8上C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为windows 8建立一个metro风格的应用程式,而且我已经从网路服务下载了一个zip档案,我要解压缩。



我已经看到了压缩和解压缩的示例,但是需要一个文件压缩/解压缩它。我有一个完整的目录结构,我需要提取。



这里是我到目前为止:

  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);
}

但这不好, bytesDecompressed 变量始终为零大小,但zip文件为1.2MB



任何帮助都将非常感激。



编辑:回答,感谢 Mahantesh



这是解压文件的代码:

  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);

使用(var archive = new ZipArchive(zipMemoryStream,ZipArchiveMode.Read))
{
foreach(archive.Entries中的ZipArchiveEntry条目)
{
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();
}
}
}
}
}
}
}
}


解决方案

在Metro风格的应用程序中,使用ZipArchive,ZipArchiveEntry,DeflateStream ,和GZipStream类。



请参阅:在地铁中解压缩文件



参考:在地铁c中解压缩zip / unzip


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();
                            }
                        }
                    }
                }
            }
        }
    }
}

解决方案

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#

这篇关于解压缩ZIP文件在Windows 8上C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆