Reputation: 123
I'm trying in my program to Compress/Decompress data using GZIP streams and when using the charset "ISO-8859-1", everything working well but when changing the charset to "UTF-8", i'm getting the Error message "Exception in thread "main" java.util.zip.ZipException: Not in GZIP format". this is my code:
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
System.out.println("String length : " + str.length());
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
String outStr = out.toString("UTF-8");
System.out.println("Output String lenght : " + outStr.length());
System.out.println("Output : " + outStr.toString());
return outStr;
}
public static String decompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
System.out.println("Input String length : " + str.length());
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes("UTF-8")));
BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
String outStr = "";
String line;
while ((line=bf.readLine())!=null) {
outStr += line;
}
System.out.println("Output String lenght : " + outStr.length());
return outStr;
}
public static void main(String[] args) throws IOException {
String string = "my data";
System.out.println("after compress:");
String compressed = compress(string);
System.out.println(compressed);
System.out.println("after decompress:");
String decomp = decompress(compressed);
System.out.println(decomp);
}
Upvotes: 2
Views: 6093
Reputation: 59
String outStr = out.toString("UTF-8"); This "out" is ziped byte stream,encode it to String then decode it from String will be lose some bytes.This maybe a bug of java. To Resolve it,you can encode bytes to String in compress() to return, such as :
String infoBase64Encode = new String(Base64.encodeBase64(out.toByteArray()))
decode String to bytes in decompress() to return, such as :
String infoBase64Decode = Base64.decodeBase64(decryptAESinfo)
complete code as follows:
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
System.out.println("String length : " + str.length());
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
String outStr = new String(Base64.encodeBase64(out.toByteArray()));
System.out.println("Output String lenght : " + outStr.length());
System.out.println("Output : " + outStr.toString());
return outStr;
}
public static String decompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
System.out.println("Input String length : " + str.length());
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(Base64.decodeBase64(str)));
String outStr = "";
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[256];
int n;
while ((n = gis.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
System.out.println("Output String lenght : " + outStr.length());
return new String(out.toByteArray());
}
public static void main(String[] args) throws IOException {
String string = "my data";
System.out.println("after compress:");
String compressed = compress(string);
System.out.println(compressed);
System.out.println("after decompress:");
String decomp = decompress(compressed);
System.out.println(decomp);
}
Upvotes: 2