package com.dc.util; |
|
import java.io.ByteArrayOutputStream; |
import java.io.IOException; |
import java.util.zip.Deflater; |
import java.util.zip.Inflater; |
|
import org.apache.commons.codec.binary.Base64; |
|
/* ****************** 类说明 ********************* |
* class : ZLibUtils |
* @author : ncc |
* create time : 2017-12-20 下午03:15:56 |
* @version : 1.0 |
* description : |
* @see : |
* ************************************************/ |
public abstract class ZLibUtils { |
|
/* ******************************************** |
* method name : compress |
* description : 压缩方法 |
* @return : byte[] 压缩后的数据 |
* @param : @param data 待压缩数据 |
* @param : @return |
* modified : ncc , 2017-12-20 |
* @see : |
* ********************************************/ |
public static byte [] compress( byte [] data) { |
byte [] output = new byte [ 0 ]; |
|
Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION); |
|
compresser.reset(); |
compresser.setInput(data); |
compresser.finish(); |
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); |
try { |
byte [] buf = new byte [ 1024 ]; |
while (!compresser.finished()) { |
int i = compresser.deflate(buf); |
bos.write(buf, 0 , i); |
} |
output = bos.toByteArray(); |
} catch (Exception e) { |
output = data; |
e.printStackTrace(); |
} finally { |
try { |
bos.close(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
compresser.end(); |
return output; |
} |
|
/* ******************************************** |
* method name : compressData |
* description : 字符串先压缩,后Base64编码 |
* @return : String |
* @param : @param data |
* @param : @return |
* @param : @throws Exception |
* modified : ncc , 2017-12-20 |
* @see : |
* ********************************************/ |
public static String compressData(String data) throws Exception{ |
return new String(getenBASE64inCodec(ZLibUtils.compress(data.getBytes()))); |
} |
|
/* ******************************************** |
* method name : decompress |
* description : 解压缩 |
* @return : byte[] 解压缩后的数据 |
* @param : @param data 待解压的数据 |
* @param : @return |
* modified : ncc , 2017-12-20 |
* @see : |
* ********************************************/ |
public static byte [] decompress( byte [] data) { |
byte [] output = new byte [ 0 ]; |
|
Inflater decompresser = new Inflater(); |
decompresser.reset(); |
decompresser.setInput(data); |
|
ByteArrayOutputStream o = new ByteArrayOutputStream(data.length); |
try { |
byte [] buf = new byte [ 1024 ]; |
while (!decompresser.finished()) { |
int i = decompresser.inflate(buf); |
o.write(buf, 0 , i); |
} |
output = o.toByteArray(); |
} catch (Exception e) { |
output = data; |
e.printStackTrace(); |
} finally { |
try { |
o.close(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
|
decompresser.end(); |
return output; |
} |
|
/* ******************************************** |
* method name : decompressData |
* description : 解压缩字符串,先进行Base64解码,在解压 |
* @return : String |
* @param : @param encdata |
* @param : @return |
* modified : ncc , 2017-12-20 |
* @see : |
* ********************************************/ |
public static String decompressData(String encdata) { |
return new String(ZLibUtils.decompress(getdeBASE64inCodec(encdata))); |
} |
|
/* ******************************************** |
* method name : getenBASE64inCodec |
* description : 对数组进行Base64 encode |
* @return : String |
* @param : @param b |
* @param : @return |
* modified : ncc , 2017-12-20 |
* @see : |
* ********************************************/ |
public static String getenBASE64inCodec( byte [] b) { |
if (b == null ) |
return null ; |
return new String(( new Base64()).encode(b)); |
} |
|
/* ******************************************** |
* method name : getdeBASE64inCodec |
* description : 对字符串进行Base64 decode |
* @return : byte[] |
* @param : @param s |
* @param : @return |
* modified : ncc , 2017-12-20 |
* @see : |
* ********************************************/ |
public static byte [] getdeBASE64inCodec(String s) { |
if (s == null ) |
return null ; |
return new Base64().decode(s.getBytes()); |
} |
|
public static void main(String[] args) throws Exception{ |
|
StringBuilder batchContent = new StringBuilder(); |
batchContent.append( "start," ).append( "100" ).append( "|" ); |
for ( int i = 1 ; i <= 100 ; i ++) { |
batchContent.append( "123456#" ).append( "0000#" ).append( "10" ).append( "#" ).append( "110101198001010038" ).append( "#" ).append( "622105010755901123" ).append( "#" ); |
if (i < 100 ) { |
batchContent.append( "," ); |
} else { |
batchContent.append( "|end" ); |
} |
} |
|
System.out.println( "压缩前的长度:" + batchContent.length() + ",压缩前为:" + batchContent); |
String compressStr = ZLibUtils.compressData(batchContent.toString()); |
System.out.println( "压缩后的长度:" + compressStr.length() + ",压缩前为:" + compressStr); |
String decompressStr = ZLibUtils.decompressData(compressStr.toString()); |
System.out.println( "解压后的长度:" + decompressStr.length() + ",解压后为:" + decompressStr); |
} |
} |