Skip to content

Commit

Permalink
Add ReadAll to ZipReaderFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Tasssadar committed Feb 23, 2021
1 parent 5f3f1a7 commit 186f320
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions zipreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"sync"
Expand Down Expand Up @@ -135,6 +136,28 @@ func (zr *ZipReaderFile) ZipHeader() *zip.FileHeader {
return nil
}

// Open, Read all bytes until limit and close the file
func (zr *ZipReaderFile) ReadAll(limit int64) ([]byte, error) {
if err := zr.Open(); err != nil {
return nil, err
}
defer zr.Close()

var data []byte
var lastErr error
for zr.Next() {
data, lastErr = ioutil.ReadAll(io.LimitReader(zr, limit))
if lastErr == nil {
return data, nil
}
}

if lastErr == nil {
return nil, io.ErrUnexpectedEOF
}
return nil, lastErr
}

// Closes this ZIP archive and all it's ZipReaderFile entries.
func (zr *ZipReader) Close() error {
if zr.zipFileReader == nil {
Expand Down

0 comments on commit 186f320

Please sign in to comment.