Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.zip.GZIPInputStream;
  7. import java.util.zip.GZIPOutputStream;
  8.  
  9. public class Gzip {
  10.  
  11. public static byte[] compress(String data) throws IOException {
  12. ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length());
  13. GZIPOutputStream gzip = new GZIPOutputStream(bos);
  14. gzip.write(data.getBytes());
  15. gzip.close();
  16. byte[] compressed = bos.toByteArray();
  17. bos.close();
  18. return compressed;
  19. }
  20.  
  21. public static String decompress(final byte[] compressed) throws IOException {
  22. ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
  23. GZIPInputStream gis = new GZIPInputStream(bis);
  24. byte[] bytes = IOUtils.toByteArray(gis);
  25. return new String(bytes, "UTF-8");
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement