// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package zip

import (
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
)

var zipinsecurepath = godebug.New("zipinsecurepath")

var (
	ErrFormat       = errors.New("zip: not a valid zip file")
	ErrAlgorithm    = errors.New("zip: unsupported compression algorithm")
	ErrChecksum     = errors.New("zip: checksum error")
	ErrInsecurePath = errors.New("zip: insecure file path")
)

// A Reader serves content from a ZIP archive.
type Reader struct {
	r             io.ReaderAt
	File          []*File
	Comment       string
	decompressors map[uint16]Decompressor

	// Some JAR files are zip files with a prefix that is a bash script.
	// The baseOffset field is the start of the zip file proper.
	baseOffset int64

	// fileList is a list of files sorted by ename,
	// for use by the Open method.
	fileListOnce sync.Once
	fileList     []fileListEntry
}

// A ReadCloser is a [Reader] that must be closed when no longer needed.
type ReadCloser struct {
	f *os.File
	Reader
}

// A File is a single file in a ZIP archive.
// The file information is in the embedded [FileHeader].
// The file content can be accessed by calling [File.Open].
type File struct {
	FileHeader
	zip          *Reader
	zipr         io.ReaderAt
	headerOffset int64 // includes overall ZIP archive baseOffset
	zip64        bool  // zip64 extended information extra field presence
}

// OpenReader will open the Zip file specified by name and return a ReadCloser.
//
// If any file inside the archive uses a non-local name
// (as defined by [filepath.IsLocal]) or a name containing backslashes
// and the GODEBUG environment variable contains `zipinsecurepath=0`,
// OpenReader returns the reader with an ErrInsecurePath error.
// A future version of Go may introduce this behavior by default.
// Programs that want to accept non-local names can ignore
// the ErrInsecurePath error and use the returned reader.
func ( string) (*ReadCloser, error) {
	,  := os.Open()
	if  != nil {
		return nil, 
	}
	,  := .Stat()
	if  != nil {
		.Close()
		return nil, 
	}
	 := new(ReadCloser)
	if  = .init(, .Size());  != nil &&  != ErrInsecurePath {
		.Close()
		return nil, 
	}
	.f = 
	return , 
}

// NewReader returns a new [Reader] reading from r, which is assumed to
// have the given size in bytes.
//
// If any file inside the archive uses a non-local name
// (as defined by [filepath.IsLocal]) or a name containing backslashes
// and the GODEBUG environment variable contains `zipinsecurepath=0`,
// NewReader returns the reader with an [ErrInsecurePath] error.
// A future version of Go may introduce this behavior by default.
// Programs that want to accept non-local names can ignore
// the [ErrInsecurePath] error and use the returned reader.
func ( io.ReaderAt,  int64) (*Reader, error) {
	if  < 0 {
		return nil, errors.New("zip: size cannot be negative")
	}
	 := new(Reader)
	var  error
	if  = .init(, );  != nil &&  != ErrInsecurePath {
		return nil, 
	}
	return , 
}

