Skip to content

Commit

Permalink
Add Seek
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2brain committed Mar 18, 2017
1 parent 5561eca commit 6fec559
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
30 changes: 21 additions & 9 deletions unarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,32 @@ func (a *Archive) Read(b []byte) (n int, err error) {
return
}

// Seek moves the read offset pointer interpreted according to whence.
//
// Returns the new offset.
func (a *Archive) Seek(offset int64, whence int) (int64, error) {
r := bool(C.ar_seek(a.stream, C.off64_t(offset), C.int(whence)))
if !r {
return 0, errors.New("unarr: Seek failed")
}

return int64(C.ar_tell(a.stream)), nil
}

// Close closes the underlying unarr archive
func (a *Archive) Close() (err error) {
C.ar_close_archive(a.archive)
C.ar_close(a.stream)

return
}

// Size returns the total size of uncompressed data of the current entry
func (a *Archive) Size() int {
return int(C.ar_entry_get_size(a.archive))
}

// Offset returns the stream offset of the current entry
// Offset returns the stream offset of the current entry, for use with EntryAt
func (a *Archive) Offset() int64 {
return int64(C.ar_entry_get_offset(a.archive))
}
Expand All @@ -165,14 +185,6 @@ func (a *Archive) ModTime() time.Time {
return time.Unix((filetime/10000000)-11644473600, 0)
}

// Close closes the underlying unarr archive
func (a *Archive) Close() (err error) {
C.ar_close_archive(a.archive)
C.ar_close(a.stream)

return
}

// ReadAll reads current entry and returns data
func (a *Archive) ReadAll() ([]byte, error) {
size := a.Size()
Expand Down
48 changes: 34 additions & 14 deletions unarr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,40 @@ func TestRead(t *testing.T) {
}
}

func TestSeek(t *testing.T) {
for _, e := range exts {
a, err := NewArchive(filepath.Join("testdata", "test."+e))
if err != nil {
t.Error(err)
}

off, err := a.Seek(3, 0)
if err != nil {
t.Error(err)
}

if off != 3 {
t.Error("Seek failed")
}

a.Close()
}
}

func TestClose(t *testing.T) {
for _, e := range exts {
a, err := NewArchive(filepath.Join("testdata", "test."+e))
if err != nil {
t.Error(err)
}

err = a.Close()
if err != nil {
t.Error(err)
}
}
}

func TestSize(t *testing.T) {
for _, f := range files {
a, err := NewArchive(filepath.Join("testdata", f))
Expand Down Expand Up @@ -231,20 +265,6 @@ func TestModTime(t *testing.T) {
}
}

func TestClose(t *testing.T) {
for _, e := range exts {
a, err := NewArchive(filepath.Join("testdata", "test."+e))
if err != nil {
t.Error(err)
}

err = a.Close()
if err != nil {
t.Error(err)
}
}
}

func TestReadAll(t *testing.T) {
for _, e := range exts {
a, err := NewArchive(filepath.Join("testdata", "test."+e))
Expand Down

0 comments on commit 6fec559

Please sign in to comment.