package org.dstadler.jgit.porcelain;

/*
   Copyright 2013, 2014 Dominik Stadler

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.dstadler.jgit.helper.CookbookHelper;
import org.eclipse.jgit.api.ArchiveCommand;
import org.eclipse.jgit.api.ArchiveCommand.Format;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;



/**
 * Simple snippet which shows how to package the contents of a branch into an archive file.
 * 
 * @author dominik.stadler at gmx.at
 */
public class CreateArchive {

    /**
     * A custom format for Zip-files, unfortunately JGit does not come with any pre-defined ones
     */
    private static final class ZipArchiveFormat implements Format<ZipOutputStream> {

        public ZipOutputStream createArchiveOutputStream(OutputStream s) throws IOException {
            return new ZipOutputStream(s);
        }

        public void putEntry(ZipOutputStream out, String path, FileMode mode, ObjectLoader loader) throws IOException {
            ZipEntry entry = new ZipEntry(path);
            out.putNextEntry(entry);
            out.write(loader.getBytes());
            out.closeEntry();
        }

        public Iterable<String> suffixes() {
            return Collections.singleton(".zip");
        }
    }

    public static void main(String[] args) throws IOException, GitAPIException {
        Repository repository = CookbookHelper.openJGitCookbookRepository();


        File file = File.createTempFile("test", ".zip");
        try {

            // make the archive format known
            ArchiveCommand.registerFormat("zip", new ZipArchiveFormat());
            try {
                // this is the file that we write the archive to
                OutputStream out = new FileOutputStream(file);
                try {
                    // finally call the ArchiveCommand to write out using the given format
                    new Git(repository).archive()
                            .setTree(repository.resolve("master"))
                            .setFormat("zip")
                            .setOutputStream(out)
                            .call();
                } finally {
                    out.close();
                }
            } finally {
                ArchiveCommand.unregisterFormat("zip");
            }

            System.out.println("Wrote " + file.length() + " bytes to " + file);
        } finally {
            file.delete();
        }

        repository.close();
    }
}

@@ -35,17 +35,17 @@
 n;%0A%0A/*%0A 
- 
+*
  Copyrig
@@ -74,19 +74,22 @@
 Stadler%0A
+ * 
 %0A 
- 
+*
  License
@@ -146,17 +146,17 @@
 nse%22);%0A 
- 
+*
  you may
@@ -214,17 +214,17 @@
 cense.%0A 
- 
+*
  You may
@@ -256,21 +256,22 @@
 ense at%0A
-%0A    
+ * %0A *
  http://
@@ -306,19 +306,22 @@
 NSE-2.0%0A
+ * 
 %0A 
- 
+*
  Unless 
@@ -382,17 +382,17 @@
 ftware%0A 
- 
+*
  distrib
@@ -451,17 +451,17 @@
 BASIS,%0A 
- 
+*
  WITHOUT
@@ -527,17 +527,17 @@
 plied.%0A 
- 
+*
  See the
@@ -598,17 +598,17 @@
 ns and%0A 
- 
+*
  limitat
@@ -1828,32 +1828,119 @@
 s IOException %7B%0A
+            // loader is null for directories...%0A            if (loader != null) %7B%0A    
             ZipE
@@ -1980,24 +1980,28 @@
             
+    
 out.putNextE
@@ -2021,24 +2021,28 @@
             
+ 
+   
 out.write(lo
@@ -2055,24 +2055,28 @@
 etBytes());%0A
+    
             
@@ -2089,24 +2089,38 @@
 oseEntry();%0A
+            %7D%0A
         %7D%0A%0A 
@@ -2451,27 +2451,8 @@
 %22);%0A
-        try %7B%0A%0A    
     
@@ -2488,36 +2488,32 @@
 t known%0A        
-    
 ArchiveCommand.r
@@ -2558,36 +2558,32 @@
 mat());%0A        
-    
 try %7B%0A          
@@ -2584,20 +2584,16 @@
         
- 
-   
 // this 
@@ -2645,20 +2645,16 @@
         
-    
 OutputSt
@@ -2700,38 +2700,30 @@
             
-  
-  
 try %7B%0A
-    
             
@@ -2797,20 +2797,16 @@
  format%0A
-    
         
@@ -2863,28 +2863,24 @@
             
-    
 .setTree(rep
@@ -2926,28 +2926,24 @@
             
-    
 .setFormat(%22
@@ -2968,28 +2968,24 @@
             
- 
-   
 .setOutputSt
@@ -3018,20 +3018,16 @@
         
-    
 .call();
@@ -3031,36 +3031,32 @@
 ();%0A            
-  
-  
 %7D finally %7B%0A    
@@ -3043,36 +3043,32 @@
     %7D finally %7B%0A
-    
                 
@@ -3088,34 +3088,26 @@
             
-   
- 
 %7D%0A
-    
         %7D fi
@@ -3106,36 +3106,32 @@
     %7D finally %7B%0A
-    
             Arch
@@ -3170,35 +3170,27 @@
 %22);%0A        
- 
-   %7D%0A%0A    
+%7D%0A%0A
         Syst
@@ -3256,65 +3256,8 @@
 le);
-%0A        %7D finally %7B%0A            file.delete();%0A        %7D
 %0A%0A  

Fix CreateArchive to not try to access null loader for directories, also keep temporary file to show results here