fix zip reader

This commit is contained in:
royalcat 2024-06-20 11:20:43 +03:00
parent 08a36d8165
commit d973e67ea3
2 changed files with 22 additions and 7 deletions

View file

@ -239,6 +239,10 @@ func (d *archiveFile) loadMore(ctx context.Context, to int64) error {
if err != nil && err != io.EOF {
return fmt.Errorf("error copying from archive file reader: %w", err)
}
_, err = d.buffer.Seek(d.offset, io.SeekStart)
if err != nil {
return fmt.Errorf("failed to seek to start of the file: %w", err)
}
return nil
}
@ -252,6 +256,7 @@ func (d *archiveFile) Read(ctx context.Context, p []byte) (n int, err error) {
if err != nil && err != io.EOF {
return n, fmt.Errorf("failed to read from buffer: %w", err)
}
d.offset += int64(n)
return n, nil
}
@ -292,12 +297,12 @@ func ZipLoader(ctx context.Context, ctxreader ctxio.ReaderAt, size int64) (map[s
zr, err := zip.NewReader(reader, size)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create zip reader: %w", err)
}
rc, err := zr.File[i].Open()
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to open file in zip archive: %w", err)
}
return rc, nil

View file

@ -4,7 +4,6 @@ import (
"archive/zip"
"bytes"
"context"
"io"
"testing"
"git.kmsign.ru/royalcat/tstor/src/vfs"
@ -59,13 +58,24 @@ func TestZipFilesystem(t *testing.T) {
require.Equal("1.txt", e.Name())
require.NotNil(e)
out := make([]byte, 11)
out := make([]byte, 5)
f, err := zfs.Open(ctx, "/path/to/test/file/1.txt")
require.NoError(err)
n, err := f.Read(ctx, out)
require.Equal(io.EOF, err)
require.Equal(11, n)
require.Equal(fileContent, out)
require.NoError(err)
require.Equal(5, n)
require.Equal([]byte("Hello"), out)
outSpace := make([]byte, 1)
n, err = f.Read(ctx, outSpace)
require.NoError(err)
require.Equal(1, n)
require.Equal([]byte(" "), outSpace)
n, err = f.Read(ctx, out)
require.NoError(err)
require.Equal(5, n)
require.Equal([]byte("World"), out)
}