From 1d9ac51db01b654f2c97d9fd3057b7b0ed716b91 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 3 Dec 2012 17:06:28 -0500 Subject: [PATCH] Simplified archive generation (issue-174) --- src/com/gitblit/utils/CompressionUtils.java | 88 ++++++++------------- 1 file changed, 32 insertions(+), 56 deletions(-) diff --git a/src/com/gitblit/utils/CompressionUtils.java b/src/com/gitblit/utils/CompressionUtils.java index 7b0d0471..41451906 100644 --- a/src/com/gitblit/utils/CompressionUtils.java +++ b/src/com/gitblit/utils/CompressionUtils.java @@ -15,27 +15,24 @@ */ package com.gitblit.utils; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.compressors.CompressorException; import org.apache.commons.compress.compressors.CompressorStreamFactory; -import org.apache.commons.compress.utils.IOUtils; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileMode; -import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.MutableObjectId; import org.eclipse.jgit.lib.ObjectLoader; +import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.revwalk.RevBlob; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.TreeWalk; @@ -99,37 +96,35 @@ public class CompressionUtils { RevWalk rw = new RevWalk(repository); TreeWalk tw = new TreeWalk(repository); try { + tw.reset(); tw.addTree(commit.getTree()); - ZipOutputStream zos = new ZipOutputStream(os); + ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setComment("Generated by Gitblit"); if (!StringUtils.isEmpty(basePath)) { PathFilter f = PathFilter.create(basePath); tw.setFilter(f); } tw.setRecursive(true); + MutableObjectId id = new MutableObjectId(); + ObjectReader reader = tw.getObjectReader(); + long modified = commit.getAuthorIdent().getWhen().getTime(); while (tw.next()) { - if (tw.getFileMode(0) == FileMode.GITLINK) { + FileMode mode = tw.getFileMode(0); + if (mode == FileMode.GITLINK || mode == FileMode.TREE) { continue; } - ZipEntry entry = new ZipEntry(tw.getPathString()); - entry.setSize(tw.getObjectReader().getObjectSize(tw.getObjectId(0), - Constants.OBJ_BLOB)); - entry.setComment(commit.getName()); - zos.putNextEntry(entry); + tw.getObjectId(id, 0); - ObjectId entid = tw.getObjectId(0); - FileMode entmode = tw.getFileMode(0); - RevBlob blob = (RevBlob) rw.lookupAny(entid, entmode.getObjectType()); - rw.parseBody(blob); + ZipArchiveEntry entry = new ZipArchiveEntry(tw.getPathString()); + entry.setSize(reader.getObjectSize(id, Constants.OBJ_BLOB)); + entry.setComment(commit.getName()); + entry.setUnixMode(mode.getBits()); + entry.setTime(modified); + zos.putArchiveEntry(entry); - ObjectLoader ldr = repository.open(blob.getId(), Constants.OBJ_BLOB); - byte[] tmp = new byte[4096]; - InputStream in = ldr.openStream(); - int n; - while ((n = in.read(tmp)) > 0) { - zos.write(tmp, 0, n); - } - in.close(); + ObjectLoader ldr = repository.open(id); + ldr.copyTo(zos); + zos.closeArchiveEntry(); } zos.finish(); success = true; @@ -250,6 +245,7 @@ public class CompressionUtils { RevWalk rw = new RevWalk(repository); TreeWalk tw = new TreeWalk(repository); try { + tw.reset(); tw.addTree(commit.getTree()); TarArchiveOutputStream tos = new TarArchiveOutputStream(cos); tos.setAddPaxHeadersForNonAsciiNames(true); @@ -259,45 +255,25 @@ public class CompressionUtils { tw.setFilter(f); } tw.setRecursive(true); + MutableObjectId id = new MutableObjectId(); + ObjectReader reader = tw.getObjectReader(); + long modified = commit.getAuthorIdent().getWhen().getTime(); while (tw.next()) { FileMode mode = tw.getFileMode(0); - if (mode == FileMode.GITLINK) { + if (mode == FileMode.GITLINK || mode == FileMode.TREE) { continue; } - ObjectId id = tw.getObjectId(0); + tw.getObjectId(id, 0); - // new entry TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString()); - entry.setSize(tw.getObjectReader().getObjectSize(id, Constants.OBJ_BLOB)); + entry.setSize(reader.getObjectSize(id, Constants.OBJ_BLOB)); - if (FileMode.SYMLINK.equals(mode)) { - // symlink - entry.setMode(mode.getBits()); - - // read the symlink target - ByteArrayOutputStream bs = new ByteArrayOutputStream(); - RevBlob blob = (RevBlob) rw.lookupAny(id, mode.getObjectType()); - rw.parseBody(blob); - ObjectLoader ldr = repository.open(blob.getId(), Constants.OBJ_BLOB); - IOUtils.copy(ldr.openStream(), bs); - entry.setLinkName(bs.toString("UTF-8")); - } else { - // regular file or executable file - entry.setMode(mode.getBits()); - } - entry.setModTime(commit.getAuthorIdent().getWhen()); - + entry.setMode(mode.getBits()); + entry.setModTime(modified); tos.putArchiveEntry(entry); - if (!FileMode.SYMLINK.equals(mode)) { - // write the blob - RevBlob blob = (RevBlob) rw.lookupAny(id, mode.getObjectType()); - rw.parseBody(blob); - ObjectLoader ldr = repository.open(blob.getId(), Constants.OBJ_BLOB); - IOUtils.copy(ldr.openStream(), tos); - } - - // close entry + ObjectLoader ldr = repository.open(id); + ldr.copyTo(tos); tos.closeArchiveEntry(); } tos.finish(); -- 2.39.5