Skip to content

Instantly share code, notes, and snippets.

@conholdate-docs-gists
Created October 20, 2022 09:56
Show Gist options
  • Save conholdate-docs-gists/7cbce1a3a43e0499231b59427a842a93 to your computer and use it in GitHub Desktop.
Save conholdate-docs-gists/7cbce1a3a43e0499231b59427a842a93 to your computer and use it in GitHub Desktop.
Extract from ZIP or Attachments in C# | https://docs.conholdate.com/net/extract-from-zip/
// Create an instance of Parser class
using (Parser parser = new Parser(filePath))
{
// Extract attachments from the container
IEnumerable<ContainerItem> attachments = parser.GetContainer();
// Check if container extraction is supported
if (attachments == null)
{
Console.WriteLine("Container extraction isn't supported");
}
// Iterate over zip entities
foreach (ContainerItem item in attachments)
{
// Print the file path
Console.WriteLine(item.FilePath);
try
{
// Create Parser object for the zip entity content
using (Parser attachmentParser = item.OpenParser())
{
// Extract an zip entity text
using (TextReader reader = attachmentParser.GetText())
{
Console.WriteLine(reader == null ? "No text" : reader.ReadToEnd());
}
}
}
catch (UnsupportedDocumentFormatException)
{
Console.WriteLine("Isn't supported.");
}
}
}
@conholdate-docs-gists
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment