XAR Script Service

Last modified by Eleni Cojocariu on 2026/08/21 19:17

Reference

The xar Script Service exposes the XAR API to wiki pages as $services.xar. It never throws: a call that fails returns null and stores its exception, which getLastError() then returns.

MethodReturnsDescription
getXarPackage(file)XarPackageThe archive read from a java.io.File.
getXarPackage(stream, close)XarPackageThe archive read from an InputStream, which is the form a script uses because an attachment provides one. close says whether that stream is closed once the archive has been read.
getLastError()ExceptionThe exception thrown by the previous call, or null when it succeeded.
isXARExportAvailable()booleanWhether the instance can export in XAR format at all.

Package

The XarPackage returned by getXarPackage holds the package information of the descriptor and one entry per Page file.

PropertyReturnsDescription
entriesCollection<XarEntry>One entry per Page file found in the archive.
packageFilesCollection<XarEntry>The entries the descriptor lists, which is the same set unless the archive and its descriptor disagree.
packageNameStringThe name element of the descriptor.
packageDescriptionStringIts description element.
packageLicenseStringIts licence element.
packageAuthorStringIts author element.
packageVersionStringIts version element.
packageBackupPackbooleanWhether the archive is marked as a backup package.
packagePreserveVersionbooleanWhether the Page files are meant to carry their history.
packageExtensionIdStringThe Extension the archive holds, null when it holds none.
getEntry(reference)XarEntryThe entry of one Page, by its LocalDocumentReference.

Entry

A XarEntry is a LocalDocumentReference, so it can be passed anywhere a Page reference is expected, and $services.model.serialize($entry, 'local') turns it into Sandbox.TestPage2.

PropertyReturnsDescription
documentNameStringThe name of the Page.
localeLocaleThe locale of the Page file, empty for the default one.
entryNameStringThe path of the file inside the archive, as in Sandbox/TestPage2.xml.
entryTypeStringThe entry type, null when the entry has none.
spaceNameStringDeprecated, and meaningless for a nested Page: the space of the Page.
defaultActionintDeprecated: the defaultAction attribute of the entry.

Examples

List the Pages of an archive attached to the current Page:

{{velocity}}
#set ($xar = $services.xar.getXarPackage($doc.getAttachment('backup.xar').contentInputStream, true))
#foreach ($entry in $xar.entries)
* $services.model.serialize($entry, 'local') ($entry.entryName)
#end
{{/velocity}}

Show the package information of that archive, telling a read that failed from a file that is not an archive at all:

{{velocity}}
#set ($xar = $services.xar.getXarPackage($doc.getAttachment('backup.xar').contentInputStream, true))
#if (!$xar)
  Reading the archive failed: $services.xar.lastError.message
#elseif ($xar.entries.isEmpty())
  The attachment holds no Page, so it is not a XAR archive.
#else
  **$xar.packageName** by $xar.packageAuthor, version $xar.packageVersion, $xar.entries.size() Page(s).
#end
{{/velocity}}

FAQ

Why does reading a file that is not an archive report no error?

Because a file with no ZIP entries is read as an archive holding no Page: the call succeeds and getLastError() stays null, so it is entries that has to be checked.

Does the Script Service check any right?

No, it only reads the archive and never touches the wiki, so the right to run a script in the Page is the only requirement.

Related

Get Connected