Reading Zip into Slice with offset

451 views
Skip to first unread message

Mathews

unread,
Nov 22, 2012, 10:07:01 AM11/22/12
to golan...@googlegroups.com
Hi,

i have the following problem.
I want to read the contents of a .zip into an array/slice.
For small data this works fine, however, as soon as i want to read more than 32769 bytes,
it doesn't work the way i want to.
The read() only reads up to a maximum of ~32kbyte and i can't pass the old slice to a new call
of read, without the old data getting overriden, since i don't find how to add an offset.

Of course i could read into multiple slices and then copy their contents into a bigger slice, however,
this feels kinda unnecessary copy-work.
Is there a better way around it?

The way i do it now:

var data_hash map[string]*zip.File

func GetData(name string) *[]byte {
    file, ok := data_hash[name]
    if !ok {
        log.Panic("File not found: \"" + name + "\"")
    }
    zip.NewReader()
    reader, err := file.Open()
    if err != nil {
        log.Panicf("Could not read file \"%s\": %v", name, err)
    }
    defer reader.Close()
    size := file.UncompressedSize
    buffer := make([]byte, size)
    n, err := reader.Read(buffer)
    if err != nil {
        log.Panicf("Failed to read file \"%s\": %v", name, err)
    }
    if n != int(size) {
//This is what happens if the file is too big
//I want to do something like: reader.Read(buffer + n)
        log.Panicf("Failed to read file \"%s\": File has wrong size.", name)
    }
    return &buffer
}
 

Peter

unread,
Nov 22, 2012, 10:26:46 AM11/22/12
to golan...@googlegroups.com
Which version of go or what zip package is this?

archive/zip.NewReader has this signature

 func NewReader(r io.ReaderAt, size int64) (*Reader, error)

Mathews

unread,
Nov 22, 2012, 10:38:59 AM11/22/12
to golan...@googlegroups.com


Am Donnerstag, 22. November 2012 16:26:46 UTC+1 schrieb Peter:
Which version of go or what zip package is this?

archive/zip.NewReader has this signature

 func NewReader(r io.ReaderAt, size int64) (*Reader, error)

The latest version (1.0.3)
The function zip.NewReader() is available, however i need to pass
it a ReaderAt() and i don't have one.
(Obviously i cant use a self-implemented, since i'm having problems with getting
read() to work the way i want to.)

Mathews

unread,
Nov 22, 2012, 12:16:14 PM11/22/12
to golan...@googlegroups.com
I solved it.
You can call Read(buffer[a:b)]), to pass a part of a slice.
Alternatively there is already the class bytes.buffer, which provides 
buffer.ReadFrom(reader)
Message has been deleted

Niklas Schnelle

unread,
Nov 27, 2012, 7:16:05 PM11/27/12
to golan...@googlegroups.com
I think you might want to have a look at ReadFull, I stumbled into that one twice. Read(buffer[a:b])
might not read the whole range but only as much as available. Especially bufio tends to do that
Reply all
Reply to author
Forward
0 new messages