/**
   * Zips a given file or directory
   *
   * @param byte[] content to zip
   * @return byte[] zipped content
   * @throws IOException If the zip content can't be created
   */
  private byte[] zipContent(byte[] content) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);

    zos.setComment("jLibrary ZIP archive");
    zos.setMethod(ZipOutputStream.DEFLATED);
    zos.setEncoding("UTF-8");
    zos.setLevel(Deflater.DEFAULT_COMPRESSION);

    // create and initialize a zipentry for it
    ZipEntry entry = new ZipEntry("jlibrary");
    entry.setTime(System.currentTimeMillis());
    zos.putNextEntry(entry);
    zos.write(content);
    zos.closeEntry();

    zos.close();
    baos.close();

    return baos.toByteArray();
  }
  public void exportRepository(Ticket ticket, OutputStream stream)
      throws RepositoryNotFoundException, RepositoryException, SecurityException {

    try {
      javax.jcr.Session session = SessionManager.getInstance().getSession(ticket);
      if (session == null) {
        throw new RepositoryException("Session has expired. Please log in again.");
      }
      javax.jcr.Node root = JCRUtils.getRootNode(session);
      if (!JCRSecurityService.canRead(root, ticket.getUser().getId())) {
        throw new SecurityException(SecurityException.NOT_ENOUGH_PERMISSIONS);
      }

      // Create a temporary file with content
      File tempRoot = File.createTempFile("tmp", "jlib");
      FileOutputStream fos = null;
      try {
        fos = new FileOutputStream(tempRoot);
        session.exportSystemView(JCRUtils.getRootNode(session).getPath(), fos, false, false);
      } finally {
        if (fos != null) {
          fos.close();
        }
      }

      // Will wrap compression around the given stream
      ZipOutputStream zos = null;
      try {
        zos = new ZipOutputStream(stream);
        zos.setComment("jLibrary ZIP archive");
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setEncoding("UTF-8");
        zos.setLevel(Deflater.DEFAULT_COMPRESSION);

        // create and initialize a zipentry for it
        ZipEntry entry = new ZipEntry("jlibrary");
        entry.setTime(System.currentTimeMillis());
        zos.putNextEntry(entry);
        // write header
        String tag = String.valueOf(tempRoot.length()) + "*";
        byte[] header = tag.getBytes();
        zos.write(header);

        // write root
        FileInputStream fis = null;
        try {
          fis = new FileInputStream(tempRoot);
          IOUtils.copy(fis, zos);
        } finally {
          if (fis != null) {
            fis.close();
          }
        }
        // Delete root file
        tempRoot.delete();

        session.exportSystemView(JCRUtils.getSystemNode(session).getPath(), zos, false, false);
      } finally {
        if (zos != null) {
          zos.closeEntry();
          zos.close();
        }
      }

    } catch (PathNotFoundException e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    } catch (javax.jcr.RepositoryException e) {
      logger.error(e.getMessage(), e);
      throw new RepositoryException(e);
    }
  }