|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.proteomecommons.tranche.util; |
|
17 |
| |
|
18 |
| import SevenZip.LzmaAlone; |
|
19 |
| import java.io.ByteArrayInputStream; |
|
20 |
| import java.io.ByteArrayOutputStream; |
|
21 |
| import java.io.File; |
|
22 |
| import java.io.FileInputStream; |
|
23 |
| import java.io.FileOutputStream; |
|
24 |
| import java.io.IOException; |
|
25 |
| import java.util.List; |
|
26 |
| import java.util.zip.GZIPInputStream; |
|
27 |
| import java.util.zip.GZIPOutputStream; |
|
28 |
| import org.apache.tools.bzip2.CBZip2InputStream; |
|
29 |
| import org.apache.tools.bzip2.CBZip2OutputStream; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| public class CompressionUtil { |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
1
| public static final byte[] gzipCompress(byte[] dataBytes, byte[] padding) throws IOException {
|
|
41 |
| |
|
42 |
1
| ByteArrayInputStream fis = null;
|
|
43 |
1
| ByteArrayOutputStream fos = null;
|
|
44 |
1
| GZIPOutputStream gos = null;
|
|
45 |
1
| try {
|
|
46 |
| |
|
47 |
1
| fis = new ByteArrayInputStream(dataBytes);
|
|
48 |
1
| fos = new ByteArrayOutputStream();
|
|
49 |
1
| gos = new GZIPOutputStream(fos);
|
|
50 |
| |
|
51 |
1
| IOUtil.getBytes(fis, gos);
|
|
52 |
1
| gos.write(padding);
|
|
53 |
| } finally { |
|
54 |
1
| IOUtil.safeClose(fis);
|
|
55 |
1
| IOUtil.safeClose(gos);
|
|
56 |
1
| IOUtil.safeClose(fos);
|
|
57 |
| } |
|
58 |
| |
|
59 |
1
| return fos.toByteArray();
|
|
60 |
| } |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
328
| public static final byte[] gzipDecompress(byte[] dataBytes) throws IOException {
|
|
66 |
| |
|
67 |
328
| ByteArrayInputStream fis = null;
|
|
68 |
328
| ByteArrayOutputStream fos = null;
|
|
69 |
328
| GZIPInputStream gis = null;
|
|
70 |
328
| try {
|
|
71 |
| |
|
72 |
328
| fis = new ByteArrayInputStream(dataBytes);
|
|
73 |
328
| fos = new ByteArrayOutputStream();
|
|
74 |
328
| gis = new GZIPInputStream(fis);
|
|
75 |
| |
|
76 |
328
| IOUtil.getBytes(gis, fos);
|
|
77 |
| } finally { |
|
78 |
328
| IOUtil.safeClose(gis);
|
|
79 |
328
| IOUtil.safeClose(fis);
|
|
80 |
328
| IOUtil.safeClose(fos);
|
|
81 |
| } |
|
82 |
| |
|
83 |
328
| return fos.toByteArray();
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
2
| public static final File gzipCompress(File input) throws IOException {
|
|
91 |
| |
|
92 |
2
| File gzip = TempFileUtil.createTemporaryFile(".gzip");
|
|
93 |
| |
|
94 |
2
| FileInputStream fis = null;
|
|
95 |
2
| FileOutputStream fos = null;
|
|
96 |
2
| GZIPOutputStream gos = null;
|
|
97 |
2
| try {
|
|
98 |
| |
|
99 |
2
| fis = new FileInputStream(input);
|
|
100 |
2
| fos = new FileOutputStream(gzip);
|
|
101 |
2
| gos = new GZIPOutputStream(fos);
|
|
102 |
| |
|
103 |
2
| IOUtil.getBytes(fis, gos);
|
|
104 |
| } finally { |
|
105 |
2
| IOUtil.safeClose(fis);
|
|
106 |
2
| IOUtil.safeClose(gos);
|
|
107 |
2
| IOUtil.safeClose(fos);
|
|
108 |
| } |
|
109 |
| |
|
110 |
2
| return gzip;
|
|
111 |
| } |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
86
| public static final File gzipDecompress(File input) throws IOException {
|
|
117 |
| |
|
118 |
86
| File gzip = TempFileUtil.createTemporaryFile();
|
|
119 |
| |
|
120 |
86
| FileInputStream fis = null;
|
|
121 |
86
| FileOutputStream fos = null;
|
|
122 |
86
| GZIPInputStream gis = null;
|
|
123 |
86
| try {
|
|
124 |
| |
|
125 |
86
| fis = new FileInputStream(input);
|
|
126 |
86
| fos = new FileOutputStream(gzip);
|
|
127 |
86
| gis = new GZIPInputStream(fis);
|
|
128 |
| |
|
129 |
86
| IOUtil.getBytes(gis, fos);
|
|
130 |
| } finally { |
|
131 |
86
| IOUtil.safeClose(gis);
|
|
132 |
86
| IOUtil.safeClose(fis);
|
|
133 |
86
| IOUtil.safeClose(fos);
|
|
134 |
| } |
|
135 |
| |
|
136 |
86
| return gzip;
|
|
137 |
| } |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
1
| public static final File bzip2Compress(File input) throws IOException {
|
|
144 |
| |
|
145 |
1
| File compressed = TempFileUtil.createTemporaryFile();
|
|
146 |
| |
|
147 |
1
| FileInputStream fis = null;
|
|
148 |
1
| FileOutputStream fos = null;
|
|
149 |
1
| CBZip2OutputStream gos = null;
|
|
150 |
1
| try {
|
|
151 |
| |
|
152 |
1
| fis = new FileInputStream(input);
|
|
153 |
1
| fos = new FileOutputStream(compressed);
|
|
154 |
1
| gos = new CBZip2OutputStream(fos);
|
|
155 |
| |
|
156 |
1
| IOUtil.getBytes(fis, gos);
|
|
157 |
| } finally { |
|
158 |
1
| IOUtil.safeClose(fis);
|
|
159 |
1
| IOUtil.safeClose(gos);
|
|
160 |
1
| IOUtil.safeClose(fos);
|
|
161 |
| } |
|
162 |
| |
|
163 |
1
| return compressed;
|
|
164 |
| } |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
1
| public static final File bzip2Decompress(File input) throws IOException {
|
|
170 |
| |
|
171 |
1
| File decompressed = TempFileUtil.createTemporaryFile();
|
|
172 |
| |
|
173 |
1
| FileInputStream fis = null;
|
|
174 |
1
| FileOutputStream fos = null;
|
|
175 |
1
| CBZip2InputStream gis = null;
|
|
176 |
1
| try {
|
|
177 |
| |
|
178 |
1
| fis = new FileInputStream(input);
|
|
179 |
1
| fos = new FileOutputStream(decompressed);
|
|
180 |
1
| gis = new CBZip2InputStream(fis);
|
|
181 |
| |
|
182 |
1
| IOUtil.getBytes(gis, fos);
|
|
183 |
| } finally { |
|
184 |
1
| IOUtil.safeClose(gis);
|
|
185 |
1
| IOUtil.safeClose(fis);
|
|
186 |
1
| IOUtil.safeClose(fos);
|
|
187 |
| } |
|
188 |
| |
|
189 |
1
| return decompressed;
|
|
190 |
| } |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
1
| public static final File lzmaDecompress(File input) throws Exception {
|
|
196 |
| |
|
197 |
1
| File decompressed = TempFileUtil.createTemporaryFile();
|
|
198 |
| |
|
199 |
| |
|
200 |
1
| LzmaAlone.main(new String[] {"d", input.getCanonicalPath(), decompressed.getCanonicalPath()});
|
|
201 |
| |
|
202 |
| |
|
203 |
1
| return decompressed;
|
|
204 |
| } |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
1
| public static final File lzmaCompress(File input) throws Exception {
|
|
210 |
| |
|
211 |
1
| File compressed = TempFileUtil.createTemporaryFile();
|
|
212 |
| |
|
213 |
| |
|
214 |
1
| LzmaAlone.main(new String[] {"e", input.getCanonicalPath(), compressed.getCanonicalPath()});
|
|
215 |
| |
|
216 |
| |
|
217 |
1
| return compressed;
|
|
218 |
| } |
|
219 |
| } |