简体   繁体   中英

How to implement PHP's gzopen in Go?

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.

Since you need to support reading from both gzip and non-gzip files, add a check to see if the if gzip.NewReader returns the ErrHeader error. If that is returned, fallback to reading the file normally:

file, err := os.Open("somefile.dat")
if err != nil {
    return err
}

var rc io.ReadCloser
if gz, err := gzip.NewReader(file); err == gzip.ErrHeader {
    if _, err := file.Seek(0, io.SeekStart); err != nil {
        return err
    }
    rc = file
} else if err == nil {
    rc = gz
} else {
    return err
}

md5sum := md5.New()
if _, err := io.Copy(md5sum, rc); err != nil {
    return err
}

if err := rc.Close(); err != nil {
    return err
}

// TODO: md5sum.Sum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM