Android GZIP壓縮IO流,最佳化APP資料轉送(一),androidapp

來源:互聯網
上載者:User

Android GZIP壓縮IO流,最佳化APP資料轉送(一),androidapp

 

  我是小搬運工,今天講解流的壓縮傳輸 

在我們安卓開發中,通常伴隨著大量的資料轉送,在此,樓主給出一個簡單的壓縮流的資料進行操作。

public void GZIPCpf(String path) {
/*
* 描述
* 壓縮測試
* 通過壓縮的方式實現對java資料流的讀取
* java壓縮類屬於inputstream和outputstream結構的一部分
* */
/*
* 壓縮類的功能
* 1、CheakedInputStream GetCheckSum()為任何inputstream產生效驗和(不僅僅只是解壓)
* checkedoutputstream GetCheckSum()為任何outputstream產生效驗和(不僅僅只是解壓)
* DeflaterOutputStream,用於壓縮類的基礎類
* ZIPOutputStream 一個DeflaterOutputStream,將資料壓縮成zip檔案格式
* GZIPOutputStream 一個DeflaterOutputStream,將資料壓縮成Gzip檔案格式
* InflaterInputStream 用於解壓類的基礎類
* ZIPInputStream 一個deflaterinputStream,解壓用zip格式儲存的資料
* GZIPInputStream 一個deflaterinputStream,解壓用gzip儲存的資料
*
* */

//用GZIP進行簡單壓縮,適用於單個資料流的壓縮
try {
//寫
File f = new File(path);
BufferedReader br = new BufferedReader(new FileReader(path));
BufferedOutputStream bOs = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(f)));
int c;
while ((c=br.read())!=-1) {
bOs.write(c);
System.out.println(c);
}
br.close();
bOs.close();

//讀
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
new FileInputStream(f)
)
)
);
String text;
while ((text=reader.readLine())!=null) {
System.out.println(text);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

大家可以根據這個簡單的案例做出更加優秀的流傳輸-v-!!!嘿嘿

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.