Create Compressed Folder
I would like to zip a folder, I tried to write code that should do this, but I am getting an error that the folder is empty :
- Create the empty zip file.
- It does not allow me to extract the files from the zip file (It tells me that, in fact, that the folder is empty).
MainPage.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 FolderPathOne = string.Empty;
string FoldeDestinationOne = string.Empty;
StorageFolder FolderPath;
StorageFolder DestinationPath;
public MainPage()
{
this.InitializeComponent();
}
private async void BtnChooseFolder_Click(object sender, RoutedEventArgs e)
{
FolderPicker FolderPickFol = new FolderPicker();
FolderPickFol.SuggestedStartLocation = PickerLocationId.Desktop;
FolderPickFol.FileTypeFilter.Add("*");
StorageFolder SelectFolderToZipa = await FolderPickFol.PickSingleFolderAsync();
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolder", SelectFolderToZipa);
FolderPath = SelectFolderToZipa;
FolderPathOne = SelectFolderToZipa.Path;
TxbFolderToZip.Text = FolderPathOne;
}
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);
DestinationPath = SelectFolderToZipa;
FoldeDestinationOne = SelectFolderToZipa.Path;
TxbZipFolder.Text = FoldeDestinationOne;
}
private async void BtnZip_Click(object sender, RoutedEventArgs e)
{
StorageFile zipFile = await DestinationPath.CreateFileAsync("ZipFolderTest.zip", CreationCollisionOption.ReplaceExisting);
Stream zipToCreate = await zipFile.OpenStreamForWriteAsync();
ZipArchive archive = new ZipArchive(zipToCreate, ZipArchiveMode.Update);
await ZipFolderContents(FolderPath, archive, FolderPath.Path);
}
private async Task ZipFolderContents(StorageFolder sourceFolder, ZipArchive archive, string sourceFolderPath)
{
IReadOnlyList<StorageFile> files = await sourceFolder.GetFilesAsync();
foreach (StorageFile file in files)
{
ZipArchiveEntry readmeEntry = archive.CreateEntry(file.Path.Remove(0, sourceFolderPath.Length));
byte[] buffer = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(file));
using (Stream entryStream = readmeEntry.Open())
{
await entryStream.WriteAsync(buffer, 0, buffer.Length);
}
}
}
Although there is writing in the stream, it creates the empty and inaccessible file.
Thanks in advance!