Java java.io.IOException:不是GZIP格式

时间:2014-08-14 14:09:31

标签: java gzipinputstream

我搜索了一个如何在Java中压缩字符串的示例。

我有一个压缩然后解压缩的功能。压缩似乎工作正常:

   public static String encStage1(String str)
   {
      String format1 = "ISO-8859-1";
      String format2 = "UTF-8";
      if (str == null || str.length() == 0)
      {
         return str;
      }
      System.out.println("String length : " + str.length());
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      String outStr = null;
      try
      {
         GZIPOutputStream gzip = new GZIPOutputStream(out);
         gzip.write(str.getBytes());
         gzip.close();
         outStr = out.toString(format2);
         System.out.println("Output String lenght : " + outStr.length());
      } catch (Exception e)
      {
                  e.printStackTrace();

      }
      return outStr;
   }

但反过来抱怨字符串不是GZIP格式,即使我将encStage1的返回值直接传递回decStage3:

   public static String decStage3(String str)
   {
      if (str == null || str.length() == 0)
      {
         return str;
      }
      System.out.println("Input String length : " + str.length());
      String outStr = "";
      try
      {
         String format1 = "ISO-8859-1";
         String format2 = "UTF-8";
         GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes(format2)));
         BufferedReader bf = new BufferedReader(new InputStreamReader(gis, format2));
         String line;
         while ((line = bf.readLine()) != null)
         {
            outStr += line;
         }
         System.out.println("Output String lenght : " + outStr.length());
      } catch (Exception e)
      {
         e.printStackTrace();
      }
      return outStr;
   }

当我使用encStage1的字符串返回调用时,我收到此错误:

   public String encIDData(String idData)
   {
      String tst = "A simple test string";
      System.out.println("Enc 0: " + tst);
      String stg1 = encStage1(tst);
      System.out.println("Enc 1: " + toHex(stg1));
      String dec1 = decStage3(stg1);
      System.out.println("unzip: " + toHex(dec1));
   }

输出/错误:

Enc 0: A simple test string
String length : 20
Output String lenght : 40
Enc 1: 1fefbfbd0800000000000000735428efbfbdefbfbd2defbfbd495528492d2e51282e29efbfbdefbfbd4b07005aefbfbd21efbfbd14000000
Input String length : 40
java.io.IOException: Not in GZIP format
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:137)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:58)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:68)

2 个答案:

答案 0 :(得分:4)

一个小错误是:

     gzip.write(str.getBytes());

采用默认的平台编码,在Windows上永远不会是ISO-8859-1。更好:

     gzip.write(str.getBytes(format1));

您可以考虑使用“Cp1252”,Windows Latin-1(对于某些欧洲语言),而不是“ISO-8859-1”,Latin-1。这会引用逗号等引号等。

主要错误是将压缩字节转换为String。 Java将二进制数据(byte [],InputStream,OutputStream)与文本(String,char,Reader,Writer)分开,内部始终以Unicode格式保存。字节序列不需要是有效的UTF-8。您可以通过将字节转换为单字节编码(例如ISO-8859-1)来逃避。

最好的方法是

     gzip.write(str.getBytes(StandardCharsets.UTF_8));

所以你有完整的Unicode,每个脚本都可以合并。

并解压缩为ByteArrayOutputStreamnew String(baos.toByteArray(), StandardCharsets.UTF_8)。 在带有UTF-8的InputStreamReader上使用BufferedReader也没关系,但readLine会抛弃换行符

outStr += line + "\r\n"; // Or so.

干净答案:

public static byte[] encStage1(String str) throws IOException
{
   try (ByteArrayOutputStream out = new ByteArrayOutputStream())
   {
       try (GZIPOutputStream gzip = new GZIPOutputStream(out))
       {
           gzip.write(str.getBytes(StandardCharsets.UTF_8));
       }
       return out.toByteArray();
       //return out.toString(StandardCharsets.ISO_8859_1);
       // Some single byte encoding
   }
}

public static String decStage3(byte[] str) throws IOException
{
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str)))
   {
       int b;
       while ((b = gis.read()) != -1) {
           baos.write((byte) b);
       }
   }
   return new String(baos.toByteArray(), StandardCharset.UTF_8);
}

答案 1 :(得分:3)

使用toString / getBytes进行编码/解码是一种错误的方法。尝试使用类似BASE64编码的东西(jdk 1.8中的java.util.Base64)

作为证明尝试这个简单的测试:

import org.testng.annotations.Test;
import java.io.ByteArrayOutputStream;
import static org.testng.Assert.assertEquals;

public class SimpleTest {

    @Test
    public void test() throws Exception {

        final String CS = "utf-8";

        byte[] b0 = {(byte) 0xff};
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        out.write(b0);
        out.close();

        byte[] b1 = out.toString(CS).getBytes(CS);

        assertEquals(b0, b1);
    }
}