Using Java to unpack and repack a jar results in a broken jar file
===

I've been debugging some jar-update snippets and I came across a zip/unzip combination that should be reversing the other, but when I tried it on a jar it failed even though all the files are present. Can anyone figure out where this is going wrong?

SSCCE (no need to compile): [Download here!][1]

    package jarsscce;
    
    import java.io.*;
    import java.util.*;
    import java.util.jar.*;
    import java.util.zip.*;
    
    public class JarSSCCE {
    
        public static void main(String[] args) throws IOException {
            File testJar = new File("test.jar");
            File testDir = new File("test");
            File jarPack = new File("packed_test.jar");
    
            unpack(testJar, testDir);
            jar(testDir, new JarOutputStream(new FileOutputStream(jarPack)));
        }
    
        private static void jar(File source, JarOutputStream target) throws IOException {
            BufferedInputStream in = null;
            try {
                if (source.isDirectory()) {
                    String name = source.getPath().replace("\\", "/");
                    if (!name.isEmpty()) {
                        if (!name.endsWith("/")) name.concat("/");
                        JarEntry entry = new JarEntry(name);
                        entry.setTime(source.lastModified());
                        target.putNextEntry(entry);
                        target.closeEntry();
                    }
                    for (File nestedFile : source.listFiles()) jar(nestedFile, target);
                    return;
                }
                JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
                entry.setTime(source.lastModified());
                target.putNextEntry(entry);
                in = new BufferedInputStream(new FileInputStream(source));
    
                byte[] buffer = new byte[1 << 12];
                int read;
                while ((read = in.read(buffer, 0, buffer.length)) != -1) {
                    target.write(buffer, 0, read);
                }
                target.closeEntry();
            } finally {
                if (in != null) in.close();
            }
        }
    
        public static void unpack(File zip, File extractTo) throws IOException {
            try (ZipFile archive = new ZipFile(zip)) {
                Enumeration e = archive.entries();
                while (e.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry) e.nextElement();
                    File file = new File(extractTo, entry.getName());
                    if (entry.isDirectory() && !file.exists()) {
                        file.mkdirs();
                    } else {
                        if (!file.getParentFile().exists()) {
                            file.getParentFile().mkdirs();
                        }
                        try (InputStream in = archive.getInputStream(entry)) {
                            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                            byte[] buffer = new byte[1 << 13];
                            int read;
                            while ((read = in.read(buffer)) != -1) {
                                out.write(buffer, 0, read);
                            }
                            out.close();
                        }
                    }
                }
            }
        }
    }


  [1]: http://dev.fhmp.net/jar_sscce.zip