Adapted from the ZipArchive
and XmlSerializer.Deserialize()
manual pages.
The ZipArchiveEntry
class has an Open()
method, which returns a stream to the file.
string zipPath = @"c:\example\start.zip";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
var sample = archive.GetEntry("sample.xml");
if (sample != null)
{
using (var zipEntryStream = sample.Open())
{
XmlSerializer serializer = new XmlSerializer(typeof(SampleClass));
SampleClass deserialized =
(SampleClass)serializer.Deserialize(zipEntryStream);
}
}
}
Note that, as documented on MSDN, you need to add a reference to the .NET assembly System.IO.Compression.FileSystem
in order to use the ZipFile
class.