ZIP Explorer Plugin

Last modified by Vincent Massol on 2024/07/05 17:42

cogProvides an API to create links to files inside a ZIP file
TypePlugin
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Standard

Description

Starting XWiki 7.4M2 the VFS API and VFS Tree Macro are replacing this plugin which becomes deprecated and will be removed in future versions of XWiki.

ZIP-related APIs to list content of a ZIP attachments and to intercept XWiki download requests so that it's possible to display contents found inside ZIP files. 

This plugin accepts specially formatted URLs pointing to files inside ZIP files by using the following syntax: http://[...]/download/Document/zipfile.zip/SomeDirectory/SomeFile.txt. In this example, the URL points to the SomeFile.txt file located in a directory named SomeDirectory inside a ZIP file named zipfile.zip and attached to the document named Document.

The following API are provided:

    /**
     * @param document the document containing the ZIP file as an attachment
     * @param attachmentName the name under which the ZIP file is attached in the document
     * @return the list of file entries in the ZIP file attached under the passed attachment name inside the passed
     *         document
     */

   List<String> getFileList(Document document, String attachmentName);

   /**
     * Finds the ZIP attachment with passed name from the passed document matching and parse the ZIP to generate a list
     * of {@link com.xpn.xwiki.objects.classes.ListItem} elements representing a tree view of all directories and files
     * in the ZIP. For example the following zip:
     *
     * <pre>
     * zipfile.zip:
     *   Directory/File.txt
     *   File2.txt
     * </pre>
     *
     * generates the following ListItem list:
     *
     * <pre>
     *
     *   { id = &quot;Directory/&quot;, value = &quot;Directory&quot;, parent = &quot;&quot;}
     *   { id = &quot;Directory/File.txt&quot;, value = &quot;File.txt&quot;, parent = &quot;Directory/&quot;}
     *   { id = &quot;File2.txt&quot;, value = &quot;File2.txt&quot;, parent = &quot;&quot;}
     *
     * </pre>
     *
     * @param document the document containing the ZIP file as an attachment
     * @param attachmentName the name under which the ZIP file is attached in the document
     * @return a tree view list of {@link com.xpn.xwiki.objects.classes.ListItem} elements representing the content of
     *         the ZIP file
     */

   List<ListItem> getFileTreeList(Document document, String attachmentName);

   /**
     * @param document the document containing the ZIP file as an attachment
     * @param attachmentName the name under which the ZIP file is attached in the document
     * @param fileName the filename to concatenate at the end of the attachment URL
     * @return the attachment URL of the passed attachment located in the passed document to which the passed filename
     *         has been suffixed.
     */

   String getFileLink(Document document, String attachmentName, String fileName);

In practice the Zip Explorer plugin intercepts the download action and adds support for the following type of URLs: 

Example 1: Link to an attached Javadoc zip

Imagine that you have a JAR/ZIP file named xwiki-commons-component-api-3.4-javadoc.jar attached to the current page and that it contains some Javadoc (and thus it has an entry page named index.html). You'd create a link to the Javadoc like this:

{{velocity}}
[[Javadoc>>path:$xwiki.zipexplorer.getFileLink($doc, "xwiki-commons-component-api-3.4-javadoc.jar", "index.html")]]
{{/velocity}}

Example 2: List files in an attachment

The following example lists the files in an attached zip file. The file content may be viewed by clicking on the generated links.

{{velocity}}
#set($attachment="sample.zip")
#set($plugin=$xwiki.zipexplorer)
#foreach($file in $plugin.getFileTreeList($doc, $attachment))
 * [[$file.id>>path:$plugin.getFileLink($doc, $attachment, $file.id)]]
#end
{{/velocity}}

Result

zipexplorer.png

If you're using XWiki Syntax 2.0 or earlier you'll need to create the link like this instead:

  * {{html}}<a href="$plugin.getFileLink($doc, $attachment, $file.id)">$file.id</a>{{/html}}

Prerequisites & Installation Instructions

To activate this plugin make sure the class com.xpn.xwiki.plugin.zipexplorer.ZipExplorerPlugin is listed in the plugins property of the xwiki.cfg configuration file.

The id of the plugin is zipexplorer.

Get Connected