Render ZIP Archives using C#

ZIP files contain one or more compressed files or folders to act as a single file. These are widely used to save storage space and increase the performance of your computers. You can effectively transfer your files and folders in a ZIP archive from one location to another. As a C# developer, you can easily render ZIP archives and view their content programmatically. This article will be focusing on how to render ZIP archives using C#.

The following topics are discussed/covered in this article:

C# API to View ZIP Files

For the rendering of ZIP files, I will be using GroupDocs.Viewer for .NET API. It is a powerful document viewer API that supports over 170 file and document types. The API provides the most flexible document viewing solution to render and display widely used file formats anywhere without installing any external software. It also enables you to quickly view PDF, HTML, XML, Microsoft Office Word, Excel worksheets, PowerPoint presentations, Outlook emails, Visio diagrams, Project, metafiles, images, and various other file formats with ease and with fewer programming hazards.

You can either download the DLL of the API or install it using the NuGet.

Install-Package GroupDocs.Viewer

Render ZIP Archives in HTML

You can render the ZIP file in HTML by following the simple steps given below:

  1. Create an instance of the Viewer class
  2. Provide the input file path
  3. Create an instance of HtmlViewOptions class
  4. Provide output file path
  5. Call the View() method and pass HtmlViewOptions

The following code sample shows how to render the ZIP file in HTML using C#.

Render ZIP Archives in HTML

Render ZIP Archives in HTML

The Viewer class is the main class that provides functionality to control the document rendering process. The View() method of this class creates the view of all document pages.

The HtmlViewOptions class provides options for the rendering of documents into HTML format. The ForEmbeddedResources constructor method creates a new instance of HtmlViewOptions class for rendering into HTML with embedded resources. As you can see, I have provided the output file path in the code example.

You can find more details about “Document HTML Viewer” in the documentation.

Render Specific Folder from ZIP Archives in HTML

You can render a specific folder available inside the ZIP file in HTML by following the simple steps given below:

  1. Create an instance of the Viewer class
  2. Provide the input file path
  3. Create an instance of HtmlViewOptions class
  4. Provide output file path
  5. Set the folder name to render
  6. Call the View() method and pass HtmlViewOptions

The following code sample shows how to render a specific folder from the ZIP file in HTML using C#.

Render Specific Folder from ZIP Archives in HTML

Render Specific Folder from ZIP Archives in HTML

The ArchiveOptions class provides options for the rendering of archive files. It enables you to render a specific folder from ZIP archives by providing the name of the folder available inside the archive.

View Content of ZIP Archives in PDF

You can render the ZIP file in PDF document by following the simple steps given below:

  1. Create an instance of the Viewer class
  2. Provide the input file path
  3. Create an instance of PdfViewOptions class
  4. Provide output file path
  5. Call the View() method and pass PdfViewOptions

The following code sample shows how to render the ZIP file in PDF using C#.

View Content of ZIP Archives in PDF

View Content of ZIP Archives in PDF

The PdfViewOptions class provides options for the rendering of documents into PDF format. You can find more details about “Document PDF Viewer” in the documentation.

Render ZIP Archives to JPG

You can render the ZIP file in JPG image by following the simple steps given below:

  1. Create an instance of the Viewer class
  2. Provide the input file path
  3. Create an instance of JpgViewOptions class
  4. Provide output file path
  5. Call the View() method and pass JpgViewOptions

The following code sample shows how to render the ZIP file in JPG image using C#.

Render ZIP Archives to JPG

Render ZIP Archives to JPG

You can render documents to JPG or PNG image formats. The JpgViewOptions class provides options for the rendering of documents into JPG format. Similarly, the PngViewOptions class provides options for rendering documents into PNG format.

You can find more details about “Document Image Viewer” in the documentation.

Get a List of Folders from ZIP archives

You can get a list of all the folders and subfolders from the ZIP file programmatically by following the steps given below:

  1. Create an instance of the Viewer class
  2. Provide the input file path
  3. Create ViewInfoOptions
  4. Create ViewInfo instance by calling GetViewInfo() method
  5. Get ArchiveViewInfo
  6. Show the results

The following code sample shows how to get a list of folders from the ZIP file using C#.

Get a List of Folders from ZIP archives

Get a List of Folders from ZIP archives

The ViewInfoOptions class provides options used for retrieving the information about the view. It provides various methods to get view information for specific formats. I used the ForHtmlView() method which initializes a new instance of ViewInfoOptions class to retrieve information about the view when rendering into HTML.

The ViewInfo class provides view information for the generic documents. The GetViewInfo() method of the Viewer class returns information about the view and document-specific information.

The ArchiveViewInfo class provides view information for the archive file.

Render and Rename ZIP Files

You can rename ZIP files while rendering programmatically by following the steps given below:

  1. Create an instance of the Viewer class
  2. Provide the input file path
  3. Create an instance of PdfViewOptions class
  4. Provide output file path
  5. Set new file name to display
  6. Call the View() method and pass PdfViewOptions

The following code sample shows how to rename ZIP file while rendering using C#.

Render and Rename ZIP Files

Render and Rename ZIP Files

The ArchiveOptions class provides the FileName property which is used to display the file name in the header. You can set a new display name as shown in the above code example. By default, it displays the name of the source file.

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to render ZIP archives using C#. You have also learned how to convert and view the content of ZIP files in HTML, PDF, and JPG images. Moreover, you have learned how to get a list of folders and subfolders from the ZIP archive programmatically in C#. You can learn more about GroupDocs.Viewer for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also