<> use gzip Compress data , decompression

We are in the development process , If a large number of data transmission operations need to be carried out between the client and the server , In this scenario, we will give priority to using data compression to reduce the amount of data transmitted , So as to improve the transmission efficiency , And reduce the running memory of the client .

<>gzip brief introduction

gzip Is a common compression algorithm , It is the abbreviation of several file compression programs , Usually refers to GNU Implementation of plan , Here gzip representative GNU zip.

HTTP On the agreement GZIP Coding is a way to improve WEB Technology for application performance . Large flow WEB Sites often use GZIP Compression technology to make users feel faster .

<>Java in gzip Compression and decompression implementation

<> Byte stream compression :
/** * Byte stream gzip compress * @param data * @return */ public static byte[] gZip(byte[]
data) { byte[] b = null; try { ByteArrayInputStream in = new
ByteArrayInputStream(data); ByteArrayOutputStream out = new
ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out);
byte[] buffer = new byte[4096]; int n = 0; while((n = in.read(buffer, 0,
buffer.length)) > 0){ gzip.write(buffer, 0, n); } gzip.close(); in.close(); b =
out.toByteArray(); out.close(); } catch (Exception ex) { ex.printStackTrace();
} return b; }
<> Byte stream decompression :
/** * gzip decompression * @param data * @return */ public static byte[] unGZip(byte[]
data){ // Create a new output stream ByteArrayOutputStream out = new ByteArrayOutputStream();
try { ByteArrayInputStream in = new ByteArrayInputStream(data); GZIPInputStream
gzip = new GZIPInputStream(in); byte[] buffer = new byte[4096]; int n = 0; //
Write the decompressed data to the output stream while ((n = gzip.read(buffer)) >= 0) { out.write(buffer, 0, n); }
in.close(); gzip.close(); out.close(); } catch (Exception e) {
e.printStackTrace(); } return out.toByteArray(); }

Technology