本例子演示怎样对java对象进行压缩和解压,主要用到java.util.zip包里的GZIPInputStream和GZIPOutputStream类。 AjavaUser 为要压缩的对象,会被压缩为 ajavauser.dat文件,再通过解压方法,读取 ajavauser.dat 里的数据。
java类对象:
package ajava.code.javase;
import java.io.Serializable;
public class AjavaUser implements Serializable {
private Long id;
private String username;
private String password;
private String firstName;
private String lastName;
public AjavaUser() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("id=").append(id);
sb.append("; username=").append(username);
sb.append("; password=").append(password);
sb.append("; firstName=").append(firstName);
sb.append("; lastName=").append(lastName);
return sb.toString();
}
}
压缩java对象:
package ajava.code.javase;
import java.io.FileOutputStream;
import java.io.File;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
public class AjavaZipObjectDemo {
public static void main(String[] args) {
AjavaUser admin = new AjavaUser();
admin.setId(new Long(1));
admin.setUsername("ajava");
admin.setPassword("123456");
admin.setFirstName("ajava.org");
admin.setLastName("java独立社区");
AjavaUser foo = new AjavaUser();
foo.setId(new Long(2));
foo.setUsername("mark");
foo.setPassword("123456");
foo.setFirstName("ajava");
foo.setLastName("会员");
System.out.println("压缩中...");
System.out.println(admin);
System.out.println(foo);
try {
FileOutputStream fos = new FileOutputStream(new File("ajavauser.dat"));
GZIPOutputStream gos = new GZIPOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(gos);
oos.writeObject(admin);
oos.writeObject(foo);
oos.flush();
oos.close();
gos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
压缩中...
id=1; username=ajava; password=123456; firstName=ajava.org; lastName=java独立社区
id=2; username=mark; password=123456; firstName=ajava; lastName=会员
java对象压缩文件的解压:
package ajava.code.javase;
import java.io.FileInputStream;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class AjavaUnzipObjectDemo {
public static void main(String[] args) {
AjavaUser admin = null;
AjavaUser foo = null;
try {
FileInputStream fis = new FileInputStream(new File("ajavauser.dat"));
GZIPInputStream gis = new GZIPInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(gis);
admin = (AjavaUser) ois.readObject();
foo = (AjavaUser) ois.readObject();
ois.close();
gis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("ajava网站创始人 = [" + admin + "]");
System.out.println("ajava网站会员 = [" + foo + "]");
System.out.println("部分信息 = [" + admin.getFirstName() +"-"+admin.getLastName()+ "]");
}
}
运行结果:
ajava网站创始人 = [id=1; username=ajava; password=123456; firstName=ajava.org; lastName=java独立社区]
ajava网站会员 = [id=2; username=mark; password=123456; firstName=ajava; lastName=会员]
部分信息 = [ajava.org-java独立社区]