冷冷的警车 · Oracle获得两个日期之间的时间差(年、月 ...· 4 月前 · |
千杯不醉的生菜 · Create a New SAML ...· 8 月前 · |
大鼻子的手链 · The Resource Server: ...· 1 年前 · |
刚毅的刺猬 · linux服务器无法发送邮件-问答-阿里云开 ...· 1 年前 · |
力能扛鼎的作业本 · **Python ...· 1 年前 · |
我正在尝试将文件夹的内容存档到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)