Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Object 对象压缩后的MD5哈希不匹配_Object_Md5_Compression_Mismatch - Fatal编程技术网

Object 对象压缩后的MD5哈希不匹配

Object 对象压缩后的MD5哈希不匹配,object,md5,compression,mismatch,Object,Md5,Compression,Mismatch,我正在使用以下代码段压缩可序列化对象: private byte[] compressObject(Object obj) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzipOut = new GZIPOutputStream(baos); ObjectOutputStream objectOut = new Object

我正在使用以下代码段压缩可序列化对象:

 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;       
}
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;
}
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);     
}
并使用以下代码段解压缩同一对象:

 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;       
}
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;
}
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哈希:

 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;       
}
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;
}
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哈希不匹配。请建议如何获取解压缩后的对象


谢谢。

您可能需要在GZIPOutputStream上使用finish方法来压缩数据。

您好,这没有帮助。你能提出一些其他的建议吗?你能解决这个问题吗?我也有同样的问题。