diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/FSRepoStore.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/FSRepoStore.java index 7ab8a95e64..a12dbf2ec2 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/FSRepoStore.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/FSRepoStore.java @@ -60,7 +60,7 @@ public class FSRepoStore implements RepoStore { @Override public InputStream bzip2Project(String projectName) throws IOException { Preconditions.checkArgument(Project.isValidProjectName(projectName)); - return Tar.tar(getDotGitForProject(projectName)); + return Tar.bz2.zip(getDotGitForProject(projectName)); } @Override @@ -76,7 +76,7 @@ public class FSRepoStore implements RepoStore { ) throws IOException { Preconditions.checkArgument(Project.isValidProjectName(projectName)); Preconditions.checkState(getDirForProject(projectName).mkdirs()); - Tar.untar(dataStream, getDirForProject(projectName)); + Tar.bz2.unzip(dataStream, getDirForProject(projectName)); } private File getDirForProject(String projectName) { diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Tar.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Tar.java index ec8083aa40..1326aedc5c 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Tar.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/util/Tar.java @@ -4,6 +4,8 @@ import com.google.api.client.repackaged.com.google.common.base.Preconditions; import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; +import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.io.output.ByteArrayOutputStream; @@ -17,18 +19,76 @@ import java.nio.file.Paths; */ public class Tar { + public static class bz2 { + + public static InputStream zip( + File fileOrDir + ) throws IOException { + ByteArrayOutputStream target = new ByteArrayOutputStream(); + try (OutputStream bzip2 = new BZip2CompressorOutputStream(target)) { + tarTo(fileOrDir, bzip2); + } + return target.toInputStream(); + } + + public static void unzip( + InputStream tarbz2, + File parentDir + ) throws IOException { + try (InputStream tar = new BZip2CompressorInputStream(tarbz2)) { + untar(tar, parentDir); + } + } + + } + private Tar() {} public static InputStream tar(File fileOrDir) throws IOException { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - TarArchiveOutputStream tout = new TarArchiveOutputStream(bout); - addTarEntry( - tout, - Paths.get(fileOrDir.getParentFile().getAbsolutePath()), - fileOrDir - ); - tout.close(); - return new ByteArrayInputStream(bout.toByteArray()); + ByteArrayOutputStream target = new ByteArrayOutputStream(); + tarTo(fileOrDir, target); + return target.toInputStream(); + } + + public static void tarTo( + File fileOrDir, + OutputStream target + ) throws IOException { + try (TarArchiveOutputStream tout = new TarArchiveOutputStream(target)) { + addTarEntry( + tout, + Paths.get(fileOrDir.getParentFile().getAbsolutePath()), + fileOrDir + ); + tout.close(); + } + } + + public static void untar( + InputStream tar, + File parentDir + ) throws IOException { + TarArchiveInputStream tin = new TarArchiveInputStream(tar); + ArchiveEntry e; + while ((e = tin.getNextEntry()) != null) { + File f = new File(parentDir, e.getName()); + f.setLastModified(e.getLastModifiedDate().getTime()); + f.getParentFile().mkdirs(); + if (e.isDirectory()) { + f.mkdir(); + continue; + } + long size = e.getSize(); + Preconditions.checkArgument( + size > 0 && size < Integer.MAX_VALUE, + "file too big: tarTo should have thrown an IOException" + ); + try (OutputStream out = new FileOutputStream(f)) { + /* TarInputStream pretends each + entry's EOF is the stream's EOF */ + IOUtils.copy(tin, out); + } + } } private static void addTarEntry( @@ -79,28 +139,4 @@ public class Tar { tout.closeArchiveEntry(); } - public static void untar(InputStream tar, File parentDir) throws IOException { - TarArchiveInputStream tin = new TarArchiveInputStream(tar); - ArchiveEntry e; - while ((e = tin.getNextEntry()) != null) { - File f = new File(parentDir, e.getName()); - f.setLastModified(e.getLastModifiedDate().getTime()); - f.getParentFile().mkdirs(); - if (e.isDirectory()) { - f.mkdir(); - continue; - } - long size = e.getSize(); - Preconditions.checkArgument( - size > 0 && size < Integer.MAX_VALUE, - "file too big: tar should have thrown an IOException" - ); - try (OutputStream out = new FileOutputStream(f)) { - /* TarInputStream pretends each - entry's EOF is the stream's EOF */ - IOUtils.copy(tin, out); - } - } - } - } diff --git a/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/util/TarTest.java b/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/util/TarTest.java index 74b6fcf109..1ce147e5d8 100644 --- a/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/util/TarTest.java +++ b/services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/util/TarTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertTrue; public class TarTest { private File testDir; + private File tmpDir; @Before public void setup() throws IOException { @@ -29,17 +30,23 @@ public class TarTest { "src/test/resources/uk/ac/ic/wlgitbridge/util/TarTest/testdir" ); FileUtils.copyDirectory(resdir.toFile(), testDir); + tmpDir = tmpFolder.newFolder(); } @Test public void tarAndUntarProducesTheSameResult() throws IOException { InputStream tar = Tar.tar(testDir); - TemporaryFolder tmpF = new TemporaryFolder(); - tmpF.create(); - File parentDir = tmpF.newFolder(); - Tar.untar(tar, parentDir); - File untarred = new File(parentDir, "testdir"); + Tar.untar(tar, tmpDir); + File untarred = new File(tmpDir, "testdir"); assertTrue(Files.contentsAreEqual(testDir, untarred)); } + @Test + public void tarbz2AndUntarbz2ProducesTheSameResult() throws IOException { + InputStream tarbz2 = Tar.bz2.zip(testDir); + Tar.bz2.unzip(tarbz2, tmpDir); + File unzipped = new File(tmpDir, "testdir"); + assertTrue(Files.contentsAreEqual(testDir, unzipped)); + } + } \ No newline at end of file