public static boolean isCompressed(final byte[] compressed) {
return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
}
public static byte[] compress(final String str) throws IOException {
if ((str == null) || (str.length() == 0)) {
return null;
}
ByteArrayOutputStream obj = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(obj);
gzip.write(str.getBytes("UTF-8"));
gzip.close();
//System.out.println(obj.toByteArray()[1]);
return obj.toByteArray();
}
public static String packetDecompress(final String msg){
//byte[] bcdz = compress("asdasdz");
//System.out.println(decompress(bcdz));
//System.out.println(bcdz[0]);
//String ad = "{\"type\":\"Buffer\",\"data\":[31,139,8,0,0,0,0,0,0,0,99,9,73,18,16,22,101,9,73,150,20,148,100,9,73,18,73,73,66,99,139,37,139,130,216,146,169,44,68,171,140,70,85,74,138,162,81,27,6,129,13,20,184,108,184,42,194,46,1,0,199,90,197,78,61,3,0,0]}";
//ad = "[31,-117,8,0,0,0,0,0,0,0,75,44,78,73,44,78,-87,2,0,35,70,-59,53,7,0,0,0,0,0,0,0,0]";
//ad = "[31,139,8,0,0,0,0,0,0,0,99,9,73,18,16,22,101,9,73,18,145,76,141,102,129,114,146,37,5,37,1,247,124,130,131,25,0,0,0]";
String[] tmp;
int lastPos = 0, pos = 0;
pos = msg.indexOf("[");
lastPos = msg.indexOf("]");
String msg2 = msg.substring(pos+1, lastPos);
//System.out.println(ad);
tmp = msg2.split(",");
byte[] bytes = new byte[tmp.length];
for(int i=0;i<tmp.length;i++){
if(Integer.valueOf(tmp[i]) > 127) bytes[i] = (byte) (Integer.valueOf(tmp[i])+256); // -128 ~ 127 자바 byte 타입 범위
else bytes[i] = Byte.valueOf(tmp[i]);
}
try {
return XorDecryption(decompress(bytes));
} catch (IOException e) {
e.printStackTrace();
}
return "NULL";
/*System.out.println(decompress(bytes));
System.out.println(XorDecryption(decompress(bytes)));
System.out.println(tmp.length);*/
}
public static String decompress(final byte[] compressed) throws IOException {
String outStr = "";
if ((compressed == null) || (compressed.length == 0)) {
return "";
}
if (isCompressed(compressed)) {
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
outStr += line;
}
} else {
//outStr = new String(compressed);
outStr = "HACKING";
}
return outStr;
}