/
TarExtractor.java
55 lines (44 loc) · 1.43 KB
/
TarExtractor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.meshpoint.anode.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import org.xeustechnologies.jtar.TarEntry;
import org.xeustechnologies.jtar.TarInputStream;
public class TarExtractor {
private File src, dest;
TarExtractor(File src, File dest) {
this.src = src;
this.dest = dest;
}
public void extract() throws IOException {
/* first extract the tar file */
File tarFile = new File(src.getAbsolutePath() + ".tar");
byte[] buf = new byte[1024];
GZIPInputStream zis = null;
zis = new GZIPInputStream(new FileInputStream(src));
FileOutputStream tarfos = new FileOutputStream(tarFile);
int count;
while ((count = zis.read(buf, 0, 1024)) != -1)
tarfos.write(buf, 0, count);
tarfos.close();
zis.close();
/* now unpack the tar */
TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile)));
TarEntry entry;
while((entry = tis.getNextEntry()) != null) {
FileOutputStream fos = new FileOutputStream(new File(dest, entry.getName()));
BufferedOutputStream dest = new BufferedOutputStream(fos);
while((count = tis.read(buf)) != -1)
dest.write(buf, 0, count);
dest.flush();
dest.close();
}
tis.close();
/* delete the tar file */
tarFile.delete();
}
}