Test.java
3.22 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.bsth.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
public class Test {
/**
*
* @Title: pack
* @Description: 将一组文件打成tar包
* @param sources 要打包的原文件数组
* @param target 打包后的文件
* @return File 返回打包后的文件
* @throws
*/
public static File pack(File[] sources, File target){
FileOutputStream out = null;
try {
out = new FileOutputStream(target);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
TarArchiveOutputStream os = new TarArchiveOutputStream(out);
for (File file : sources) {
try {
os.putArchiveEntry(new TarArchiveEntry(file));
IOUtils.copy(new FileInputStream(file), os);
os.closeArchiveEntry();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return target;
}
/**
*
* @Title: compress
* @Description: 将文件用gzip压缩
* @param source 需要压缩的文件
* @return File 返回压缩后的文件
* @throws
*/
public static File compress(File source) {
File target = new File(source.getName() + ".gz");
FileInputStream in = null;
GZIPOutputStream out = null;
try {
in = new FileInputStream(source);
out = new GZIPOutputStream(new FileOutputStream(target));
byte[] array = new byte[1024];
int number = -1;
System.out.println( in.read(array, 0, array.length) != -1);
while((number = in.read(array, 0, array.length)) != -1) {
out.write(array, 0, number);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if(in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
if(out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
return target;
}
public static void main(String[] args) {
/* File[] sources = new File[] {new File("D:/test.txt")};
File target = new File("release_package.tar");
File targetFile = pack(sources, target);
FTPClientUtils clientUtils = new FTPClientUtils();
clientUtils.testUpLoadFromDisk(targetFile,targetFile.getName());
File gzFile = compress(pack(sources, target));
clientUtils.testUpLoadFromDisk(gzFile,gzFile.getName());*/
}
}