49 lines
1.3 KiB
Java
49 lines
1.3 KiB
Java
![]() |
package net.droidtech.utils;
|
||
|
|
||
|
import java.io.ByteArrayInputStream;
|
||
|
import java.io.ByteArrayOutputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.util.zip.GZIPInputStream;
|
||
|
import java.util.zip.GZIPOutputStream;
|
||
|
|
||
|
public class GZIP {
|
||
|
|
||
|
private GZIP(){
|
||
|
|
||
|
}
|
||
|
public static byte[] compress(byte[] data) {
|
||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||
|
GZIPOutputStream gzip;
|
||
|
try {
|
||
|
gzip = new GZIPOutputStream(out);
|
||
|
gzip.write(data);
|
||
|
gzip.close();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
return out.toByteArray();
|
||
|
}
|
||
|
|
||
|
public static byte[] decompress(byte[] bytes) {
|
||
|
if (bytes == null || bytes.length == 0) {
|
||
|
return null;
|
||
|
}
|
||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||
|
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||
|
try {
|
||
|
GZIPInputStream ungzip = new GZIPInputStream(in);
|
||
|
byte[] buffer = new byte[256];
|
||
|
int n;
|
||
|
while ((n = ungzip.read(buffer)) >= 0) {
|
||
|
out.write(buffer, 0, n);
|
||
|
}
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
return out.toByteArray();
|
||
|
}
|
||
|
|
||
|
}
|