GZIP解压缩字符串幻数异常

时间:2016-03-16 10:06:05

标签: java android gzip compression

我正在尝试解压缩String但在解压缩时总是得到幻数异常。首先我压缩一个String然后它得到base64encoded然后解码和解压缩。代码处于通话顺序。

这是一个基于android的项目,没有外部依赖。

编码和解码工作正常。

是否有人注意到此代码中的错误并告诉我如何解决它?:

//Compressing a String
public String compress(String s) {
  if(s == null || s.length() == 0) { return string; }

  try {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(s.getBytes("UTF-8"));
    gzip.close();
    return out.toString("UTF-8");
  } catch (Exception e) {
    e.printStackTrace();
  }
}
//Encode to Base64
public String encodeBase64(String s) {
  return Base64.encodeToString(s.getBytes("UTF-8"), Base64.NO_WRAP);
}

//Decode Base64
public String decodeBase64(String s) {
  return new String(Base64.decode(s, Base64.NO_WRAP), "UTF-8");
}

public String decompress(String s) {
  if(s == null || s.length() == 0) {
    return s;
  }
  byte[] ba = s.getBytes("UTF-8");
  byte[] buffer = new byte[1024];

  try {
    ByteArrayOutputStream out = new ByteArrayOutputStream(ba.length);
    ByteArrayInputStream in = new ByteArrayInputStream(ba);
    GZIPInputStream gzip = new GZIPInputStream(in); // Magic Number Exception occures here
    int len;
    while((len = gzip.read(buffer)) > 0) {
      out.write(buffer,0 ,len);
    }
    gzip.close();
    out.close();
    return out.toString("UTF-8");
  } catch (Exception e) {
    e.printStackTrace;
  } 
}

更新

我使用方法和一个应该可以在android项目中使用的callmethod创建了一个Testclass:

