mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Implement bz2 support and test
This commit is contained in:
parent
8c0937511e
commit
25fea8ef58
3 changed files with 83 additions and 40 deletions
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue