Web ZIP archive browser
Simple ASP.NET application for ZIP archive browsing.
This sample demonstrates the following features:
- Browsing folders of a ZIP archive.
- Using ASP.NET GridView to display a content of a ZIP archive folder.
- Extracting a file from a ZIP archive and sending it through HTTP to the user's web browser.
The following code snippets show the core of this sample:
Binding a ZIP archive folder listing to ASP.NET GridView control:
C#
// open the current ZIP archive using (ZipArchive archive = new ZipArchive( @"c:\temp\archive.zip", ArchiveOpenMode.Open, ArchiveAccessMode.Read)) { // gets the list of files and directories // in the root directory of the current ZIP archive ZipItemCollection items = archive.GetItems("*", TraversalMode.NonRecursive); // sort the collection by item path (directories first) items.Sort(); // bind the items to the grid ArchiveFilesGridView.DataSource = items; ArchiveFilesGridView.DataBind(); }
VB.NET
' open the current ZIP archive Using archive As New ZipArchive( _ "c:\temp\archive.zip", _ ArchiveOpenMode.Open, _ ArchiveAccessMode.Read) ' gets the list of files and directories ' in the root of the current ZIP archive Dim items As ZipItemCollection = _ archive.GetItems("*", TraversalMode.NonRecursive) ' sort the collection by item path (directories first) items.Sort() ' bind the items to the grid ArchiveFilesGridView.DataSource = items ArchiveFilesGridView.DataBind() End Using
Extracting a file from a ZIP archive and sending it through HTTP to the web browser:
C#
string currentPath = @"\dir\file.txt"; string currentFileName = Path.GetFileName(currentPath); // open the current ZIP archive using (ZipArchive archive = new ZipArchive( @"c:\temp\archive.zip", ArchiveOpenMode.Open, ArchiveAccessMode.Read)) { // clear response stream and set the response header and content type response.Clear(); response.AddHeader( "Content-Disposition", string.Format("attachment; filename={0}", currentFileName)); response.ContentType = "application/octet-stream"; // extract (decompress) the file from the archive directly // to the response stream archive.ExtractFile(currentPath, response.OutputStream); } // close the current HTTP response and stop executing this page response.End();
VB.NET
Dim currentPath As String = "\dir\file.txt" Dim currentFileName As String = Path.GetFileName(currentPath) ' open the current ZIP archive Using archive As New ZipArchive( _ "c:\temp\archive.zip", _ ArchiveOpenMode.Open, _ ArchiveAccessMode.Read) ' clear response stream and set the response header and content type response.Clear() response.AddHeader( _ "Content-Disposition", _ String.Format("attachment; filename={0}", currentFileName)) response.ContentType = "application/octet-stream" ' extract (decompress) the file from the archive directly ' to the response stream archive.ExtractFile(currentPath, response.OutputStream) End Using ' close the current HTTP response and stop executing this page response.[End]()