Created
October 20, 2022 09:56
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read complete article here: https://docs.conholdate.com/net/extract-from-zip/