从 UWP 中的存档中提取特定文件 [英] Extracting specific File from archive in UWP
问题描述
为了节省空间,我在 UWP 项目中压缩了我的书籍(xml 格式).我想根据文件名称将文件提取到我的本地文件夹.
To save space I have zipped my books(in xml format) in my UWP Project. I want to Extract a file to my Local Folder based upon it's name.
到目前为止我所做的(这会提取所有文件):
Till Now what I have done(This extracts all files) :
ZipFile.ExtractToDirectory(sourceCompressedFile.Path, destinationFolder.Path);
但是,这会将我的存档中的所有文件提取到我的目标文件夹.我知道使用 SharpZipLib 这可能是一项微不足道的任务,但这是一种内置方法,可以帮助我减少应用程序的大小.我只想提取一个名称与我提供的名称匹配的文件.除此以外还有三种方法,但我用它们迷路了.
However this extracts all the files from my archive to my destination folder. I know this could be a trivial task using SharpZipLib but this is an inbuilt method and would help me reduce my app size . I simply want to extract a file whose name matches with a name I provide. There are three methods other than this but I lost my way using them.
这可以使用 DotNetZip 轻松完成,如下所示 here 但我不想使用任何第三方库
This can be done easily using DotNetZip as seen here but I don't want to use any third party Library
推荐答案
我认为您将多个文件压缩在一个 zip 存档中,ZipFile.ExtractToDirectory Method (String, String) 将指定 zip 存档中的所有文件解压到一个目录中.
I think you have several files zipped in one zip archive, so will the ZipFile.ExtractToDirectory Method (String, String) extract all the files in the specified zip archive to a directory.
如果您只想访问此压缩存档中的一个特殊文件,您可以使用 ZipArchiveEntry 类来实现这个工作,例如这里:
If you just want to access one special file in this zipped archive, you can use ZipArchiveEntry Class to achieve this work, for example here:
StorageFolder _folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
/*fails here FileNotFound*/
StorageFile sourceCompressedFile = await _folder.GetFileAsync("archived.zip");
StorageFolder folder = ApplicationData.Current.LocalFolder;
// ZipFile.ExtractToDirectory(file.Path, folder.Path);
using (ZipArchive archive = ZipFile.OpenRead(sourceCompressedFile.Path))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.ToString() == "miao2.jpg")
{
entry.ExtractToFile(Path.Combine(folder.Path, entry.FullName));
}
}
}
我压缩了几张图片作为archived.zip"文件进行测试,在这个示例中,它只会提取名为miao2.jpg"的图片文件.
I zipped several pictures as the "archived.zip" file for test, in this sample, it will only extract the image file named "miao2.jpg".
这篇关于从 UWP 中的存档中提取特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!