【问题标题】:Zip File percentage UWPZip 文件百分比 UWP
【发布时间】:2019-02-25 20:39:10
【问题描述】:

我想查看每个文件压缩的​​百分比,我尝试过错误地解决这个问题,这里是代码:

MainPaga.xaml

<Grid>
    <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel Orientation="Horizontal" Margin="5">
            <Button x:Name="BtnChooseFolder" Click="BtnChooseFolder_Click" Content="Choose Folder" Margin="5"/>
            <TextBlock Text="Folder to Zip: " VerticalAlignment="Center"/>
            <TextBlock x:Name="TxbFolderToZip" VerticalAlignment="Center"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="5">
            <Button x:Name="BtnChooseDestination" Click="BtnChooseDestination_Click" Content="Choose Destination" Margin="5"/>
            <TextBlock Text="Zip Folder: " VerticalAlignment="Center"/>
            <TextBlock x:Name="TxbZipFolder" VerticalAlignment="Center"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button x:Name="BtnZip" Click="BtnZip_Click" Content="Zippa" Margin="10"/>
            <TextBlock x:Name="TxbPercentage" VerticalAlignment="Center"/>
        </StackPanel>
    </StackPanel>
</Grid>

MainPage.xaml.cs

string DestinationFolderPath = string.Empty;
string SourceFolderPath = string.Empty;

StorageFolder SourceFolder;
StorageFolder DestinationFolder;

public MainPage()
{
    this.InitializeComponent();
}

private async void BtnChooseFolder_Click(object sender, RoutedEventArgs e)
{
    FolderPicker FolderPickFol = new FolderPicker();
    FolderPickFol.SuggestedStartLocation = PickerLocationId.Desktop;
    FolderPickFol.FileTypeFilter.Add("*");
    Windows.Storage.StorageFolder SelectFolderToZipa = await FolderPickFol.PickSingleFolderAsync();
    StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolder", SelectFolderToZipa);
    SourceFolder = SelectFolderToZipa;
    SourceFolderPath = SelectFolderToZipa.Path;
    TxbFolderToZip.Text = SourceFolderPath;
}

private async void BtnChooseDestination_Click(object sender, RoutedEventArgs e)
{
    FolderPicker FolderPickFol = new FolderPicker();
    FolderPickFol.SuggestedStartLocation = PickerLocationId.Desktop;
    FolderPickFol.FileTypeFilter.Add("*");
    StorageFolder SelectFolderToZipa = await FolderPickFol.PickSingleFolderAsync();
    StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedDestination", SelectFolderToZipa);
    DestinationFolder = SelectFolderToZipa;
    DestinationFolderPath = SelectFolderToZipa.Path;
    TxbZipFolder.Text = DestinationFolderPath;
}

private async void BtnZip_Click(object sender, RoutedEventArgs e)
{

    if (SourceFolder != null)
    {
        try
        {
            string appFolderPath = ApplicationData.Current.LocalFolder.Path;
            StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", SourceFolder);

            //Gets the folder named TestFolder from Documents Library Folder  
            StorageFolder sourceFolder = SourceFolder;

            //Creates a zip file named TestFolder.zip in Local Folder  
            StorageFile zipFile = await DestinationFolder.CreateFileAsync("TestFolder.zip", CreationCollisionOption.ReplaceExisting);
            Stream zipToCreate = await zipFile.OpenStreamForWriteAsync();
            ZipArchive archive = new ZipArchive(zipToCreate, ZipArchiveMode.Create);

            await ZipFolderContentsHelper(sourceFolder, archive, sourceFolder.Path);
            archive.Dispose();
            MessageDialog msg = new MessageDialog("Success");
            await msg.ShowAsync();
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.ToString());
        }
    }
}


private async Task ZipFolderContentsHelper(StorageFolder sourceFolder, ZipArchive archive, string sourceFolderPath)
{
    IReadOnlyList<StorageFile> files = await sourceFolder.GetFilesAsync();

    foreach (StorageFile file in files)
    {
        var path = file.Path.Remove(0, sourceFolderPath.Length);
        ZipArchiveEntry readmeEntry = archive.CreateEntry(file.Path.Remove(0, sourceFolderPath.Length));
        ulong fileSize = (await file.GetBasicPropertiesAsync()).Size;
        byte[] buffer = fileSize > 0 ? (await FileIO.ReadBufferAsync(file)).ToArray() : new byte[0];

        using (Stream entryStream = readmeEntry.Open())
        {
            await entryStream.WriteAsync(buffer, 0, buffer.Length);
            await Task.Run(async () =>
            {
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                {
                    double progress = entryStream.Position / buffer.Length;
                    TxbPercentage.Text = ((progress) * 100).ToString("0.00");
                });
            });
        }
    }

    IReadOnlyList<StorageFolder> subFolders = await sourceFolder.GetFoldersAsync();

    if (subFolders.Count() == 0)
    {
        return;
    }

    foreach (StorageFolder subfolder in subFolders)
    {
         await ZipFolderContentsHelper(subfolder, archive, sourceFolderPath);
    }
}

在 ZipFolderContentsHelper 方法的 Stream 中,我选择了“entryStream.Position”,但这不会返回文件实际完成的压缩大小......我该如何继续?

总是提前感谢!

【问题讨论】:

    标签: c# uwp zip percentage


    【解决方案1】:

    ZipArchiveFileEntry 类提供属性LenghtCompressedLength。您可以使用这些属性来计算压缩比。如果您采用您的方法,您必须先将其中一个长度转换为double,然后再分配它(double progress = (double) entryStream.Position / buffer.Length)。但我不确定这是否会产生正确的结果。

    【讨论】:

    • 我可能找到了一个例子,但我无法调整它,因为在这个example 中,对于 zip 文件,我们使用字符串中的路径而不是存储文件,但是它可能对解决方案有用
    • 你能举一个你的答案的例子吗?
    【解决方案2】:

    在 ZipFolderContentsHelper 方法的 Stream 中,我选择了“entryStream.Position”,但这不会返回文件的实际完成大小......我该如何继续?

    如果您的最终要求是获取压缩文件的大小。您可以使用StorageFile.Properties 获取它。

    然后,您可以调用StorageFile.GetBasicPropertiesAsync 方法。此方法返回一个 BasicProperties 对象,该对象定义了项目(文件或文件夹)的大小以及上次修改项目的时间。

    更多信息,请阅读Getting a file's basic properties了解详情。

    [2018 年 9 月 25 日更新]

    不,我不想查找文件大小,而是查看单个文件的压缩进度。

    使用您的代码,entryStream.Position 始终等于buffer.Length,而ZipArchive 类不包括进度报告。因此,您需要考虑的是自己在代码中报告异步任务的进度。

    请看相关信息:Enabling Progress and Cancellation in Async APIs和相关话题How to do progress reporting using Async/Await

    【讨论】:

    • 不,我不想查找文件大小,而是查看单个文件的压缩进度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    相关资源
    最近更新 更多