package org.exist.backup;

import java.io.*;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;

/**
 * Implementation of BackupWriter that writes to a zip file.
 */
public class ZipWriter implements BackupWriter {

    private String currentPath;
    private ZipOutputStream out;
    private StringWriter contents;

    public ZipWriter(String zipFile, String collection) throws IOException {
        File file = new File(zipFile);
        out = new ZipOutputStream(new FileOutputStream(file));
        currentPath = collection;
    }

    public Writer newContents() throws IOException {
        contents = new StringWriter();
        return contents;
    }

    public void closeContents() throws IOException {
        ZipEntry entry = new ZipEntry(currentPath + "/__contents__.xml");
        out.putNextEntry(entry);
        out.write(contents.toString().getBytes("UTF-8"));
        out.closeEntry();
    }

    public OutputStream newEntry(String name) throws IOException {
        ZipEntry entry = new ZipEntry(currentPath + '/' + name);
        out.putNextEntry(entry);
        return out;
    }

    public void closeEntry() throws IOException {
        out.closeEntry();
    }

    public void newCollection(String name) {
        currentPath = currentPath + '/' + name;
    }

    public void closeCollection() {
        int p = currentPath.lastIndexOf('/');
        if (p > 0)
            currentPath = currentPath.substring(0, p);
    }

    public void close() throws IOException {
        out.close();
    }
}
@@ -782,32 +782,43 @@
  = new ZipEntry(
+mkRelative(
 currentPath + %22/
@@ -812,16 +812,17 @@
 rentPath
+)
  + %22/__c
@@ -1074,16 +1074,27 @@
 ipEntry(
+mkRelative(
 currentP
@@ -1088,32 +1088,33 @@
 tive(currentPath
+)
  + '/' + name);%0D
@@ -1613,9 +1613,174 @@
 %0A    %7D%0D%0A
+    %0D%0A    private String mkRelative(String path) %7B%0D%0A    %09if (path.length() %3E 0 && path.charAt(0) == '/')%0D%0A    %09%09return path.substring(1);%0D%0A    %09return path;%0D%0A    %7D%0D%0A
 %7D

Store relative paths when creating a backup in a .zip file.