Skip to content

Commit

Permalink
Add fileReader
Browse files Browse the repository at this point in the history
When calling Open on a File, the associated pool is used to find an
existing SizeReadSeekCloser for the underlying stream.

The Close method attempts to put the underlying SizeReadSeekCloser back in
the pool if it's not yet at the end of the stream.
  • Loading branch information
bodgit committed May 2, 2022
1 parent d50f5dd commit af5b2ca
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ type File struct {
offset int64
}

type fileReader struct {
rc util.SizeReadSeekCloser
f *File
}

func (fr *fileReader) Read(p []byte) (int, error) {
return fr.rc.Read(p)
}

func (fr *fileReader) Close() error {
if fr.rc == nil {
return nil
}

offset, err := fr.rc.Seek(0, io.SeekCurrent)
if err != nil {
return err
}

f := fr.f

if offset == fr.rc.Size() { // EOF reached
if err := fr.rc.Close(); err != nil {
return err
}
} else {
if _, err := f.zip.pool[f.folder].Put(offset, fr.rc); err != nil {
return err
}
}

fr.rc = nil

return nil
}

// Open returns an io.ReadCloser that provides access to the File's contents.
// Multiple files may be read concurrently.
func (f *File) Open() (io.ReadCloser, error) {
Expand All @@ -66,16 +102,26 @@ func (f *File) Open() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(nil)), nil
}

r, _, err := f.zip.folderReader(f.zip.si, f.folder)
if err != nil {
return nil, err
var err error

rc, _ := f.zip.pool[f.folder].Get(f.offset)
if rc == nil {
rc, _, err = f.zip.folderReader(f.zip.si, f.folder)
if err != nil {
return nil, err
}
}

if _, err := io.CopyN(ioutil.Discard, r, f.offset); err != nil {
if _, err = rc.Seek(f.offset, io.SeekStart); err != nil {
return nil, err
}

return plumbing.LimitReadCloser(r, int64(f.UncompressedSize)), nil
fr := &fileReader{
rc: rc,
f: f,
}

return plumbing.LimitReadCloser(fr, int64(f.UncompressedSize)), nil
}

// OpenReaderWithPassword will open the 7-zip file specified by name using
Expand Down

0 comments on commit af5b2ca

Please sign in to comment.