[UWP]通用Windows平台ZipFile.CreateFromDirectory创建空ZIP文件 [英] [UWP]Universal Windows Platform ZipFile.CreateFromDirectory creates empty ZIP file

查看:91
本文介绍了[UWP]通用Windows平台ZipFile.CreateFromDirectory创建空ZIP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在压缩现有目录时遇到问题。当我尝试压缩现有目录时,我总是得到一个空的zip文件。我是基于MSDN的这个例子。调试应用程序时没有例外。我的应用代码:

 private async void PickFolderToCompressButton_Click(object sender,RoutedEventArgs e)
{
//清除之前返回的文件夹名称,如果是在这种情况的迭代之间存在
OutputTextBlock.Text ="" ;;

FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add(" .dll");
folderPicker.FileTypeFilter.Add(" .json");
folderPicker.FileTypeFilter.Add(" .xml");
folderPicker.FileTypeFilter.Add(" .pdb");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if(folder!= null)
{
//应用程序现在对拾取的文件夹中的所有内容(包括其他子文件夹内容)具有读/写访问权限
StorageApplicationPermissions。 FutureAccessList.AddOrReplace(" PickedFolderToken",文件夹);
OutputTextBlock.Text = $" Picked folder:{folder.Name}" ;;

var files = await folder.GetFilesAsync();
foreach(文件中的var文件)
{
OutputTextBlock.Text + = $" \\\
{file.Name}" ;;
}

await Task.Run(()=>
{
try
{
ZipFile.CreateFromDirectory(folder.Path, $" {folder.Path} \\ {Guid.NewGuid()}。zip",
CompressionLevel.NoCompression,true);
Debug.WriteLine(" folder zipped");
}
catch(例外w)
{
Debug.WriteLine(w);
}
});
}
其他
{
OutputTextBlock.Text ="操作已取消。" ;;
}
}

创建一个Zip文件,但它始终为空。源文件夹中有很多文件。


我错了什么?




解决方案

我在压缩现有目录时遇到问题。当我尝试压缩现有目录时,我总是得到一个空的zip文件。我是基于MSDN的这个例子。调试应用程序时没有例外。我的应用代码:

 private async void PickFolderToCompressButton_Click(object sender,RoutedEventArgs e)
{
//清除之前返回的文件夹名称,如果是在这种情况的迭代之间存在
OutputTextBlock.Text ="" ;;

FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add(" .dll");
folderPicker.FileTypeFilter.Add(" .json");
folderPicker.FileTypeFilter.Add(" .xml");
folderPicker.FileTypeFilter.Add(" .pdb");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if(folder!= null)
{
//应用程序现在对拾取的文件夹中的所有内容(包括其他子文件夹内容)具有读/写访问权限
StorageApplicationPermissions。 FutureAccessList.AddOrReplace(" PickedFolderToken",文件夹);
OutputTextBlock.Text =


" Picked folder:{folder.Name}" ;;

var files = await folder.GetFilesAsync();
foreach(文件中的var文件)
{
OutputTextBlock.Text + =


" \\\
{file.Name}" ;;
}

await Task.Run(()=>
{
try
{
ZipFile.CreateFromDirectory(folder.Path,

I have a problem with zipping existing directories. When I try to compress an existing directory, I always get an empty zip file. I was basing on this example from MSDN. There are no exceptions when debugging the application. My app code:

private async void PickFolderToCompressButton_Click(object sender, RoutedEventArgs e)
{
    // Clear previous returned folder name, if it exists, between iterations of this scenario
    OutputTextBlock.Text = "";

    FolderPicker folderPicker = new FolderPicker();
    folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
    folderPicker.FileTypeFilter.Add(".dll");
    folderPicker.FileTypeFilter.Add(".json");
    folderPicker.FileTypeFilter.Add(".xml");
    folderPicker.FileTypeFilter.Add(".pdb");
    StorageFolder folder = await folderPicker.PickSingleFolderAsync();
    if (folder != null)
    {
        // Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
        StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
        OutputTextBlock.Text = $"Picked folder: {folder.Name}";

        var files  = await folder.GetFilesAsync();
        foreach (var file in files)
        {
            OutputTextBlock.Text += $"\n {file.Name}";
        }

        await Task.Run(() =>
        {
            try
            {
                ZipFile.CreateFromDirectory(folder.Path, $"{folder.Path}\\{Guid.NewGuid()}.zip",
                    CompressionLevel.NoCompression, true);
                Debug.WriteLine("folder zipped");
            }
            catch (Exception w)
            {
                Debug.WriteLine(w);
            }
        });
    }
    else
    {
        OutputTextBlock.Text = "Operation cancelled.";
    }
}

A Zip file is created, but it is always empty. There are many files in the source folder.

What I am making wrong?


解决方案

I have a problem with zipping existing directories. When I try to compress an existing directory, I always get an empty zip file. I was basing on this example from MSDN. There are no exceptions when debugging the application. My app code:

private async void PickFolderToCompressButton_Click(object sender, RoutedEventArgs e)
{
    // Clear previous returned folder name, if it exists, between iterations of this scenario
    OutputTextBlock.Text = "";

    FolderPicker folderPicker = new FolderPicker();
    folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
    folderPicker.FileTypeFilter.Add(".dll");
    folderPicker.FileTypeFilter.Add(".json");
    folderPicker.FileTypeFilter.Add(".xml");
    folderPicker.FileTypeFilter.Add(".pdb");
    StorageFolder folder = await folderPicker.PickSingleFolderAsync();
    if (folder != null)
    {
        // Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
        StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
        OutputTextBlock.Text =


"Picked folder: {folder.Name}"; var files = await folder.GetFilesAsync(); foreach (var file in files) { OutputTextBlock.Text +=


"\n {file.Name}"; } await Task.Run(() => { try { ZipFile.CreateFromDirectory(folder.Path,


这篇关于[UWP]通用Windows平台ZipFile.CreateFromDirectory创建空ZIP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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