func ( *Reader) ( io.ReaderAt,  int64) error {
	, ,  := readDirectoryEnd(, )
	if  != nil {
		return 
	}
	.r = 
	.baseOffset = 
	// Since the number of directory records is not validated, it is not
	// safe to preallocate r.File without first checking that the specified
	// number of files is reasonable, since a malformed archive may
	// indicate it contains up to 1 << 128 - 1 files. Since each file has a
	// header which will be _at least_ 30 bytes we can safely preallocate
	// if (data size / 30) >= end.directoryRecords.
	if .directorySize < uint64() && (uint64()-.directorySize)/30 >= .directoryRecords {
		.File = make([]*File, 0, .directoryRecords)
	}
	.Comment = .comment
	 := io.NewSectionReader(, 0, )
	if _,  = .Seek(.baseOffset+int64(.directoryOffset), io.SeekStart);  != nil {
		return 
	}
	 := bufio.NewReader()

	// The count of files inside a zip is truncated to fit in a uint16.
	// Gloss over this by reading headers until we encounter
	// a bad one, and then only report an ErrFormat or UnexpectedEOF if
	// the file count modulo 65536 is incorrect.
	for {
		 := &File{zip: , zipr: }
		 = readDirectoryHeader(, )
		if  == ErrFormat ||  == io.ErrUnexpectedEOF {
			break
		}
		if  != nil {
			return 
		}
		.headerOffset += .baseOffset
		.File = append(.File, )
	}
	if uint16(len(.File)) != uint16(.directoryRecords) { // only compare 16 bits here
		// Return the readDirectoryHeader error if we read
		// the wrong number of directory entries.
		return 
	}
	if zipinsecurepath.Value() == "0" {
		for ,  := range .File {
			if .Name == "" {
				// Zip permits an empty file name field.
				continue
			}
			// The zip specification states that names must use forward slashes,
			// so consider any backslashes in the name insecure.
			if !filepath.IsLocal(.Name) || strings.Contains(.Name, `\`) {
				zipinsecurepath.IncNonDefault()
				return ErrInsecurePath
			}
		}
	}
	return nil
}

// RegisterDecompressor registers or overrides a custom decompressor for a
// specific method ID. If a decompressor for a given method is not found,
// [Reader] will default to looking up the decompressor at the package level.
func ( *Reader) ( uint16,  Decompressor) {
	if .decompressors == nil {
		.decompressors = make(map[uint16]Decompressor)
	}
	.decompressors[] = 
}

func ( *Reader) ( uint16) Decompressor {
	 := .decompressors[]
	if  == nil {
		 = decompressor()
	}
	return 
}

// Close closes the Zip file, rendering it unusable for I/O.
func ( *ReadCloser) () error {
	return .f.Close()
}

// DataOffset returns the offset of the file's possibly-compressed
// data, relative to the beginning of the zip file.
//
// Most callers should instead use [File.Open], which transparently
// decompresses data and verifies checksums.
func ( *File) () ( int64,  error) {
	,  := .findBodyOffset()
	if  != nil {
		return
	}
	return .headerOffset + , nil
}

// Open returns a [ReadCloser] that provides access to the [File]'s contents.
// Multiple files may be read concurrently.
func ( *File) () (io.ReadCloser, error) {
	,  := .findBodyOffset()
	if  != nil {
		return nil, 
	}
	if strings.HasSuffix(.Name, "/") {
		// The ZIP specification (APPNOTE.TXT) specifies that directories, which
		// are technically zero-byte files, must not have any associated file
		// data. We previously tried failing here if f.CompressedSize64 != 0,
		// but it turns out that a number of implementations (namely, the Java
		// jar tool) don't properly set the storage method on directories
		// resulting in a file with compressed size > 0 but uncompressed size ==
		// 0. We still want to fail when a directory has associated uncompressed
		// data, but we are tolerant of cases where the uncompressed size is
		// zero but compressed size is not.
		if .UncompressedSize64 != 0 {
			return &dirReader{ErrFormat}, nil
		} else {
			return &dirReader{io.EOF}, nil
		}
	}
	 := int64(.CompressedSize64)
	 := io.NewSectionReader(.zipr, .headerOffset+, )
	 := .zip.decompressor(.Method)
	if  == nil {
		return nil, ErrAlgorithm
	}
	var  io.ReadCloser = ()
	var  io.Reader
	if .hasDataDescriptor() {
		 = io.NewSectionReader(.zipr, .headerOffset++, dataDescriptorLen)
	}
	 = &checksumReader{
		rc:   ,
		hash: crc32.NewIEEE(),
		f:    ,
		desr: ,
	}
	return , nil
}

// OpenRaw returns a [Reader] that provides access to the [File]'s contents without
// decompression.
func ( *File) () (io.Reader, error) {
	,  := .findBodyOffset()
	if  != nil {
		return nil, 
	}
	 := io.NewSectionReader(.zipr, .headerOffset+, int64(.CompressedSize64))
	return , nil
}

type dirReader struct {
	err error
}

func ( *dirReader) ([]byte) (int, error) {
	return 0, .err
}

func ( *dirReader) () error {
	return nil
}

type checksumReader struct {
	rc    io.ReadCloser
	hash  hash.Hash32
	nread uint64 // number of bytes read so far
	f     *File
	desr  io.Reader // if non-nil, where to read the data descriptor
	err   error     // sticky error
}

func ( *checksumReader) () (fs.FileInfo, error) {
	return headerFileInfo{&.f.FileHeader}, nil
}

func ( *checksumReader) ( []byte) ( int,  error) {
	if .err != nil {
		return 0, .err
	}
	,  = .rc.Read()
	.hash.Write([:])
	.nread += uint64()
	if .nread > .f.UncompressedSize64 {
		return 0, ErrFormat
	}
	if  == nil {
		return
	}
	if  == io.EOF {
		if .nread != .f.UncompressedSize64 {
			return 0, io.ErrUnexpectedEOF
		}
		if .desr != nil {
			if  := readDataDescriptor(.desr, .f);  != nil {
				if  == io.EOF {
					 = io.ErrUnexpectedEOF
				} else {
					 = 
				}
			} else if .hash.Sum32() != .f.CRC32 {
				 = ErrChecksum
			}
		} else {
			// If there's not a data descriptor, we still compare
			// the CRC32 of what we've read against the file header
			// or TOC's CRC32, if it seems like it was set.
			if .f.CRC32 != 0 && .hash.Sum32() != .f.CRC32 {
				 = ErrChecksum
			}
		}
	}
	.err = 
	return
}

func ( *checksumReader) () error { return .rc.Close() }

// findBodyOffset does the minimum work to verify the file has a header
// and returns the file body offset.
func ( *File) () (int64, error) {
	var  [fileHeaderLen]byte
	if ,  := .zipr.ReadAt([:], .headerOffset);  != nil {
		return 0, 
	}
	 := readBuf([:])
	if  := .uint32();  != fileHeaderSignature {
		return 0, ErrFormat
	}
	 = [22:] // skip over most of the header
	 := int(.uint16())
	 := int(.uint16())
	return int64(fileHeaderLen +  + ), nil
}

// readDirectoryHeader attempts to read a directory header from r.
// It returns io.ErrUnexpectedEOF if it cannot read a complete header,
// and ErrFormat if it doesn't find a valid header signature.
func readDirectoryHeader( *File,  io.Reader) error {
	var  [directoryHeaderLen]byte
	if ,  := io.ReadFull(, [:]);  != nil {
		return 
	}
	 := readBuf([:])
	if  := .uint32();  != directoryHeaderSignature {
		return ErrFormat
	}
	.CreatorVersion = .uint16()
	.ReaderVersion = .uint16()
	.Flags = .uint16()
	.Method = .uint16()
	.ModifiedTime = .uint16()
	.ModifiedDate = .uint16()
	.CRC32 = .uint32()
	.CompressedSize = .uint32()
	.UncompressedSize = .uint32()
	.CompressedSize64 = uint64(.CompressedSize)
	.UncompressedSize64 = uint64(.UncompressedSize)
	 := int(.uint16())
	 := int(.uint16())
	 := int(.uint16())
	 = [4:] // skipped start disk number and internal attributes (2x uint16)
	.ExternalAttrs = .uint32()
	.headerOffset = int64(.uint32())
	 := make([]byte, ++)
	if ,  := io.ReadFull(, );  != nil {
		return 
	}
	.Name = string([:])
	.Extra = [ : +]
	.Comment = string([+:])

	// Determine the character encoding.
	,  := detectUTF8(.Name)
	,  := detectUTF8(.Comment)
	switch {
	case ! || !:
		// Name and Comment definitely not UTF-8.
		.NonUTF8 = true
	case ! && !:
		// Name and Comment use only single-byte runes that overlap with UTF-8.
		.NonUTF8 = false
	default:
		// Might be UTF-8, might be some other encoding; preserve existing flag.
		// Some ZIP writers use UTF-8 encoding without setting the UTF-8 flag.
		// Since it is impossible to always distinguish valid UTF-8 from some
		// other encoding (e.g., GBK or Shift-JIS), we trust the flag.
		.NonUTF8 = .Flags&0x800 == 0
	}

	 := .UncompressedSize == ^uint32(0)
	 := .CompressedSize == ^uint32(0)
	 := .headerOffset == int64(^uint32(0))

	// Best effort to find what we need.
	// Other zip authors might not even follow the basic format,
	// and we'll just ignore the Extra content in that case.
	var  time.Time
:
	for  := readBuf(.Extra); len() >= 4; { // need at least tag and size
		 := .uint16()
		 := int(.uint16())
		if len() <  {
			break
		}
		 := .sub()

		switch  {
		case zip64ExtraID:
			.zip64 = true

			// update directory values from the zip64 extra block.
			// They should only be consulted if the sizes read earlier
			// are maxed out.
			// See golang.org/issue/13367.
			if  {
				 = false
				if len() < 8 {
					return ErrFormat
				}
				.UncompressedSize64 = .uint64()
			}
			if  {
				 = false
				if len() < 8 {
					return ErrFormat
				}
				.CompressedSize64 = .uint64()
			}
			if  {
				 = false
				if len() < 8 {
					return ErrFormat
				}
				.headerOffset = int64(.uint64())
			}
		case ntfsExtraID:
			if len() < 4 {
				continue 
			}
			.uint32()        // reserved (ignored)
			for len() >= 4 { // need at least tag and size
				 := .uint16()
				 := int(.uint16())
				if len() <  {
					continue 
				}
				 := .sub()
				if  != 1 ||  != 24 {
					continue // Ignore irrelevant attributes
				}

				const  = 1e7    // Windows timestamp resolution
				 := int64(.uint64()) // ModTime since Windows epoch
				 :=  / 
				 := (1e9 / ) * ( % )
				 := time.Date(1601, time.January, 1, 0, 0, 0, 0, time.UTC)
				 = time.Unix(.Unix()+, )
			}
		case unixExtraID, infoZipUnixExtraID:
			if len() < 8 {
				continue 
			}
			.uint32()              // AcTime (ignored)
			 := int64(.uint32()) // ModTime since Unix epoch
			 = time.Unix(, 0)
		case extTimeExtraID:
			if len() < 5 || .uint8()&1 == 0 {
				continue 
			}
			 := int64(.uint32()) // ModTime since Unix epoch
			 = time.Unix(, 0)
		}
	}

	 := msDosTimeToTime(.ModifiedDate, .ModifiedTime)
	.Modified = 
	if !.IsZero() {
		.Modified = .UTC()

		// If legacy MS-DOS timestamps are set, we can use the delta between
		// the legacy and extended versions to estimate timezone offset.
		//
		// A non-UTC timezone is always used (even if offset is zero).
		// Thus, FileHeader.Modified.Location() == time.UTC is useful for
		// determining whether extended timestamps are present.
		// This is necessary for users that need to do additional time
		// calculations when dealing with legacy ZIP formats.
		if .ModifiedTime != 0 || .ModifiedDate != 0 {
			.Modified = .In(timeZone(.Sub()))
		}
	}

	// Assume that uncompressed size 2³²-1 could plausibly happen in
	// an old zip32 file that was sharding inputs into the largest chunks
	// possible (or is just malicious; search the web for 42.zip).
	// If needUSize is true still, it means we didn't see a zip64 extension.
	// As long as the compressed size is not also 2³²-1 (implausible)
	// and the header is not also 2³²-1 (equally implausible),
	// accept the uncompressed size 2³²-1 as valid.
	// If nothing else, this keeps archive/zip working with 42.zip.
	_ = 

	if  ||  {
		return ErrFormat
	}

	return nil
}

func readDataDescriptor( io.Reader,  *File) error {
	var  [dataDescriptorLen]byte
	// The spec says: "Although not originally assigned a
	// signature, the value 0x08074b50 has commonly been adopted
	// as a signature value for the data descriptor record.
	// Implementers should be aware that ZIP files may be
	// encountered with or without this signature marking data
	// descriptors and should account for either case when reading
	// ZIP files to ensure compatibility."
	//
	// dataDescriptorLen includes the size of the signature but
	// first read just those 4 bytes to see if it exists.
	if ,  := io.ReadFull(, [:4]);  != nil {
		return 
	}
	 := 0
	 := readBuf([:4])
	if .uint32() != dataDescriptorSignature {
		// No data descriptor signature. Keep these four
		// bytes.
		 += 4
	}
	if ,  := io.ReadFull(, [:12]);  != nil {
		return 
	}
	 := readBuf([:12])
	if .uint32() != .CRC32 {
		return ErrChecksum
	}

	// The two sizes that follow here can be either 32 bits or 64 bits
	// but the spec is not very clear on this and different
	// interpretations has been made causing incompatibilities. We
	// already have the sizes from the central directory so we can
	// just ignore these.

	return nil
}

func readDirectoryEnd( io.ReaderAt,  int64) ( *directoryEnd,  int64,  error) {
	// look for directoryEndSignature in the last 1k, then in the last 65k
	var  []byte
	var  int64
	for ,  := range []int64{1024, 65 * 1024} {
		if  >  {
			 = 
		}
		 = make([]byte, int())
		if ,  := .ReadAt(, -);  != nil &&  != io.EOF {
			return nil, 0, 
		}
		if  := findSignatureInBlock();  >= 0 {
			 = [:]
			 =  -  + int64()
			break
		}
		if  == 1 ||  ==  {
			return nil, 0, ErrFormat
		}
	}

	// read header into struct
	 := readBuf([4:]) // skip signature
	 := &directoryEnd{
		diskNbr:            uint32(.uint16()),
		dirDiskNbr:         uint32(.uint16()),
		dirRecordsThisDisk: uint64(.uint16()),
		directoryRecords:   uint64(.uint16()),
		directorySize:      uint64(.uint32()),
		directoryOffset:    uint64(.uint32()),
		commentLen:         .uint16(),
	}
	 := int(.commentLen)
	if  > len() {
		return nil, 0, errors.New("zip: invalid comment length")
	}
	.comment = string([:])

	// These values mean that the file can be a zip64 file
	if .directoryRecords == 0xffff || .directorySize == 0xffff || .directoryOffset == 0xffffffff {
		,  := findDirectory64End(, )
		if  == nil &&  >= 0 {
			 = 
			 = readDirectory64End(, , )
		}
		if  != nil {
			return nil, 0, 
		}
	}

	 := uint64(1<<63 - 1)
	if .directorySize >  || .directoryOffset >  {
		return nil, 0, ErrFormat
	}

	 =  - int64(.directorySize) - int64(.directoryOffset)

	// Make sure directoryOffset points to somewhere in our file.
	if  :=  + int64(.directoryOffset);  < 0 ||  >=  {
		return nil, 0, ErrFormat
	}

	// If the directory end data tells us to use a non-zero baseOffset,
	// but we would find a valid directory entry if we assume that the
	// baseOffset is 0, then just use a baseOffset of 0.
	// We've seen files in which the directory end data gives us
	// an incorrect baseOffset.
	if  > 0 {
		 := int64(.directoryOffset)
		 := io.NewSectionReader(, , -)
		if readDirectoryHeader(&File{}, ) == nil {
			 = 0
		}
	}

	return , , nil
}

// findDirectory64End tries to read the zip64 locator just before the
// directory end and returns the offset of the zip64 directory end if
// found.
func findDirectory64End( io.ReaderAt,  int64) (int64, error) {
	 :=  - directory64LocLen
	if  < 0 {
		return -1, nil // no need to look for a header outside the file
	}
	 := make([]byte, directory64LocLen)
	if ,  := .ReadAt(, );  != nil {
		return -1, 
	}
	 := readBuf()
	if  := .uint32();  != directory64LocSignature {
		return -1, nil
	}
	if .uint32() != 0 { // number of the disk with the start of the zip64 end of central directory
		return -1, nil // the file is not a valid zip64-file
	}
	 := .uint64()      // relative offset of the zip64 end of central directory record
	if .uint32() != 1 { // total number of disks
		return -1, nil // the file is not a valid zip64-file
	}
	return int64(), nil
}

// readDirectory64End reads the zip64 directory end and updates the
// directory end with the zip64 directory end values.
func readDirectory64End( io.ReaderAt,  int64,  *directoryEnd) ( error) {
	 := make([]byte, directory64EndLen)
	if ,  := .ReadAt(, );  != nil {
		return 
	}

	 := readBuf()
	if  := .uint32();  != directory64EndSignature {
		return ErrFormat
	}

	 = [12:]                        // skip dir size, version and version needed (uint64 + 2x uint16)
	.diskNbr = .uint32()            // number of this disk
	.dirDiskNbr = .uint32()         // number of the disk with the start of the central directory
	.dirRecordsThisDisk = .uint64() // total number of entries in the central directory on this disk
	.directoryRecords = .uint64()   // total number of entries in the central directory
	.directorySize = .uint64()      // size of the central directory
	.directoryOffset = .uint64()    // offset of start of central directory with respect to the starting disk number

	return nil
}

func findSignatureInBlock( []byte) int {
	for  := len() - directoryEndLen;  >= 0; -- {
		// defined from directoryEndSignature in struct.go
		if [] == 'P' && [+1] == 'K' && [+2] == 0x05 && [+3] == 0x06 {
			// n is length of comment
			 := int([+directoryEndLen-2]) | int([+directoryEndLen-1])<<8
			if +directoryEndLen+ <= len() {
				return 
			}
		}
	}
	return -1
}

type readBuf []byte

func ( *readBuf) () uint8 {
	 := (*)[0]
	* = (*)[1:]
	return 
}

func ( *readBuf) () uint16 {
	 := binary.LittleEndian.Uint16(*)
	* = (*)[2:]
	return 
}

func ( *readBuf) () uint32 {
	 := binary.LittleEndian.Uint32(*)
	* = (*)[4:]
	return 
}

func ( *readBuf) () uint64 {
	 := binary.LittleEndian.Uint64(*)
	* = (*)[8:]
	return 
}

func ( *readBuf) ( int) readBuf {
	 := (*)[:]
	* = (*)[:]
	return 
}

// A fileListEntry is a File and its ename.
// If file == nil, the fileListEntry describes a directory without metadata.
type fileListEntry struct {
	name  string
	file  *File
	isDir bool
	isDup bool
}

type fileInfoDirEntry interface {
	fs.FileInfo
	fs.DirEntry
}

func ( *fileListEntry) () (fileInfoDirEntry, error) {
	if .isDup {
		return nil, errors.New(.name + ": duplicate entries in zip file")
	}
	if !.isDir {
		return headerFileInfo{&.file.FileHeader}, nil
	}
	return , nil
}

// Only used for directories.
func ( *fileListEntry) () string      { , ,  := split(.name); return  }
func ( *fileListEntry) () int64       { return 0 }
func ( *fileListEntry) () fs.FileMode { return fs.ModeDir | 0555 }
func ( *fileListEntry) () fs.FileMode { return fs.ModeDir }
func ( *fileListEntry) () bool       { return true }
func ( *fileListEntry) () any          { return nil }

func ( *fileListEntry) () time.Time {
	if .file == nil {
		return time.Time{}
	}
	return .file.FileHeader.Modified.UTC()
}

func ( *fileListEntry) () (fs.FileInfo, error) { return , nil }

func ( *fileListEntry) () string {
	return fs.FormatDirEntry()
}

// toValidName coerces name to be a valid name for fs.FS.Open.
func toValidName( string) string {
	 = strings.ReplaceAll(, `\`, `/`)
	 := path.Clean()

	 = strings.TrimPrefix(, "/")

	for strings.HasPrefix(, "../") {
		 = [len("../"):]
	}

	return 
}

func ( *Reader) () {
	.fileListOnce.Do(func() {
		// files and knownDirs map from a file/directory name
		// to an index into the r.fileList entry that we are
		// building. They are used to mark duplicate entries.
		 := make(map[string]int)
		 := make(map[string]int)

		// dirs[name] is true if name is known to be a directory,
		// because it appears as a prefix in a path.
		 := make(map[string]bool)

		for ,  := range .File {
			 := len(.Name) > 0 && .Name[len(.Name)-1] == '/'
			 := toValidName(.Name)
			if  == "" {
				continue
			}

			if ,  := [];  {
				.fileList[].isDup = true
				continue
			}
			if ,  := [];  {
				.fileList[].isDup = true
				continue
			}

			for  := path.Dir();  != ".";  = path.Dir() {
				[] = true
			}

			 := len(.fileList)
			 := fileListEntry{
				name:  ,
				file:  ,
				isDir: ,
			}
			.fileList = append(.fileList, )
			if  {
				[] = 
			} else {
				[] = 
			}
		}
		for  := range  {
			if ,  := []; ! {
				if ,  := [];  {
					.fileList[].isDup = true
				} else {
					 := fileListEntry{
						name:  ,
						file:  nil,
						isDir: true,
					}
					.fileList = append(.fileList, )
				}
			}
		}

		sort.Slice(.fileList, func(,  int) bool { return fileEntryLess(.fileList[].name, .fileList[].name) })
	})
}

func fileEntryLess(,  string) bool {
	, ,  := split()
	, ,  := split()
	return  <  ||  ==  &&  < 
}

// Open opens the named file in the ZIP archive,
// using the semantics of fs.FS.Open:
// paths are always slash separated, with no
// leading / or ../ elements.
func ( *Reader) ( string) (fs.File, error) {
	.initFileList()

	if !fs.ValidPath() {
		return nil, &fs.PathError{Op: "open", Path: , Err: fs.ErrInvalid}
	}
	 := .openLookup()
	if  == nil {
		return nil, &fs.PathError{Op: "open", Path: , Err: fs.ErrNotExist}
	}
	if .isDir {
		return &openDir{, .openReadDir(), 0}, nil
	}
	,  := .file.Open()
	if  != nil {
		return nil, 
	}
	return .(fs.File), nil
}

func split( string) (,  string,  bool) {
	if len() > 0 && [len()-1] == '/' {
		 = true
		 = [:len()-1]
	}
	 := len() - 1
	for  >= 0 && [] != '/' {
		--
	}
	if  < 0 {
		return ".", , 
	}
	return [:], [+1:], 
}

var dotFile = &fileListEntry{name: "./", isDir: true}

func ( *Reader) ( string) *fileListEntry {
	if  == "." {
		return dotFile
	}

	, ,  := split()
	 := .fileList
	 := sort.Search(len(), func( int) bool {
		, ,  := split([].name)
		return  >  ||  ==  &&  >= 
	})
	if  < len() {
		 := [].name
		if  ==  || len() == len()+1 && [len()] == '/' && [:len()] ==  {
			return &[]
		}
	}
	return nil
}

func ( *Reader) ( string) []fileListEntry {
	 := .fileList
	 := sort.Search(len(), func( int) bool {
		, ,  := split([].name)
		return  >= 
	})
	 := sort.Search(len(), func( int) bool {
		, ,  := split([].name)
		return  > 
	})
	return [:]
}

type openDir struct {
	e      *fileListEntry
	files  []fileListEntry
	offset int
}

func ( *openDir) () error               { return nil }
func ( *openDir) () (fs.FileInfo, error) { return .e.stat() }

func ( *openDir) ([]byte) (int, error) {
	return 0, &fs.PathError{Op: "read", Path: .e.name, Err: errors.New("is a directory")}
}

func ( *openDir) ( int) ([]fs.DirEntry, error) {
	 := len(.files) - .offset
	if  > 0 &&  >  {
		 = 
	}
	if  == 0 {
		if  <= 0 {
			return nil, nil
		}
		return nil, io.EOF
	}
	 := make([]fs.DirEntry, )
	for  := range  {
		,  := .files[.offset+].stat()
		if  != nil {
			return nil, 
		}
		[] = 
	}
	.offset += 
	return , nil
}