GeekInterview.com
Series: Subject: Topic:
Question: 17 of 995

Compress String

How to compress a String (algorithem)?
Asked by: Darsh_singh | Member Since Jul-2009 | Asked on: Oct 21st, 2010

View all questions by Darsh_singh

Showing Answers 1 - 1 of 1 Answers

Code
  1. import java.io.ByteArrayOutputStream;
  2. java.io.IOException;
  3. import java.util.zip.GZIPOutputStream;
  4. import java.util.zip.*;
  5.  
  6. public class zipUtil{
  7. public static String compress(String str){
  8.     if (str == null || str.length() == 0) {
  9.         return str;
  10.     }
  11.     GZIPOutputStream gzip = new GZIPOutputStream(out);
  12.     gzip.write(str.getBytes());
  13.     gzip.close();
  14.     return out.toString("ISO-8859-1");
  15.  }
  16.  
  17.  public static void main(String[] args) throws IOException {
  18. String string = "admin";
  19.     System.out.println("after compress:");
  20.     System.out.println(ZipUtil.compress(string));
  21.  
  22.   }
  23. }
  24.  

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.