Return JGits git.archive() OutputStream as Enumerator

I’m new to Scala and what I’m trying to do is to zip a git repository via JGit and offer the zipped file as download.
The idea is that there won’t be Zip-File generated beforehand but instead it will be zipped on the fly and transmitted right away. I’ve got it to work without using Jgit and something like this example https://gist.github.com/gre/4058734

However since there is already a git.archive method that writes a zipped repository into an OutputStream, I would like to use it instead of the Solution above.
I’ve tried the following but it didn’t work and I can’t seem to find out why.

  • What differences are in the patches/files created by diff and git diff?
  • How to reject / remove yourself from a (collaborative) GitHub repo?
  • Is Perforce worth it?
  • calculating a git packfile sha1 checksum in java
  • How can I move files & folders using git in Xcode 4?
  • Git clueless — please clarify
  •   ArchiveFormats.registerAll();
    
      var enum = Enumerator.outputStream { os =>   
        git.archive()
          .setTree(git.getRepository.resolve("master"))
          .setFormat("zip")
          .setOutputStream(os)
          .call() 
       }
    
      ArchiveFormats.unregisterAll();
    
    
     Ok.stream(enum >>> Enumerator.eof).withHeaders(
     "Content-Type"->"application/zip", 
     "Content-Disposition"->"attachment; filename=asdf.zip"
    )
    

  • Git: show message when cloning
  • Rebase a merge commit
  • Why this import of huge sources fails on GitHub?
  • How can i configure imported repository path in GitLab to remove root folder prefix?
  • How to remove a git repository from the Repository Browser on Netbeans?
  • Subtle difference with git pull --rebase and git rebase?
  • One Solution collect form web for “Return JGits git.archive() OutputStream as Enumerator”

    The problem lies in the fact that setOutputStream is expecting a ByteArrayOutputStream, not just an outputStream.

    You could put the zip contents into a ByteArrayOutputStream then convert that to an InputStream so you can return the contents.

      ByteArrayOutputStream data= new ByteArrayOutputStream();
    
      git.archive()
          .setTree(git.getRepository.resolve("master"))
          .setFormat("zip")
          .setOutputStream(data)
          .call() 
    
      InputStream inStream = new ByteArrayInputStream(data.toByteArray());
    
      val dataContent: Enumerator[Array[Byte]] = Enumerator.fromStream(inStream)
    
      Ok.stream(dataContent).withHeaders(
         "Content-Type"->"application/zip", 
         "Content-Disposition"->"attachment; filename=asdf.zip"
      )
    

    There are some caveats to watch out for here, the example above shows how to get around the problem in the question but attention needs to be paid to how much in-memory work is being done.

    The documentation here covers examples of sending large amounts of data in chunks.

    There are some notes here about how you can more efficiently convert a ByteArrayOutputStream into a ByteArrayInputStream if you are looking for maximum efficiency.

    Git Baby is a git and github fan, let's start git clone.