I have to convert a PHP script into Go and am now stumped with a problem (I forsee many more to come).
In the script, it uses gzopen
to open both .gz and non .gz files and then takes the read data and returns a MD5-hash version of it.
I have tried using zlib.NewReader, gzip.NewReader and even flate but they are not working. Can someone point me in the right direction?
file, err := os.Open("somefile.dat")
if err != nil {
return err
}
// rc := flate.NewReader(f) //error: corrupt input before offset 5
// rc, err := zlib.NewReader(f) //error: invalid header
// rc, err := gzip.NewReader(f) //error: invalid header
if err != nil {
return err
}
buffer := make([]byte, 4096)
rc.Read(buffer)
Note: With regards to converting to MD5 hash, i followed the example here I originally used the md5.Sum() but that returned a [Size]byte where Size is 16bytes which was not what i needed. I had to convert the MD5 checksum into a 32 character hexadecimal string for it to match the checksum i needed. Hope this helps anybody else.