Render archives as HTML, PDF, and image files

GroupDocs.Viewer for Java allows you to view the contents of archive files in HTML, PDF, PNG, and JPEG formats. You do not need to use third-party file archiver and compression software to display archive file contents within your Java application (web or desktop).

To start using the GroupDocs.Viewer API, create a Viewer class instance. Pass an archive file you want to view to the class constructor. You can load the archive from a file or stream. Call one of the Viewer.view method overloads to convert the archive file to HTML, PDF, or image format.

View archive files online View demos and examples on GitHub

Supported archive and compressed file formats

GroupDocs.Viewer supports the following archive file formats:

Render archive files as HTML

To convert an archive file to HTML, call the HtmlViewOptions.forEmbeddedResources method to create an HtmlViewOptions class instance and pass this instance to the Viewer.view method.

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
// ...

try (Viewer viewer = new Viewer("Documents.zip")) {
    // Create an HTML file for top folder and each subfolder in the archive.
    // {0} is replaced with the current page number in the file name.
    HtmlViewOptions viewOptions = HtmlViewOptions.forEmbeddedResources("page_{0}.html");
    viewer.view(viewOptions);
}

The following image demonstrates the result:

Render an archive file to HTML

Specify the number of items to render

GroupDocs.Viewer supports the HtmlViewOptions.getArchiveOptions.setItemsPerPage method that allows you to specify the number of archive items to display on each HTML page. The default property value is 16.

The following example demonstrates how to set this option in code:

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
// ...

try (Viewer viewer = new Viewer("Documents.zip")) {
    // Create an HTML file for the top folder and each subfolder in the archive.
    // {0} is replaced with the current page number in the output file name.
    HtmlViewOptions viewOptions = HtmlViewOptions.forEmbeddedResources("page_{0}.html");
    // Specify the number of items to display on each HTML page.
    viewOptions.getArchiveOptions().setItemsPerPage(10);
    viewer.view(viewOptions);
}

Create a single HTML page

If you need to display the contents of an archive file on a single HTML page, use the HtmlViewOptions.setRenderToSinglePage method, as shown below:

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
// ...

try (Viewer viewer = new Viewer("Documents.zip")) {
    // Create an HTML file for the top folder and each subfolder in the archive.
    // {0} is replaced with the current page number in the output file name.
    HtmlViewOptions viewOptions = HtmlViewOptions.forEmbeddedResources("page_{0}.html");
    // Render the archive file to a single page.
    viewOptions.setRenderToSinglePage(true);
    viewer.view(viewOptions);
}

The animation below demonstrates the result. You can navigate between the archive folders. Click on a particular folder to see its contents. To go backward, click the required folder name in the navigation bar at the top of the web page.

Navigate between folders in the archive

Render archive files as PDF

Create a PdfViewOptions class instance and pass it to the Viewer.view method to convert an archive file to PDF. The PdfViewOptions class properties allow you to control the conversion process. For instance, you can protect the output PDF file or reorder its pages. Refer to the following documentation section for details: Rendering to PDF.

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.PdfViewOptions;
// ...

try (Viewer viewer = new Viewer("Documents.zip")) {
    PdfViewOptions viewOptions = new PdfViewOptions("output.pdf");
    viewer.view(viewOptions);
}

The following image demonstrates the result:

Render an archive file to PDF

Render archive files as PNG

Create a PngViewOptions class instance and pass it to the Viewer.view method to convert an archive file to PNG. Use the PngViewOptions.setHeight and PngViewOptions.setWidth methods to specify the output image size in pixels.

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.PngViewOptions;
// ...

try (Viewer viewer = new Viewer("Documents.zip")) {
    // Create a PNG image for the top folder and each subfolder in the archive.
    // {0} is replaced with the current page number in the image name.
    PngViewOptions viewOptions = new PngViewOptions("output_{0}.png");
    // Set width and height.
    viewOptions.setWidth(950);
    viewOptions.setHeight(550);
    viewer.view(viewOptions);
}

The following image demonstrates the result:

Render an archive file to PNG

Render archive files as JPEG

