解压缩Windows 8 C上的ZIP文件#

时间:2012-07-19 03:31:52

标签: c# compression windows-8

我正在为Windows 8构建一个metro风格的应用程序,我有一个zip文件,我正在从Web服务下载,我想提取它。

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

这是我到目前为止所做的:

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

            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 个答案:

答案 0 :(得分:13)

在Metro风格的应用程序中,您可以使用ZipArchive,ZipArchiveEntry,DeflateStream和GZipStream类中的方法处理压缩文件。

参考:UnZip File in Metro

参考:Folder zip/unzip in metro c#

答案 1 :(得分:4)

根据您的代码和建议,我想出了一个支持文件夹提取的方法,这是我的需求之一:

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