import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class CompressTest {

    public static final String message = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.   \n" +
            "\n" +
            "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.   \n" +
            "\n" +
            "Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.   \n" +
            "\n" +
            "Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.   \n" +
            "\n" +
            "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis.   \n" +
            "\n" +
            "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.   \n" +
            "\n" +
            "Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus.   \n" +
            "\n" +
            "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.   \n" +
            "\n" +
            "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.   \n" +
            "\n" +
            "Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.   \n" +
            "\n" +
            "Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo";

    //Compressing a String
    public static String compress(String s) {
        if(s == null || s.length() == 0) { return s; }

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            gzip.write(s.getBytes("UTF-8"));
            gzip.close();
            return out.toString("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //Encode to Base64
    public static String encodeBase64(String s) throws UnsupportedEncodingException {
        return Base64.encodeToString(s.getBytes("UTF-8"), Base64.NO_WRAP);
    }

    //Decode Base64
    public static String decodeBase64(String s) throws UnsupportedEncodingException {
        return new String(Base64.decode(s, Base64.NO_WRAP), "UTF-8");
    }

    public static String decompress(String s) throws UnsupportedEncodingException {
        if(s == null || s.length() == 0) {
            return s;
        }
        byte[] ba = s.getBytes("UTF-8");
        byte[] buffer = new byte[1024];

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream(ba.length);
            ByteArrayInputStream in = new ByteArrayInputStream(ba);
            GZIPInputStream gzip = new GZIPInputStream(in); // Magic Number Exception occures here
            int len;
            while((len = gzip.read(buffer)) > 0) {
                out.write(buffer,0 ,len);
            }
            gzip.close();
            out.close();
            return out.toString("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

这里是电话:

try {
            String s = CompressTest.message;
            System.out.println("-----------------Message is:-----------------");
            s = CompressTest.compress(s);
            System.out.println("-----------------Compression is:-----------------");
            System.out.println(s);

            s = CompressTest.encodeBase64(s);
            System.out.println("-----------------Base64 Encode is:-----------------");
            System.out.println(s);

            s = CompressTest.decodeBase64(s);
            System.out.println("-----------------Base64 Decode is:-----------------");
            System.out.println(s);

            s = CompressTest.decompress(s);
            System.out.println("-----------------decompressed is:-----------------");
            System.out.println(s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

此外,我测试了上述测试类中的数据。

消息:见上面的代码 压缩: Mn09vtScqL8MN7 \ A [“ - Y $ ohTIcDRi'N74ܤK7cnI(LDK * H>Ĵ :电子ޡI%> d&安培; HX @ %/吨(yN_Bʲ\, m3DLl'P%O:HBG [t9Se16 - )#ޒOTTbya {P2Nmu5%A $βEN@ yrod [“mlVrMG9ݞp&LT ; F“ 'H9-O C,/ / O< L5Ntl9' $办公自动化 图5f> 8 / C I = v_Q42Q

编码:

ħ++ / vQgAAAAAAAAA77 + 9TW7vv70wEO + / VE + / vTnvv70c77 + 977 + 9He + / vXYZdFPvv70AY3Hvv71M77 + 9H ++ / VE + / vThNTu + / vTfvv71c77 + 9QVvvv73vv70iDu + / VE + / vS1ZJO + / VW / vv71o77 + 9VEnvv71j77 + 9RO + / VE + / vVLvv71pJ07vv703NO + / vdykSzfvv73vv70BYwbvv70H77 + 977 + 977 + 9bu + / vUnvv73vv73vv70o77 + 9BO + / vURrKu + / VE + / VE + / vRHvv71oPmrvv73vv73vv706Rd6hCEnvv70LCCU + ZCbvv73vv71o77 + 9WEAq77 + 977 + 9Je + / vdiN77 + 9dO + / VE + / vSjvv73vv73vv73vv715GO + / VE + / VE + / vU5f77 + 977 + 9Qu + / vcqy77 + 977 + 9XO + / VE + / VE + / vSxt77 + 9M0Tvv70A77 + 9TA / vv70CHWwn77 + 9UCVv77 + 9Fu + / VE + / VTO / aGI8b ++ / VE + / vW7vv73vv70677 + 977 + 977 + 977 + 9D ++ / vVZOK ++ / VX / vv71EdHPvv73RtBEbLEJXJO + / vTDvv71qcDTvv73vv71M77 + 9Q ++ / VE + / vWnvv705Whzvv71zFx / vv73vv73vv70177 + 9LxHvv71UZu + / VE + / VE + / VE + / vX0177 + 977 + 9VBbvv73vv73vv71iB ++ / vU7vv71iZO + / VE + / vVHvv73vv70 / Qe的+ / vWcqCS7vv71aJywlKO + / VE + / vSFra ++ / VE + / vdGUdu + / vXPvv73vv73vv73vv700QVscMXcUfHnvv70aKUjvv73vv71e77 + 9YHDvv70 + FyUQ77 + 9SlhfK ++ / VE + / vQzvv70d77 + 977 + 977 + 977 + 9c3heZwfvv73Juu + / VE + / vWltU ++ / vTQPF ++ / vUHvv71fRmdJzpTvv70 + 77 + + 90qMv77 977 + 977 + 977 + + 9YCfvv70wJe / vSNuOW3vv71FQ ++ / vdarBe + / vW9SB0UU77 + 97 7 + 9TG4uDe + / VE + / vRDvv70Makfvv73vv70F77 + 977 + 9PkdbdDlTZR0x77 + 977 + 9Nu + / vQTvv73vv73vv73vv70t77 + 9Ge + / vSkj3pJP77 + 9VFTvv73vv73vv73vv70BYu + / vXnvv70Y77 + 977 + 9YR3vv73vv717UO + / vTJO77 + 977 + 9BE + / VE + / VE + / vR51Ne + / vSVB77 + 9DCTPkAbvv71FThLvv71A77 + 9GHnvv71y77 + 977 + 9bxrvv71k77 + 9W ++ / VE + / VE + / vSLvv73vv73vv71tbFZyTRvvv73vv71H77 + 9Od2e77 + 977 + 9cDzvv70 / ZiInF ++ / vWgBOe + / VE + / VE + / VE + / VE + / VE + / VE + / vRkt77 + 977 + 9bz9j77 + 977 + 9LO + / VS / vv73vv70q77 + 9L ++ / vU8877 + 9BgDvv73vv73vv70E77 + 977 + 9TDVOdGzvv705J ++ / vSRP77 + 977 + 9YQ3vv701Zu + / VE + / vT4477 + 9L ++ / vQpjDe + / VE + / vWk9dl9R77 + 977 + 977 + 9NO + / VE + / VE + / vTLvv70e77 + 977 + 977 + 977 + 977 + 9He + / VE + / vVEY77 + 9FwAA

解码:

Mn09vtScqL8MN7 \ A [” -Y $ ohTIcDRi'N74ܤK7cnI(LDK * h取代;Ĵ:电子ޡI%> d&安培; HX @ %/吨(yN_Bʲ\ ,m3DLl'P%O:HBG [t9Se16 - )#ޒOTTby一个{P2Nmu5%A $ @βENyrod [“mlVrMG9ݞ p< F“'H9-O C,/ / O< L5Ntl 9' $办公自动化 图5f> 8 / C I = v_Q42Q

这里它不是真的可见,但我在编码之前比较压缩后解码,它看起来像这样:

Notepad++ compare

一开始的一个角色缺少其他一切都很好。这可能是数据头部分包含gzip格式的信息。我稍后会尝试手动添加并发布结果

解决方案

对于所有感兴趣的人,我在这里更改了代码,这是Teemu Ilmonen建议的方法的bytearray版本:

//Compressing a String
    public static byte[] compress(String s) {
        if(s == null || s.length() == 0) { return null; }

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            gzip.write(s.getBytes("UTF-8"));
            gzip.close();
            return out.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //Encode to Base64
    public static String encodeBase64(byte[]  array) throws UnsupportedEncodingException {
        return Base64.encodeToString(array, Base64.NO_WRAP);
    }

    //Decode Base64
    public static byte[] decodeBase64(String s) throws UnsupportedEncodingException {
        return Base64.decode(s, Base64.NO_WRAP);
    }

    public static String decompress(byte[] array) throws UnsupportedEncodingException {
        if(array == null || array.length == 0) {
            return null;
        }
        byte[] buffer = new byte[1024];

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream(array.length);
            ByteArrayInputStream in = new ByteArrayInputStream(array);
            GZIPInputStream gzip = new GZIPInputStream(in); // Magic Number Exception occures here
            int len;
            while((len = gzip.read(buffer)) > 0) {
                out.write(buffer,0 ,len);
            }
            gzip.close();
            out.close();
            return out.toString("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

2 个答案:

答案 0 :(得分:1)

使用gzip压缩内容后,您应该将其转换为字符串。你可能正在那里嘲笑那些内容。您应该使用byte []代替:

return out.toByteArray();

答案 1 :(得分:0)

您需要按以下顺序执行:

  1. 将字符串转换为Base64
  2. 使用GZip
  3. 压缩Base64字符串
  4. 解压缩GZip流。
  5. 将Base64解码为纯文本。
  6. 您收到异常是因为您尝试解压缩Base64字符串而不是 GZip流