mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-01 13:21:37 +02:00
Implement bz2 support and test
This commit is contained in:
committed by
Michael Mazour
parent
8c0937511e
commit
25fea8ef58
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user