|
|
千杯不醉的树叶 · 第五十四次特邀测试开启公告__《阴阳师》手游 ...· 1 年前 · |
|
|
越狱的橙子 · 除了大S,还有4个中国女星嫁给了韩国男星,还 ...· 2 年前 · |
|
|
温文尔雅的番茄 · 关于禁止旅客随身携带液体物品乘坐国内航班的公 ...· 2 年前 · |
|
|
耍酷的脸盆 · 天神纠错组视频,分类表 - 快看漫画· 2 年前 · |
|
|
叛逆的小蝌蚪 · 2023年国家统一法律职业资格考试公告_通知 ...· 3 年前 · |
我正在尝试将文件夹的内容存档到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));
}
我得到的访问权限被拒绝,无论我选择的文件夹!我如何解决这个问题,将几个较大的文件合并到一个归档文件中?
使用此方法,您可以从源目录开始创建ZIP文件。以下是微软的一些文档: ZIPFile.CreateFromDirectory
您可以在以下名称空间中找到提到的类和方法: System.IO.Compression
如果创建临时文件夹来归档整个文件夹是您的解决方案,请尝试以下方法:
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
如何将多个文件归档到一个
文件中?
CreateFromDirectory
方法将使用第二个
destinationArchiveFileName
参数创建zip文件。因此您不需要预先创建zip文件。你可以使用下面的代码直接压缩你的文件夹。
if (ZipFolder != null)
// Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", ZipFolder);
await Task.Run(() =>
ZipFile.CreateFromDirectory(ApplicationData.Current.LocalFolder.Path, $"{ZipFolder.Path}\\{Guid.NewGuid()}.zip");
Debug.WriteLine("folder zipped");
catch (Exception w)
|
|
耍酷的脸盆 · 天神纠错组视频,分类表 - 快看漫画 2 年前 |