forked from striner/javaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressUtilTest.java
More file actions
56 lines (49 loc) · 1.83 KB
/
Copy pathCompressUtilTest.java
File metadata and controls
56 lines (49 loc) · 1.83 KB
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
56
package utils;
import common.utils.CompressUtil;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.junit.Test;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class CompressUtilTest {
@Test
public void test1() throws IOException {
CompressUtil.compressZip("E:\\striner\\测试.txt", "E:\\striner\\测试22.zip");
}
@Test
public void test2() throws IOException {
CompressUtil.compressZip("E:\\striner\\bbb", "E:\\striner\\aaa\\bbb.zip");
}
@Test
public void bzip2() throws IOException {
InputStream in = Files.newInputStream(Paths.get("archive.tar"));
OutputStream fout = Files.newOutputStream(Paths.get("archive.tar.gz"));
BufferedOutputStream out = new BufferedOutputStream(fout);
BZip2CompressorOutputStream bzOut = new BZip2CompressorOutputStream(out);
final byte[] buffer = new byte[2048];
int n = 0;
while (-1 != (n = in.read(buffer))) {
bzOut.write(buffer, 0, n);
}
bzOut.close();
in.close();
}
@Test
public void gzip() throws IOException {
InputStream in = Files.newInputStream(Paths.get("archive.tar"));
OutputStream fout = Files.newOutputStream(Paths.get("archive.tar.gz"));
BufferedOutputStream out = new BufferedOutputStream(fout);
GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out);
final byte[] buffer = new byte[2048];
int n = 0;
while (-1 != (n = in.read(buffer))) {
gzOut.write(buffer, 0, n);
}
gzOut.close();
in.close();
}
}