???????????????????????湤??????(?????????????????????????)????????????????????????????
???????????洢????????????????????.??????鷳?????????.??java??????????????????????????????????
?????л????????????????????(???漲?????????)???д??????????????????????棬?????κζ??????.?????
????????.??????д???????java????????????????????.?????????????????????洢??????.????????????????
????????????????????????????????.????gzip????洢???????????????????Ч???.(????????????????
??????У???????????????(10????????????7M???????600K)?????????續(xù)???????26M??????????????12M????????
?????????????.Ч?????????).
????java????????????ó????????????????????Щ?????????????(Internet??????????).???????????????????
??????????Ч??????????????????????????.?????????????Ч???????Ч??.
???????????java?μ????gzip????????????ü??.???????????????????.
(???л?????????????:Data.java)
import java.io.*;
import java.util.zip.*;
public class Data implements Serializable//??д??????л????
{
String name="???";
int age=123;
float height=1.902f;
}
(?????????????????:compressObject.java)
import java.util.zip.*;
import java.io.*;
public final class compressObject
{
//??Data??????????????л????????????????????飬??????????????д?????????????????紫??
public static byte[] writeCompressObject(Data object_)
{
byte[] data_=null;
try
{
//????????????????
ByteArrayOutputStream o = new ByteArrayOutputStream();
//????gzip????????
GZIPOutputStream gzout=new GZIPOutputStream(o);
//???????????л??????
ObjectOutputStream out = new ObjectOutputStream(gzout);
out.writeObject(object_);
out.flush();
out.close();
gzout.close();
//????????????
data_=o.toByteArray();
o.close();
}catch(IOException e)
{
System.out.println(e);
}
return(data_);
}
//???????????黹??Data???????????
public static Data readCompressObject(byte[] data_)
{
Data object_=null;
try
{
//?????????????????
ByteArrayInputStream i = new ByteArrayInputStream(data_);
//????gzip?????????
GZIPInputStream gzin=new GZIPInputStream(i);
//???????????л???????
ObjectInputStream in = new ObjectInputStream(gzin);
//???????????????
object_=(Data)in.readObject();
i.close();
gzin.close();
in.close();
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(IOException e)
{
System.out.println(e);
}
return(object_);
}
}
(??????:test.java)
import java.io.*;
import java.util.zip.*;
public class test
{
public static void main(String[] args)
{
Data testData_=new Data();
//δ??????????????
System.out.println("name="+testData_.name+" age="+testData_.age+" height="+testData_.height);
//???
byte[] i_=compressObject.writeCompressObject(testData_);
/*
????б???????紫?????????????????
*/
//?????
Data o_=compressObject.readCompressObject(i_);
//??????????????
System.out.println("name="+o_.name+" age="+o_.age+" height="+o_.height);
}
}
???????????java?????gzip????????????????????????????????????????.?????????????????????
???????????.???????chen@mail.dtbay.com.
????(???????????????? ??????:pentiumII 233??mem 160M??win98/linux??jdk1.2-jdk1.4)