Steps:
- Create or choose a database (e.g. named 'db')
- Store in BaseX a raw zip file (e.g. 'alpha/bravo/charlie/delta.zip'). My zip file has files in folders.
- Choose a target name that also has a ".zip" extension to similar to 'include name of archive in path' behavior. Note, it should not be the same as the raw zip file stored (e.g. 'delta.zip')
- Run the following in the BaseX GUI client - it will work
- Repeat 1 and 2 using the BaseX Java Client example - it does not work
GUI CLIENT - DOES WORK
========================================
let $database := 'db'
let $extractTo := 'target.zip'
let $archivePath := 'alpha/bravo/charlie/delta.zip'
let $archive := db:retrieve($database, $archivePath)
let $basePath := replace($archivePath, '[^/]+$', '') || $extractTo
let $entries := archive:entries($archive)
let $contents := archive:extract-binary($archive)
return for-each-pair($entries, $contents, %updating function($entry, $content) {
let $target := $basePath || '/' || $entry/text()
return try {
db:store($database, $target, $content)
} catch * {
()
}
})
JAVA - DOES NOT WORK:
===============================================
// Usage: Create a BaseXClient object and call this method.
// For example:
// ExplodeArchive("db", "target.zip", "alpha/bravo/charlie/delta.zip", ..)
private static void ExplodeArchive(final String database, String extractTo, String archivePath, BaseXClient session) throws IOException {
try {
String queryString =
"let $database := '" + database +"' " +
"let $extractTo := '" + extractTo + "' " +
"let $archivePath := '"+ archivePath + "' " +
"let $archive := db:retrieve($database, $archivePath) " +
"let $basePath := replace($archivePath, '[^/]+$', '') || $extractTo " +
"let $entries := archive:entries($archive) " +
"let $contents := archive:extract-binary($archive) " +
"for $entry at $pos in $entries " +
"let $content := $contents[$pos] " +
"let $target := $basePath || '/' || $entry/text() " +
"return db:store($database, "+ "$target, $content)";
final BaseXClient.Query query = session.query(queryString);
query.execute();
} finally {
session.close();
}
}