validateZipArchiveSize performs a metadata-based preflight size check before conversion. The actual tar output limit will still be enforced while streaming.
(zipReader *zip.Reader, maxSize int64)
| 48 | // check before conversion. The actual tar output limit will still be |
| 49 | // enforced while streaming. |
| 50 | func validateZipArchiveSize(zipReader *zip.Reader, maxSize int64) error { |
| 51 | if maxSize < 0 { |
| 52 | return ErrArchiveTooLarge |
| 53 | } |
| 54 | |
| 55 | maxBytes := uint64(maxSize) |
| 56 | totalBytes := uint64(tarEndBlockBytes) |
| 57 | if totalBytes > maxBytes { |
| 58 | return ErrArchiveTooLarge |
| 59 | } |
| 60 | |
| 61 | for _, file := range zipReader.File { |
| 62 | entrySize, err := projectedTarEntrySize(file) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | if entrySize > maxBytes-totalBytes { |
| 67 | return ErrArchiveTooLarge |
| 68 | } |
| 69 | totalBytes += entrySize |
| 70 | } |
| 71 | |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | func projectedTarEntrySize(file *zip.File) (uint64, error) { |
| 76 | // Each tar entry contributes one header block plus its data |
no test coverage detected