In the transmission of files, in order to make large files more convenient and rapid transmission, the general use of compression to file compression and then transmission, The Deflater and Inflater classes in the Java.util.zip package in Java provide the user with the compression of the deflate algorithm, the following is a self written compression and decompression implementation, and an example of compressed file content, which involves a specific way to view the JDK API understanding.
/** * * @param inputbyte * Uncompressed byte array * @return uncompressed byte array * @throws IOException/public static
Byte[] Uncompress (byte[] inputbyte) throws IOException {int len = 0;
Inflater infl = new Inflater ();
Infl.setinput (Inputbyte);
Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
byte[] Outbyte = new byte[1024];
try {while (!infl.finished ()) {//uncompress and output the uncompressed content to the byte output stream bos len = infl.inflate (outbyte);
if (len = = 0) {break;
} bos.write (Outbyte, 0, Len);
} infl.end ();
catch (Exception e) {//} finally {Bos.close ();
return Bos.tobytearray ();
}/** * compression. * * @param inputbyte * @return Compressed byte array * @throws IOException/public static byte[] Comp
Ress (byte[] inputbyte) throws IOException {int len = 0;
Deflater DEFL = new Deflater ();
Defl.setinput (Inputbyte);
Defl.finish (); BytearrAyoutputstream BOS = new Bytearrayoutputstream ();
byte[] Outputbyte = new byte[1024];
try {while (!defl.finished ()) {//Compress and output the compressed content to the byte output stream bos len = defl.deflate (outputbyte);
Bos.write (outputbyte, 0, Len);
} defl.end ();
finally {bos.close ();
return Bos.tobytearray (); public static void Main (string[] args) {try {FileInputStream FIS = new FileInputStream ("d:\\testdeflate.t
XT ");
int len = fis.available ();
Byte[] B = new Byte[len];
Fis.read (b);
byte[] bd = compress (b);
In order to compress the content can transmit on the network, generally uses the Base64 code String ENCODESTR = base64.encodebase64string (BD);
byte[] bi = uncompress (base64.decodebase64 (ENCODESTR));
FileOutputStream fos = new FileOutputStream ("D:\\testinflate.txt");
Fos.write (BI);
Fos.flush ();
Fos.close ();
Fis.close (); catch (Exception e) {//}}
Above this Java deflate compression implementation method is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.