Guest User

Untitled

a guest
Jul 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. String orig = ".............";
  2.  
  3. // compress it
  4. ByteArrayOutputStream baostream = new ByteArrayOutputStream();
  5. OutputStream outStream = new GZIPOutputStream(baostream);
  6. outStream.write(orig.getBytes());
  7. outStream.close();
  8. String compressedStr = baostream.toString();
  9.  
  10. // uncompress it
  11. InputStream inStream = new GZIPInputStream(new ByteArrayInputStream(compressedStr.getBytes()));
  12. ByteArrayOutputStream baoStream2 = new ByteArrayOutputStream();
  13. byte[] buffer = new byte[8192];
  14. int len;
  15. while((len = inStream.read(buffer))>0)
  16. baoStream2.write(buffer, 0, len);
  17. String uncompressedStr = baoStream2.toString();
  18.  
  19. String orig = ".............";
  20.  
  21. // Compress it
  22. ByteArrayOutputStream baostream = new ByteArrayOutputStream();
  23. OutputStream outStream = new GZIPOutputStream(baostream);
  24. outStream.write(orig.getBytes("UTF-8"));
  25. outStream.close();
  26. byte[] compressedBytes = baostream.toByteArray(); // toString not always possible
  27.  
  28. // Uncompress it
  29. InputStream inStream = new GZIPInputStream(
  30. new ByteArrayInputStream(compressedBytes));
  31. ByteArrayOutputStream baoStream2 = new ByteArrayOutputStream();
  32. byte[] buffer = new byte[8192];
  33. int len;
  34. while ((len = inStream.read(buffer)) > 0) {
  35. baoStream2.write(buffer, 0, len);
  36. }
  37. String uncompressedStr = baoStream2.toString("UTF-8");
  38.  
  39. System.out.println("orig: " + orig);
  40. System.out.println("unc: " + uncompressedStr);
Add Comment
Please, Sign In to add comment