14

我正在为 Windows 8 构建一个 Metro 风格的应用程序,我有一个从 Web 服务下载的 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);

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

2 回答 2

14

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

参考:Metro 中的解压缩文件

参考:metro c#中的文件夹压缩/解压缩

于 2012-07-19T06:25:08.430 回答
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();
        }
    }
}
于 2012-09-14T13:58:50.657 回答