Я бы хотел видеть процентное соотношение каждого архивируемого файла, я пытался решить эту проблему неправильно, вот код:
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 я выбрал "entryStream.Position", но это не возвращает фактическое завершениеразмер файла в архиве ... Как продолжить?
Всегда заранее спасибо!