Gzip 的使用
在网络传输中,为了减少网络的开支,对传输的数据一般要进行压缩,下面是一种比较常见的压缩方法
import java.util.zip.*;<o:p></o:p>
import java.io.*;<o:p></o:p>
public <st1:rtx>cl</st1:rtx>ass testGzip {<o:p></o:p>
/**<o:p></o:p>
* 将对象转换成字节数组<o:p></o:p>
* @param obje<st1:rtx>ct</st1:rtx><o:p></o:p>
* @return<o:p></o:p>
* @throws IOException<o:p></o:p>
*/<o:p></o:p>
public static byte[] toBytes(Serializable obje<st1:rtx>ct</st1:rtx>) throws Exception {<o:p></o:p>
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();<o:p></o:p>
Obje<st1:rtx>ct</st1:rtx>OutputStream obje<st1:rtx>ct</st1:rtx>Stream = new Obje<st1:rtx>ct</st1:rtx>OutputStream(byteStream);<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Stream.writeObje<st1:rtx>ct</st1:rtx>(obje<st1:rtx>ct</st1:rtx>);<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Stream.flush();<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Stream.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
return byteStream.toByteArray();<o:p></o:p>
}<o:p></o:p>
/**<o:p></o:p>
* 将字节数组转换成对象<o:p></o:p>
* @param bytes<o:p></o:p>
* @return<o:p></o:p>
*/<o:p></o:p>
public static Obje<st1:rtx>ct</st1:rtx> toObje<st1:rtx>ct</st1:rtx>(byte[] bytes) throws Exception {<o:p></o:p>
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);<o:p></o:p>
Obje<st1:rtx>ct</st1:rtx>InputStream obje<st1:rtx>ct</st1:rtx>Stream = new Obje<st1:rtx>ct</st1:rtx>InputStream(byteStream);<o:p></o:p>
Obje<st1:rtx>ct</st1:rtx> obje<st1:rtx>ct</st1:rtx> = obje<st1:rtx>ct</st1:rtx>Stream.readObje<st1:rtx>ct</st1:rtx>();<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Stream.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
return obje<st1:rtx>ct</st1:rtx>;<o:p></o:p>
}<o:p></o:p>
/**<o:p></o:p>
* 通过GZIP算法压缩将对象转换成字节数组<o:p></o:p>
* @param obje<st1:rtx>ct</st1:rtx><o:p></o:p>
* @return<o:p></o:p>
* @throws java.lang.Exception<o:p></o:p>
*<o:p></o:p>
* The ZIP format, which is record-based, is not rea<st1:rtx>ll</st1:rtx>y suitable for this job.<o:p></o:p>
* The GZIP is more appropriate as it operates on a single stream of data.<o:p></o:p>
*/<o:p></o:p>
public static byte[] toByteByGZIP(Serializable obje<st1:rtx>ct</st1:rtx>) throws Exception {<o:p></o:p>
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();<o:p></o:p>
GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream);<o:p></o:p>
Obje<st1:rtx>ct</st1:rtx>OutputStream obje<st1:rtx>ct</st1:rtx>Stream = new Obje<st1:rtx>ct</st1:rtx>OutputStream(gzipStream);<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Stream.writeObje<st1:rtx>ct</st1:rtx>(obje<st1:rtx>ct</st1:rtx>);<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Stream.flush();<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Stream.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
gzipStream.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
return byteStream.toByteArray();<o:p></o:p>
}<o:p></o:p>
/**<o:p></o:p>
* 通过GZIP算法压缩将字节数组转换成对象<o:p></o:p>
* @param bytes<o:p></o:p>
* @return<o:p></o:p>
* @throws java.lang.Exception<o:p></o:p>
*<o:p></o:p>
* The ZIP format, which is record-based, is not rea<st1:rtx>ll</st1:rtx>y suitable for this job.<o:p></o:p>
* The GZIP is more appropriate as it operates on a single stream of data.<o:p></o:p>
*/<o:p></o:p>
public static Obje<st1:rtx>ct</st1:rtx> toObje<st1:rtx>ct</st1:rtx>ByGZIP(byte[] bytes) throws Exception {<o:p></o:p>
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);<o:p></o:p>
GZIPInputStream gzipStream = new GZIPInputStream(byteStream);<o:p></o:p>
Obje<st1:rtx>ct</st1:rtx>InputStream obje<st1:rtx>ct</st1:rtx>Stream = new Obje<st1:rtx>ct</st1:rtx>InputStream(gzipStream);<o:p></o:p>
Obje<st1:rtx>ct</st1:rtx> obje<st1:rtx>ct</st1:rtx> = obje<st1:rtx>ct</st1:rtx>Stream.readObje<st1:rtx>ct</st1:rtx>();<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Stream.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
gzipStream.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
return obje<st1:rtx>ct</st1:rtx>;<o:p></o:p>
}<o:p></o:p>
/**<o:p></o:p>
* 保存对象到文件中,通过GZIP算法压缩<o:p></o:p>
* @param fileName<o:p></o:p>
* @param obje<st1:rtx>ct</st1:rtx><o:p></o:p>
* @throws java.lang.Exception<o:p></o:p>
*/<o:p></o:p>
public static void saveObje<st1:rtx>ct</st1:rtx>ToFile(String fileName, Serializable obje<st1:rtx>ct</st1:rtx>) throws Exception {<o:p></o:p>
FileOutputStream fileStream = new FileOutputStream(fileName);<o:p></o:p>
GZIPOutputStream gzipStream = new GZIPOutputStream(fileStream);<o:p></o:p>
Obje<st1:rtx>ct</st1:rtx>OutputStream obje<st1:rtx>ct</st1:rtx>Steam = new Obje<st1:rtx>ct</st1:rtx>OutputStream(gzipStream);<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Steam.writeObje<st1:rtx>ct</st1:rtx>(obje<st1:rtx>ct</st1:rtx>);<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Steam.flush();<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Steam.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
gzipStream.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
fileStream.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
}<o:p></o:p>
/**<o:p></o:p>
* 从文件中获取对象<o:p></o:p>
* @param fileName<o:p></o:p>
* @return<o:p></o:p>
* @throws java.lang.Exception<o:p></o:p>
*/<o:p></o:p>
public static Obje<st1:rtx>ct</st1:rtx> getObje<st1:rtx>ct</st1:rtx>FromFile(String fileName) throws Exception {<o:p></o:p>
FileInputStream fileStream = new FileInputStream(fileName);<o:p></o:p>
GZIPInputStream gzipStream = new GZIPInputStream(fileStream);<o:p></o:p>
Obje<st1:rtx>ct</st1:rtx>InputStream obje<st1:rtx>ct</st1:rtx>Steam = new Obje<st1:rtx>ct</st1:rtx>InputStream(gzipStream);<o:p></o:p>
Obje<st1:rtx>ct</st1:rtx> obje<st1:rtx>ct</st1:rtx> = obje<st1:rtx>ct</st1:rtx>Steam.readObje<st1:rtx>ct</st1:rtx>();<o:p></o:p>
obje<st1:rtx>ct</st1:rtx>Steam.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
gzipStream.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
fileStream.<st1:rtx>cl</st1:rtx>ose();<o:p></o:p>
return obje<st1:rtx>ct</st1:rtx>;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>