通用 Windows 平台 ZipFile.CreateFromDirectory 创建空 ZIP 文件
Universal Windows Platform ZipFile.CreateFromDirectory creates empty ZIP file
我在压缩现有目录时遇到问题。
当我尝试压缩现有目录时,我总是得到一个空的 zip 文件。
我的代码基于 MSDN 中的这个示例。
调试应用程序时没有异常。
我的代码:
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.";
}
}
Zip 文件已创建,但始终为空。源文件夹中有很多文件。
我们发现这可能是由文件系统 api 的 .NET Core 实现引起的。
当前的解决方法是先将您要压缩的文件夹放入 windows runtime 应用程序的本地数据文件夹,并在使用压缩文件 class 时从该文件夹读取可以生成预期结果.
你可以参考MSDN上的a related post。
zipfile 库仅支持压缩回应用程序本地文件夹。如果你有来自其他文件夹的权限令牌,你可能想直接压缩。 writeZip 函数也可用于从其他地方添加一个单独的文件。
public async void Backup(StorageFolder source, StorageFolder destination)
{
var zipFile = await destination.CreateFileAsync("backup.zip",
CreationCollisionOption.ReplaceExisting);
var zipToCreate = await zipFile.OpenStreamForWriteAsync();
using (var archive = new ZipArchive(zipToCreate, ZipArchiveMode.Update))
{
var parent = source.Path.Replace(source.Name, "");
await RecursiveZip(source, archive, parent);
}
}
private async Task RecursiveZip(StorageFolder sourceFolder, ZipArchive archive, string sourceFolderPath)
{
var files = await sourceFolder.GetFilesAsync();
foreach (var file in files)
{
await WriteZip(file, archive, sourceFolderPath);
}
var subFolders = await sourceFolder.GetFoldersAsync();
foreach (var subfolder in subFolders)
{
await RecursiveZip(subfolder, archive, sourceFolderPath);
}
}
private async Task WriteZip(StorageFile file, ZipArchive archive, string sourceFolderPath)
{
var entryName = file.Path.Replace(sourceFolderPath, "");
var readmeEntry = archive.CreateEntry(entryName, CompressionLevel.Optimal);
var reader = await file.OpenStreamForReadAsync();
using (var entryStream = readmeEntry.Open())
{
await reader.CopyToAsync(entryStream);
}
}
我在压缩现有目录时遇到问题。 当我尝试压缩现有目录时,我总是得到一个空的 zip 文件。 我的代码基于 MSDN 中的这个示例。 调试应用程序时没有异常。
我的代码:
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.";
}
}
Zip 文件已创建,但始终为空。源文件夹中有很多文件。
我们发现这可能是由文件系统 api 的 .NET Core 实现引起的。
当前的解决方法是先将您要压缩的文件夹放入 windows runtime 应用程序的本地数据文件夹,并在使用压缩文件 class 时从该文件夹读取可以生成预期结果.
你可以参考MSDN上的a related post。
zipfile 库仅支持压缩回应用程序本地文件夹。如果你有来自其他文件夹的权限令牌,你可能想直接压缩。 writeZip 函数也可用于从其他地方添加一个单独的文件。
public async void Backup(StorageFolder source, StorageFolder destination)
{
var zipFile = await destination.CreateFileAsync("backup.zip",
CreationCollisionOption.ReplaceExisting);
var zipToCreate = await zipFile.OpenStreamForWriteAsync();
using (var archive = new ZipArchive(zipToCreate, ZipArchiveMode.Update))
{
var parent = source.Path.Replace(source.Name, "");
await RecursiveZip(source, archive, parent);
}
}
private async Task RecursiveZip(StorageFolder sourceFolder, ZipArchive archive, string sourceFolderPath)
{
var files = await sourceFolder.GetFilesAsync();
foreach (var file in files)
{
await WriteZip(file, archive, sourceFolderPath);
}
var subFolders = await sourceFolder.GetFoldersAsync();
foreach (var subfolder in subFolders)
{
await RecursiveZip(subfolder, archive, sourceFolderPath);
}
}
private async Task WriteZip(StorageFile file, ZipArchive archive, string sourceFolderPath)
{
var entryName = file.Path.Replace(sourceFolderPath, "");
var readmeEntry = archive.CreateEntry(entryName, CompressionLevel.Optimal);
var reader = await file.OpenStreamForReadAsync();
using (var entryStream = readmeEntry.Open())
{
await reader.CopyToAsync(entryStream);
}
}