fork download
  1. import java.util.zip.Deflater;
  2. import java.util.zip.Inflater;
  3.  
  4. class HelloWorld {
  5.  
  6. public HelloWorld() {
  7. }
  8.  
  9. private static void printB(byte[] d, int len) {
  10. for (int i = 0; i < len; i++) {
  11. System.out.printf("%02X ", d[i]);
  12. }
  13. System.out.println();
  14. }
  15.  
  16. private static void printD(byte[] d, int len) {
  17. for (int i = 0; i < len; i++) {
  18. String s = "00000000" + Integer.toString(0xFF & d[i], 2) + " ";
  19. System.out.print(s.substring(s.length() - 9));
  20. }
  21. System.out.println();
  22. }
  23.  
  24. private static void printR(byte[] d, int len) {
  25. for (int i = 0; i < len; i++) {
  26. String s = "00000000" + Integer.toString(0xFF & d[i], 2);
  27. StringBuilder b = new StringBuilder(s.substring(s.length() - 8));
  28. System.out.print(b.reverse() + " ");
  29. }
  30. System.out.println();
  31. }
  32.  
  33. public static void main(String[] args) throws java.lang.Exception {
  34.  
  35. try {
  36. // Encode a String into bytes
  37. String inputString = "blahblahblah";
  38. byte[] input = inputString.getBytes("UTF-8");
  39.  
  40. System.out.println("input:");
  41. HelloWorld.printB(input, input.length);
  42. HelloWorld.printD(input, input.length);
  43. HelloWorld.printR(input, input.length);
  44.  
  45. // Compress the bytes
  46. byte[] output = new byte[100];
  47. Deflater compresser = new Deflater(
  48. Deflater.DEFAULT_COMPRESSION, true);
  49. compresser.setInput(input);
  50. compresser.finish();
  51. int compressedDataLength = compresser.deflate(output);
  52. compresser.end();
  53.  
  54.  
  55. output[0] = 0x4B;
  56. output[1] = -54; //0xCA;
  57. output[2] = 0x49;
  58. output[3] = -52; // 0xCC;
  59. output[4] = -128; //0x80;
  60. output[5] = 0x61;
  61. output[6] = 0x00;
  62. compressedDataLength = 7;
  63.  
  64. System.out.println("output:");
  65. HelloWorld.printB(output, compressedDataLength);
  66. HelloWorld.printD(output, compressedDataLength);
  67. HelloWorld.printR(output, compressedDataLength);
  68.  
  69. // Decompress the bytes
  70. Inflater decompresser = new Inflater(true);
  71. decompresser.setInput(output, 0, compressedDataLength);
  72. byte[] result = new byte[100];
  73. int resultLength = decompresser.inflate(result);
  74. decompresser.end();
  75.  
  76. System.out.println("decompress:");
  77. HelloWorld.printB(result, resultLength);
  78.  
  79. // Decode the bytes into a String
  80. String outputString = new String(result, 0, resultLength, "UTF-8");
  81. System.out.println(outputString);
  82. } catch(java.lang.IllegalArgumentException ex) {
  83. ex.printStackTrace();
  84. } catch(java.io.UnsupportedEncodingException ex) {
  85. // handle
  86. } catch (java.util.zip.DataFormatException ex) {
  87. // handle
  88. }
  89.  
  90.  
  91. }
  92.  
  93. }
Success #stdin #stdout 0.08s 380608KB
stdin
Standard input is empty
stdout
input:
62 6C 61 68 62 6C 61 68 62 6C 61 68 
01100010 01101100 01100001 01101000 01100010 01101100 01100001 01101000 01100010 01101100 01100001 01101000 
01000110 00110110 10000110 00010110 01000110 00110110 10000110 00010110 01000110 00110110 10000110 00010110 
output:
4B CA 49 CC 80 61 00 
01001011 11001010 01001001 11001100 10000000 01100001 00000000 
11010010 01010011 10010010 00110011 00000001 10000110 00000000 
decompress:
62 6C 61 68 62 6C 61 68 62 6C 61 68 
blahblahblah