golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
120.1k stars 17.24k forks source link

proposal: archive/zip: File.OpenRaw() should return io.NewSectionReader #67116

Closed krum110487 closed 2 weeks ago

krum110487 commented 2 weeks ago

Proposal Details

Right now as implemented in OpenRaw() it uses a NewSectionReader to return a reader so that it could have a Read, Seek and ReadAt, but for whatever reason it is returning just an io.Reader implementation meaning that with OpenRaw it is not seekable.

Is there a good reason why the function returns io.Reader and not io.ReadSeeker so we can have more access to the bytes?

It seems like a very easy modification from:

func (f *File) OpenRaw() (io.Reader, error) {
    bodyOffset, err := f.findBodyOffset()
    if err != nil {
        return nil, err
    }
    r := io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset, int64(f.CompressedSize64))
    return r, nil
}

to

func (f *File) OpenRaw() (io.ReadSeeker, error) {
    bodyOffset, err := f.findBodyOffset()
    if err != nil {
        return nil, err
    }
    r := io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset, int64(f.CompressedSize64))
    return r, nil
}
seankhliao commented 2 weeks ago

changing the function return signature isn't a backwards compatible change. you can type assert if you need the other methods

krum110487 commented 2 weeks ago

changing the function return signature isn't a backwards compatible change. you can type assert if you need the other methods

I modified my post, I meant to put "ReadSeeker" it has been updated. Since it also implements reader, wouldn't it work fine and be backwards compatible since it implements the functions?

OH! so I can just do this?

r,_ := f.OpenRaw()
return r.(io.ReadSeeker), nil