创建压缩文件夹UWP
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建压缩文件夹UWP相关的知识,希望对你有一定的参考价值。
创建压缩文件夹
我想压缩一个文件夹,我试着编写应该这样做的代码,但是我收到一个错误,该文件夹是空的:
- 创建空的zip文件。
- 它不允许我从zip文件中提取文件(它告诉我,事实上,该文件夹是空的)。
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);
}
}
}
虽然流中有写入,但它会创建空的和不可访问的文件。
提前致谢!
答案
对于您的方案,您可以使用ZipFile.CreateFromDirectory
方法直接压缩文件夹。
string DestinationFolderPath = string.Empty;
string SourceFolderPath = string.Empty;
StorageFolder SourceFolder;
StorageFolder DestinationFolder;
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)
{
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", SourceFolder);
await Task.Run(() =>
{
try
{
System.IO.Compression.ZipFile.CreateFromDirectory(SourceFolderPath, $"{DestinationFolderPath}\{SourceFolder.Name}.zip");
}
catch (Exception w)
{
}
});
}
}
对于此方法,您只需传递源文件夹路径和目标zip文件两个参数。
ZipFile.CreateFromDirectory(SourceFolderPath, $"{DestinationFolderPath}\{SourceFolder.Name}.zip");
以上代码根据您的帖子进行编辑,您可以直接在项目中使用它。
更新
从这个case得到。
从
WindowsRuntimeBufferExtensions
ToArray方法抛出此System.ArgumentException,该方法期望IBuffer的大小大于0。
所以我用follow方法替换它
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);
}
}
IReadOnlyList<StorageFolder> subFolders = await sourceFolder.GetFoldersAsync();
if (subFolders.Count() == 0)
{
return;
}
foreach (StorageFolder subfolder in subFolders)
{
await ZipFolderContentsHelper(subfolder, archive, sourceFolderPath);
}
}
这是你可以直接使用的code sample。
以上是关于创建压缩文件夹UWP的主要内容,如果未能解决你的问题,请参考以下文章
UWP 后台任务 - 应用程序触发器 - 当文件已存在时无法创建文件
如何在 Xamarin.UWP 中调整大小和减小大小(使用压缩质量值)
在为 uwp 生成 AppPackage 时在 Xamarin.Forms 中。有没有办法将其转换为 .exe 或单击安装之类的东西