Created
September 25, 2014 18:32
-
-
Save mooman219/02d136107760a22d81e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.util.zip.GZIPInputStream; | |
import java.util.zip.GZIPOutputStream; | |
public class Test { | |
public static void main(String[] args) { | |
String comp = compress("poooooooo"); | |
System.out.println('"' + comp + '"'); // "af)AAAAAAAv()yVOC")+?~!BAAAA" | |
System.out.println('"' + decompress(comp) + '"'); // "poooooooo" | |
} | |
public static String compress(String input) { | |
byte[] data = input.getBytes(StandardCharsets.US_ASCII); | |
try { | |
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(data.length); | |
try (GZIPOutputStream compressStream = new GZIPOutputStream(byteStream)) { | |
compressStream.write(data); | |
} | |
return new String(Base91.encode(byteStream.toByteArray()), StandardCharsets.US_ASCII.name()); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
return null; | |
} | |
public static String decompress(String input) { | |
byte[] data = Base91.decode(input.getBytes(StandardCharsets.US_ASCII)); | |
try (GZIPInputStream compressStream = new GZIPInputStream(new ByteArrayInputStream(data)); | |
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream()) { | |
while (compressStream.available() != 0) { | |
byteOutputStream.write(compressStream.read()); | |
} | |
return new String(byteOutputStream.toByteArray(), StandardCharsets.US_ASCII); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment