【发布时间】:2012-05-02 18:14:58
【问题描述】:
我正在使用以下 sn-p 压缩一个可序列化对象:
private byte[] compressObject(Object obj) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
ObjectOutputStream objectOut = new ObjectOutputStream(gzipOut);
objectOut.writeObject(obj);
objectOut.close();
byte[] bytes = baos.toByteArray();
return bytes;
}
并使用以下 sn-p 解压缩同一个对象:
private Object decompressObject(byte[] bytes) throws IOException,ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
GZIPInputStream gzipIn = new GZIPInputStream(bais);
ObjectInputStream objectIn = new ObjectInputStream(gzipIn);
Object obj = objectIn.readObject();
objectIn.close();
return obj;
}
在压缩 Object 之前和解压 Object 之后,我使用以下 sn-p 计算 MD5 Hash:
public String getMD5Hash(Object obj) throws IOException, NoSuchAlgorithmException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(obj);
byte[] data = bos.toByteArray();
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(data,0,data.length);
BigInteger i = new BigInteger(1,m.digest());
return String.format("%1$032X", i);
}
但是压缩前和压缩后计算的MD5 Hash不匹配。请建议解压后如何获取对象。
谢谢。
【问题讨论】:
-
你解决了吗?我也有同样的问题。
标签: object md5 compression mismatch