fork(4) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import sun.misc.BASE64Decoder;
  4. import sun.misc.BASE64Encoder;
  5.  
  6. import java.io.ByteArrayOutputStream;
  7. import java.util.zip.Deflater;
  8. import java.util.zip.Inflater;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static final String encoding = "UTF-8";
  14.  
  15. public static void main (String[] args) throws Exception {
  16. String s = "123456789 a s d f g h j k l ; ' q w e r t y u i o p [ ] \\ 1 2 3 4 5 6 7 8 9 0 - = as df gh jk l; zx cv bn m, ./ qw er ty ui op []";
  17. s += "qwe rty uio p[] asd fgh jkl zxc vbn m,. 123 456 789 1234 5678 90-= qwer tyui op[] asdf ghjk l;zx cvbn m,./";
  18. s += "12345 67890 qwert yuiop asdfg hjklz xcvbn m,khg er xcgvdst 453 gd fyrt634 5 dg e653 545u7r ydf dgfsd fsart sdgdsg";
  19.  
  20. String compressed = compress(s);
  21. System.out.println("Original_String_Length = " + s.length());
  22. System.out.println("Compressed_String_Length = " + compressed.length());
  23.  
  24. String decompressed = decompress(decodeBase64(compressed));
  25. System.out.println("Decompressed_String_Length = " + decompressed.length() + " == Original_String_Length (" + s.length() + ")");
  26. System.out.println("Original_String == Decompressed_String = " + (s.equals(decompressed) ? "True" : "False"));
  27. }
  28.  
  29. public static String compress(String str) throws Exception {
  30. byte[] bytes = str.getBytes(encoding);
  31. Deflater deflater = new Deflater();
  32. deflater.setInput(bytes);
  33. deflater.finish();
  34. byte[] buffer = new byte[1024];
  35. while(!deflater.finished()) {
  36. int count = deflater.deflate(buffer);
  37. bos.write(buffer, 0, count);
  38. }
  39. bos.close();
  40. byte[] output = bos.toByteArray();
  41. return encodeBase64(output);
  42. }
  43.  
  44. public static String decompress(byte[] bytes) throws Exception {
  45. Inflater inflater = new Inflater();
  46. inflater.setInput(bytes);
  47. byte[] buffer = new byte[1024];
  48. while (!inflater.finished()) {
  49. int count = inflater.inflate(buffer);
  50. bos.write(buffer, 0, count);
  51. }
  52. bos.close();
  53. byte[] output = bos.toByteArray();
  54. return new String(output);
  55. }
  56.  
  57. public static String encodeBase64(byte[] bytes) throws Exception {
  58. BASE64Encoder base64Encoder = new BASE64Encoder();
  59. return base64Encoder.encodeBuffer(bytes).replace("\r\n", "").replace("\n", "");
  60. }
  61.  
  62. public static byte[] decodeBase64(String str) throws Exception {
  63. BASE64Decoder base64Decoder = new BASE64Decoder();
  64. return base64Decoder.decodeBuffer(str);
  65. }
  66. }
Success #stdin #stdout 0.11s 320832KB
stdin
Standard input is empty
stdout
Original_String_Length = 348
Compressed_String_Length = 292
Decompressed_String_Length = 348 == Original_String_Length (348)
Original_String == Decompressed_String = True