最近,在作WEB的時候,考慮到了文件壓縮的問題,結果一搜,JDK自帶了Gzip類,整理了一下關於類的序列化的壓縮與解壓,記錄下來,方便之後使用。java
首先先放圖,兩個文件,一個沒壓縮,一個壓縮的,體積縮小一半左右。數組
接下來就放代碼了,論壇找的,再修改了一下:網絡
序列化的類:ide
import java.io.Serializable;
import java.util.Date;測試
public class GzipData implements Serializable {ui
private static final long serialVersionUID = 1L;
public Date date;
public String name;
public String pwd;
@Override
public String toString() {
return "GzipData [date=" + date + ", name=" + name + ", pwd=" + pwd + "]";
}
}.net
對序列化類壓縮與解壓縮:對象
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;ip
public class TestGzip {
// 將Data類型數據對象序列化對象壓縮,返回字節數組,壓縮後的對象數組可寫入文件保存或用於網絡傳輸
public byte[] writeCompressObject(Object obj) {
byte[] data_ = null;
try {
// 創建字節數組輸出流
ByteArrayOutputStream o = new ByteArrayOutputStream();
// 創建gzip壓縮輸出流
GZIPOutputStream gzout = new GZIPOutputStream(o);
// 創建對象序列化輸出流
ObjectOutputStream out = new ObjectOutputStream(gzout);
out.writeObject(obj);
out.flush();
out.close();
gzout.close();
// 返回壓縮字節流
data_ = o.toByteArray();
o.close();
} catch (IOException e) {
System.out.println(e);
}
return data_;
}開發
// 將壓縮字節數組還原爲Data類型數據對象
public Object readCompressObject(byte[] data_) {
Object obj = null;
try {
// 創建字節數組輸入流
ByteArrayInputStream i = new ByteArrayInputStream(data_);
// 創建gzip解壓輸入流
GZIPInputStream gzin = new GZIPInputStream(i);
// 創建對象序列化輸入流
ObjectInputStream in = new ObjectInputStream(gzin);
// 按制定類型還原對象
obj = (Object) in.readObject();
i.close();
gzin.close();
in.close();
} catch (ClassNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
return obj;
}
}
編寫的測試方法,能夠本身編寫,這裏只上方法,@Test
@Test
public void testGZip() throws Exception{
GzipData gd = new GzipData();
gd.date = new Date();
gd.name = "addjkfsajflkasdjfsdklajkdlasjlkdsajfdsaj卡迪夫斯基阿克拉附近阿斯科利發動機啊十分謹慎考慮的發生口角啊打開了司法局啊都快散了附近"
+ "的撒飛機啊打開了司法局考拉都是看到司法解釋昆德拉分離"
+ "對方快樂的撒艱苦弗拉基斯的時間飛快的時間啊分開了就是"
+ "的撒看見立法的撒看見反抗拉薩的捲髮流口水虧大發了四季度可拉斯警方"
+ "的撒反抗拉薩的捲髮可拉斯的捲髮可拉斯大解放拉的時間"
+ "對方卡拉就算對方快樂就打算開發累計ask法律界薩迪克"
+ "對方卡死的捲髮可拉斯的捲髮可拉斯大解放拉喀什的"
+ "對方卡死加拉斯的捲髮可拉的時間飛快拉薩的捲髮可拉斯大降"
+ "打開了飛機啊聖誕快樂附近歲的卡拉解放拉喀什大解放棵df阿斯頓附近卡桑德拉積分可拉斯的捲髮可拉"
+ "東風科技界啊獨守空房垃圾啊塞德里克飛機啊的撒考慮放寬東風科技啊刷卡費率"
+ "都快散了附近啊聖誕快樂附近啊聖誕快樂積分可拉斯大"
+ "的發生金卡積分可拉的時間啊卡夫卡"
+ "東風科技拉絲機可拉斯大降綠卡的時間分"
+ "的撒飛機卡的時間飛快拉薩的捲髮綠卡的撒打算開飛機啊聖誕快樂附近"
+ "飛機擴大時間飛快拉薩的捲髮可拉斯的分i看"
+ "薩多夫空軍桑德拉積分可拉斯的捲髮可拉的時間飛快拉薩的家"
+ "打開手機發送酒店客房拉薩的min";
gd.pwd = "jskdkljdaskaldsjfksadjkkfjds"
+ "dkjlsjfasdjfkasdjfkldsjfklsdajfklsdjowerirjioasl;fdjsklfjasldjfkaskfd;"
+ "kjldfjsakljqwioerejiwoqjmnfaskldfjkldasfjlkasdljfkdlaskdfjkladjkajfkls"
+ "kasfjkdlasjfkladsjfklasdjdfklwjeiorjioe2qweklafjdasklfjdijfiodsajf"
+ "wefkjfkladsjfklsdajfklasfjkldasfjklsadjfioewjerfjfalkdsajfkldsa"
+ "dsakfdasjfklasjkljfklajeriojwrioejsjmdklfjfkljasdfkjasfjasioewrioqipqipdfsiaj"
+ "ajkdfjksdafjksdal;fkldasfjadskfjewioruewureioejkldasjfdsklfjl"
+ "dsfalkjdsklafjklasjfkldsajfklasjkladfjdsildasjfkldsajfijoiw"
+ "dkflasjfklasdjklfdjsdfljadsiowjriowuiorweqjadklsfjasdkljdflasjkas"
+ "dfjkslajasdlk;jfasdl;jieowireowjrslakdfjkldasjfdklasjfdklasfjsdklafjkdls";
//Object寫 ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream("d:/obj1.txt"))); out.writeObject(gd); out.close(); //壓縮類 TestGzip tgz = new TestGzip(); byte[] temp = tgz.writeCompressObject(gd); //壓縮的類寫如文件保存 try { File file = new File("d:/obj2.txt"); OutputStream out2 = new FileOutputStream(file); out2.write(temp); out2.flush(); out2.close(); } catch (IOException e) { // TODO: handle exception } //讀取剛剛寫入的壓縮的類 try { File file = new File("d:/obj2.txt"); InputStream in = new FileInputStream(file); byte[]b = new byte[(int)file.length()]; for(int i=0;i<b.length;i++){ b[i]=(byte)in.read(); } in.close();//下面解壓縮類,還原類 Object obj = tgz.readCompressObject(b); gd = (GzipData)obj;//下面是測試解壓縮的類可否正確輸出 System.out.println(gd); }catch(Exception e){ e.printStackTrace(); } }