This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
teamcity-executors-test-task/src/main/kotlin/ziputils/EndOfCentralDirectoryLocator.kt

35 lines
1.3 KiB
Kotlin

package ziputils
import java.nio.ByteBuffer
import java.nio.ByteOrder
/**
* Represents a partial ZIP64 end of central directory locator.
*/
internal class EndOfCentralDirectoryLocator(
val endOfCentralDirectory64Offset: ULong
) {
companion object {
const val SIGNATURE = 0x07064b50U
const val SIZE = 20
/**
* Create `EndOfCentralDirectoryLocator` from raw byte data.
* @throws InvalidDataException Provided `ByteArray` is not a supported EOCD locator.
* @param data Raw byte data.
* @param offset Skip first <offset> bytes in data array.
* @return A `EndOfCentralDirectoryLocator`.
*/
@Throws(InvalidDataException::class)
fun fromByteArray(data: ByteArray, offset: Int): EndOfCentralDirectoryLocator {
if (data.size - offset < SIZE) {
throw InvalidDataException("EOCD64 locator must be at least 20 bytes")
}
val buf = ByteBuffer.wrap(data, offset, SIZE).order(ByteOrder.LITTLE_ENDIAN)
if (buf.getInt().toUInt() != SIGNATURE) {
throw InvalidSignatureException("Invalid signature")
}
buf.position(offset + 8)
return EndOfCentralDirectoryLocator(buf.getLong().toULong())
}
}
}