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);
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.
This can be done easily using DotNetZip as seen here but I don't want to use any third party Library
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.
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:
I zipped several pictures as the "archived.zip" file for test, in this sample, it will only extract the image file named "miao2.jpg".
Ok... got it!
First, to read a specific entry, use ZipArchiveEntry.GetEntry(entrytosearch);
Second, can't read a ZipArchiveEntry into a IRandomAccessStream, I don't know why... fiddled with it for a good while before deciding to read it in memory first. Since what I'm doing is read images to display on screen, it has limited memory management impact. However, beware of the size if you're reading something big. I would put a check on the .Length of the entry before reading it. However, for simplicty purposes, here is the updated code to read a specific ".jpg" entry of a zip file into a Image.Source.
Not elegant or sophisticated yet, but i hope it saves someone the time I spent fiddling with this!