mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
Every function and method, tests included, has a doc comment: what it is or does, what it returns and anything a caller needs to know. Only comments are added; gofmt split a few aligned blocks of one-line functions once each had its own comment. The generated protobuf code is left as protoc writes it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package archive
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
// walkZIP is Walk over a ZIP, by archive/zip; an entry it will not open
|
|
// (not stored or deflated) is ErrMethod, and one that fails as it unpacks
|
|
// (a bad CRC, as archive/zip checks it at the end) ErrChecksum.
|
|
func walkZIP(r io.ReaderAt, size int64, fn func(e Entry, open Opener) bool) error {
|
|
z, err := zip.NewReader(r, size)
|
|
if err != nil {
|
|
return ErrFormat
|
|
}
|
|
for _, f := range z.File {
|
|
method := "deflated"
|
|
if f.Method == zip.Store {
|
|
method = "stored"
|
|
}
|
|
e := Entry{Name: f.Name, Size: int64(f.UncompressedSize64), Packed: int64(f.CompressedSize64),
|
|
Method: method, Modified: f.Modified, Dir: strings.HasSuffix(f.Name, "/")}
|
|
open := func(max int64) ([]byte, error) {
|
|
if err := sized(e.Size, max); err != nil {
|
|
return nil, err
|
|
}
|
|
rc, err := f.Open()
|
|
if err != nil {
|
|
return nil, ErrMethod
|
|
}
|
|
defer rc.Close()
|
|
var b bytes.Buffer
|
|
n, err := io.Copy(&b, io.LimitReader(rc, max+1))
|
|
if n > max {
|
|
return nil, ErrTooBig
|
|
}
|
|
if err != nil {
|
|
return nil, ErrChecksum // archive/zip checks the CRC as it ends
|
|
}
|
|
return b.Bytes(), nil
|
|
}
|
|
if !fn(e, open) {
|
|
return nil
|
|
}
|
|
}
|
|
return nil
|
|
}
|