Hi ahmedsa,
There is extension methods to create a new entry in a zip archive from an existing file and extract the archive contents.
You need to reference the System.IO.Compression.FileSystem assembly in your project.
For exporting only specific extension you need to create a zip and add the files to zip using CreateEntryFromFile method by checking each file extension.
Refer below sample code.
C#
string pathToCreate = ExcelExportPath + "\\file" + Guid.NewGuid().ToString();
string zipPath = ExcelExportPath + "\\result" + Guid.NewGuid().ToString() + ".zip";
using (System.IO.FileStream zipToOpen = new System.IO.FileStream(zipPath, FileMode.OpenOrCreate))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
string[] files = System.IO.Directory.GetFiles(pathToCreate);
foreach (string filePath in files)
{
if (System.IO.Path.GetExtension(filePath).ToLower() == ".xlsx")
{
archive.CreateEntryFromFile(filePath, System.IO.Path.GetFileName(filePath));
archive.ExtractToDirectory(pathToCreate);
}
}
}
}
VB.Net
Dim pathToCreate As String = ExcelExportPath & "\file" + Guid.NewGuid().ToString()
Dim zipPath As String = ExcelExportPath & "\result" + Guid.NewGuid().ToString() & ".zip"
Using zipToOpen As System.IO.FileStream = New System.IO.FileStream(zipPath, FileMode.OpenOrCreate)
Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
Dim files As String() = System.IO.Directory.GetFiles(pathToCreate)
For Each filePath As String In files
If System.IO.Path.GetExtension(filePath).ToLower() = ".xlsx" Then
archive.CreateEntryFromFile(filePath, System.IO.Path.GetFileName(filePath))
archive.ExtractToDirectory(pathToCreate)
End If
Next
End Using
End Using