线程" main"中的例外情况java.util.zip.ZipException:不是GZIP格式。使用ISO charset压缩文件时

时间:2014-10-23 12:36:53

标签: java compression gzip

我尝试在我的程序中使用GZIP流压缩/解压缩数据,并且在使用charset" ISO-8859-1"时,一切运行良好,但在将字符集更改为&#34时; UTF-8",我收到了错误消息"线程中的异常" main" java.util.zip.ZipException:不是GZIP格式"。这是我的代码:

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); 
} 

1 个答案:

答案 0 :(得分:1)

String outStr = out.toString(" UTF-8"); 这个" out"是ziped字节流,将其编码为String然后从String解码它将丢失一些字节。这可能是java的一个bug。 要解析它,您可以在compress()中将字节编码为String以返回,例如:

String infoBase64Encode = new String(Base64.encodeBase64(out.toByteArray()))

将convertress()中的字符串解码为返回的字节,例如:

String infoBase64Decode = Base64.decodeBase64(decryptAESinfo)

完整代码如下:

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); 
    }