Create a JpgViewOptions class instance and pass it to the Viewer.view method to convert an archive file to JPEG. Use the JpgViewOptions.setHeight and JpgViewOptions.setWidth methods to specify the output image size in pixels.

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.JpgViewOptions;
// ...

try (Viewer viewer = new Viewer("Documents.zip")) {
    // Create a JPG image for the top folder and each subfolder in the archive.
    // {0} is replaced with the current page number in the image name.
    JpgViewOptions viewOptions = new JpgViewOptions("output_{0}.jpg");
    // Set width and height.
    viewOptions.setWidth(950);
    viewOptions.setHeight(550);
    viewer.view(viewOptions);
}

Obtain information about folders in an archive file

Follow the steps below to obtain information about folders contained in an archive file. You can use this information to specify which folder to display in the output file.

  1. Create a ViewInfoOptions instance for a specific view.
  2. Call the Viewer.getViewInfo method, pass the ViewInfoOptions instance to this method as a parameter, and cast the returned object to the ArchiveViewInfo type.
  3. Use the ArchiveViewInfo.getFolders method to obtain the lists of folders in the archive file.
import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.ViewInfoOptions;
import com.groupdocs.viewer.results.ArchiveViewInfo;
import com.groupdocs.viewer.results.ViewInfo;

public class GetViewInfoForArchiveFile {

    /**
     * This example demonstrates how to get view info for Archive files.
     */
    public static void run() {
        try (Viewer viewer = new Viewer("sample.zip")) {
            ViewInfo viewInfo = viewer.getViewInfo(ViewInfoOptions.forHtmlView());

            System.out.println("File type: " + viewInfo.getFileType());
            System.out.println("Pages count: " + viewInfo.getPages().size());
            System.out.println("Folders: ");
            System.out.println(" - /");

            String rootFolder = "";
            readFolders(viewer, rootFolder);
        }

        System.out.println("\nView info retrieved successfully.");
    }

    private static void readFolders(Viewer viewer, String folder) {
        ViewInfoOptions viewInfoOptions = ViewInfoOptions.forHtmlView();
        viewInfoOptions.getArchiveOptions().setFolder(folder);

        ArchiveViewInfo viewInfo = (ArchiveViewInfo) viewer.getViewInfo(viewInfoOptions);

        for (String subFolder : viewInfo.getFolders()) {
            System.out.println(" - " + subFolder);

            readFolders(viewer, subFolder);
        }
    }
}

The following image shows a sample console output:

Get information about an archive file

GroupDocs.Viewer also allows you to list and extract all files contained in the archive. Refer to the following help topics for details:

Render a specific folder

When you convert an archive file to HTML, PDF, or image format, GroupDocs.Viewer renders items from all folders contained in the archive. If you need to render items from a specific folder, use the ArchiveOptions.setFolder method of one of the following classes (depending on the output file format):

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
// ...

HtmlViewOptions viewOptions = HtmlViewOptions.forEmbeddedResources("page_{0}.html");
viewOptions.getArchiveOptions().setFolder("Documents/CAD files");

try (Viewer viewer = new Viewer("Documents.zip")) {
    viewer.view(viewOptions);
}

Specify the archive file name

When rendering an archive file, GroupDocs.Viewer displays the archive file name in the header of each page. If you need to change or hide this name, use the ArchiveOptions.setFileName method for a target view. You can set this option to one of the following values:

  • FileName.SOURCE— Returns the name of the source file (this name is used by default).
  • FileName.EMPTY—Specifies an empty name. Use this value to hide the archive file name in the output file.
  • A FileName instance with a custom name you want to display in the output file.

The following code snippet demonstrates how to use a custom name when rendering an archive file to HTML:

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
// ...

try (Viewer viewer = new Viewer("Documents.zip")) {
   HtmlViewOptions viewOptions = HtmlViewOptions.forEmbeddedResources("page_{0}.html");
    viewOptions.getArchiveOptions().setFileName(new FileName("Sample Files"));

    viewer.view(viewOptions);
}

The image below demonstrates the result.

Set a custom archive name