Use Java zip API to compress and decompress text

Source: Internet
Author: User
Tags deflater

I saw a question in the Forum in the morning, and I felt a little interested. So I took a look. the first thought is the Java zip package. after reading the API documentation, we found that there were complete examples, including compression and decompression. use the content of java.util.zip. the javadoc of Inflater is only slightly modified.

Public static void compressstring (){
System. out. println ("///////////////////////////////////// /start ////////////////////////////////////// ");
Try {
// Encode a string into bytes
String inputstring = "lllllllllllllllllllllllllooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnggggggggggggggggggggggggggggggggggggggg G ";
Byte [] input = inputstring. getbytes ("UTF-8 ");

// Compress the bytes
Byte [] Output = new byte [100];
Deflater compresser = new Deflater ();
Compresser. setinput (input );
Compresser. Finish ();
Int compresseddatalength = compresser. Deflate (output );
System. Out. println ("input string:" + inputstring );
System. Out. println ("input length:" + input. Length );
System. Out. println ("compressed length:" + compresseddatalength );
System. Out. println ("compressed string:" + new string (output ));

// Decompress the bytes
Inflater decompresser = new Inflater ();
Decompresser. setinput (output, 0, compresseddatalength );
Byte [] result = new byte [100];
Int resultlength = decompresser. Inflate (result );
Decompresser. End ();

// Decode the bytes into a string
String outputstring = new string (result, 0, resultlength, "UTF-8 ");
System. Out. println ("decompressed string:" + outputstring );
} Catch (Java. Io. unsupportedencodingexception ex ){
// Handle
} Catch (java.util.zip. dataformatexception ex ){
// Handle
}
}

After finding out the ready-made code, I feel that it is still a bit difficult, so I am using the stream method to implement it again.

Public static void compressstringwithstream () throws exception {
System. out. println ("///////////////////////////////////// /start ////////////////////////////////////// ");

String inputstring = "lllllllllllllllllllllllllooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnggggggggggggggggggggggggggggggggggggggg G ";
Byte [] input = inputstring. getbytes ();

// Compress
Bytearrayoutputstream baos = new bytearrayoutputstream ();
Deflateroutputstream ZOS = new deflateroutputstream (baos );
ZOS. Write (input );
ZOS. Finish ();
ZOS. Flush ();
Byte [] compresseddata = baos. tobytearray ();
System. Out. println ("input string:" + inputstring );
System. Out. println ("input length:" + input. Length );
System. Out. println ("compressed length:" + compresseddata. Length );
System. Out. println ("compressed string:" + new string (compresseddata ));

// Decompress
Bytearrayinputstream BAIS = new bytearrayinputstream (compresseddata );
// Inflater = new Inflater ();
// Inflater. setinput (compresseddata );
// Inflaterinputstream GIS = new inflaterinputstream (BAIS, new Inflater ());
Inflaterinputstream GIS = new inflaterinputstream (BAIS );
Bytearrayoutputstream baos2 = new bytearrayoutputstream ();
Int I;
While (I = GIS. Read ())! =-1 ){
Baos2.write (I );
}
 
Byte [] decompresseddata = baos2.tobytearray ();
GIS. Read (decompresseddata );
System. Out. println ("decompressed length:" + decompresseddata. Length );
System. Out. println ("decompressed string:" + new string (decompresseddata ));
}

In fact, when a problem needs to be solved, the idea is very important. Try to find the most direct and simple method. compress> zip> java.util.zip think of this, and the problem is solved in half. this also requires some experience, that is, you must at least know that Java has provided a zip package. here, we have to say that many things (tools/class libraries), in fact, when you first look at them, you only need to understand what they can do, as for how to use it, if the time is limited, you can not see it for the time being, it is not too late to see it when it is really necessary to use it. as long as you know that there is such a thing, when you encounter a corresponding problem, you can think of it to help you solve the problem. this also requires accumulation, but it takes much less time to look at a function. you only need to know when it is actually used. in addition, as the Internet is developed, we should make better use of search engines. even if Java does not provide a zip package, you should try to find out if someone has implemented this function. There are so many open sources, in many cases, there is always one that can meet your needs. We should try to avoid re-building the wheel.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.