|
|
@@ -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);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
}
|