我正在尝试将文件夹的内容归档到zip文件中。该内容大部分是大图像。我正在尝试使用以下代码进行此操作:
var folderPicker =
new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
StorageFile zipFile = await folder.CreateFileAsync(ThemeName.Text + ".zip",
Windows.Storage.CreationCollisionOption.GenerateUniqueName);
Stream themeZipFile = await zipFile.OpenStreamForWriteAsync();
ZipArchive archive = new ZipArchive(themeZipFile);
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFolder temp = await localFolder.GetFolderAsync("Temp");
var files = await temp.GetFilesAsync();
foreach (var item in files)
{
archive.CreateEntryFromFile(item.Path, item.Name);
}
}
但是,在执行时,我收到一个错误:
IOException:无法找到负的绝对流位置。
现在,当我尝试此代码时:
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder =
await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
StorageFile zipFile =
await folder.CreateFileAsync(ThemeName.Text + ".zip", Windows.Storage.CreationCollisionOption.GenerateUniqueName);
await Task.Run(() => ZipFile.CreateFromDirectory(localFolder.Path, zipFile.Path));
}
我知道对我选择的任何文件夹的访问均被拒绝! 我该如何解决将几个较大的文件合并到一个存档中的问题?
答案 0 :(得分:1)
使用此方法,您可以从源目录开始创建ZIP文件。 这是Microsoft的一些文档:ZIPFile.CreateFromDirectory
您可以在以下命名空间中找到提到的类和方法:System.IO.Compression
答案 1 :(得分:1)
如果创建一个临时文件夹来归档整个文件夹,那么您可以尝试以下方法:
using System.IO.Compression;
var files = System.IO.Directory.EnumerateFiles(string PATH, ".jpeg", System.IO.SearchOption.AllDirectories)
foreach (var file in files)
{
File.Copy(file, tempPath + @"\" + System.IO.Path.GetFileName(file));
}
ZipFile.CreateFromDirectory(tempPath, zipPath, CompressionLevel.Fastest, true);
Directory.Delete(tempPath, true); //delete tempfolder after compress commplete
答案 2 :(得分:1)
如何在一个ZIP文件中归档多个文件?
scatter_update()
方法将使用第二个CreateFromDirectory
创建zip文件
参数。因此,您无需提前创建zip文件。您可以使用以下代码直接压缩文件夹。
destinationArchiveFileName