Created
June 13, 2012 12:48
-
-
Save volodymyr-mykhailyk/2923864 to your computer and use it in GitHub Desktop.
JV Deflate decompression procedures
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 com.vmykhailyk.compression.deflate; | |
import org.apache.commons.codec.binary.Base64; | |
import java.io.ByteArrayOutputStream; | |
import java.util.zip.DataFormatException; | |
import java.util.zip.Inflater; | |
public class DeflateDecompressor { | |
private Inflater decompressor; | |
private ByteArrayOutputStream outputStream; | |
public DeflateDecompressor() { | |
decompressor = new Inflater(); | |
outputStream = new ByteArrayOutputStream(); | |
} | |
public String decompress(String data) { | |
try { | |
decompressor.reset(); | |
outputStream.reset(); | |
decompressor.setInput(Base64.decodeBase64(data)); | |
byte[] buffer = new byte[1024]; | |
while (!decompressor.finished()) { | |
int dataLength = decompressor.inflate(buffer); | |
outputStream.write(buffer, 0, dataLength); | |
} | |
return new String(outputStream.toByteArray(), "UTF-8"); | |
} catch (DataFormatException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment