Skip to content

Commit

Permalink
Remove commented out zip.appendbytes
Browse files Browse the repository at this point in the history
  • Loading branch information
ruvmello committed Dec 2, 2023
1 parent a9f7c80 commit 4243d96
Showing 1 changed file with 2 additions and 29 deletions.
31 changes: 2 additions & 29 deletions src/main/kotlin/zip/ZIPArchiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class ZIPArchiver(private val zipName: String = "test.zip", private val debug: B
zip.appendBytes(file.name.encodeToByteArray())

// Extra field content
// zip.appendBytes(getByteArrayOf2Bytes(0))

}

/**
Expand Down Expand Up @@ -273,9 +273,6 @@ class ZIPArchiver(private val zipName: String = "test.zip", private val debug: B
val zipVersionMadeBy: ByteArray = byteArrayOf(0x14, 0x00)
val zipVersionNeededToExtract: ByteArray = byteArrayOf(0x14, 0x00)

// zip.appendBytes(zipSignature)
// zip.appendBytes(zipVersionMadeBy)
// zip.appendBytes(zipVersionNeededToExtract)
data += zipSignature
data += zipVersionMadeBy
data += zipVersionNeededToExtract
Expand All @@ -286,29 +283,23 @@ class ZIPArchiver(private val zipName: String = "test.zip", private val debug: B
val comment = ""

// File comment length
// zip.appendBytes(getByteArrayOf2Bytes(comment.length))
data += getByteArrayOf2Bytes(comment.length)

// Number of disk
// zip.appendBytes(getByteArrayOf2Bytes(0))
data += getByteArrayOf2Bytes(0)

// Internal attributes, https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip.html
val internalAttributes = byteArrayOf(0x01, 0x00) // First bit set -> text file, TODO: Extend for other files
// zip.appendBytes(internalAttributes)
data += internalAttributes

// External attributes
val externalAttributes = byteArrayOf(0x02, 0x00, 0x00, 0x00) // Lower byte -> zip spec version, TODO: is the other mapping needed?
// zip.appendBytes(externalAttributes)
data += externalAttributes

// Offset local header, TODO: is this needed? (windows just sets 0 when compressing)
// zip.appendBytes(getByteArrayOf4Bytes(0))
data += getByteArrayOf4Bytes(0)

// File name
// zip.appendBytes(file.name.encodeToByteArray())
data += file.name.encodeToByteArray()

// Extra field content
Expand All @@ -317,7 +308,7 @@ class ZIPArchiver(private val zipName: String = "test.zip", private val debug: B
// File comment
if (comment.isNotEmpty())
data += comment.encodeToByteArray()
// zip.appendBytes(comment.encodeToByteArray())

return data
}

Expand All @@ -333,40 +324,31 @@ class ZIPArchiver(private val zipName: String = "test.zip", private val debug: B
var data = byteArrayOf()

val zipSignature: ByteArray = byteArrayOf(0x50, 0x4b, 0x05, 0x06)
// zip.appendBytes(zipSignature)
data += zipSignature

// Disk number
// zip.appendBytes(getByteArrayOf2Bytes(0))
data += getByteArrayOf2Bytes(0)

// Number of disks on which the central directory starts
// zip.appendBytes(getByteArrayOf2Bytes(0))
data += getByteArrayOf2Bytes(0)

// Disk entries
// zip.appendBytes(getByteArrayOf2Bytes(numberOfFiles))
data += getByteArrayOf2Bytes(numberOfFiles)

// Total entries
// zip.appendBytes(getByteArrayOf2Bytes(numberOfFiles))
data += getByteArrayOf2Bytes(numberOfFiles)

// Central Directory size
// zip.appendBytes(getByteArrayOf4Bytes(size))
data += getByteArrayOf4Bytes(size)

// Offset of the start of the central directory on the disk on which the central directory starts, TODO
// zip.appendBytes(getByteArrayOf4Bytes(offset))
data += getByteArrayOf4Bytes(offset)

val comment = ""
// Comment length
// zip.appendBytes(getByteArrayOf2Bytes(comment.length))
data += getByteArrayOf2Bytes(comment.length)

// Comment
// zip.appendBytes(comment.encodeToByteArray())
data += comment.encodeToByteArray()
return data
}
Expand All @@ -383,43 +365,34 @@ class ZIPArchiver(private val zipName: String = "test.zip", private val debug: B
val zipFlags: ByteArray = byteArrayOf(0x00, 0x00)
val zipCompressionMethod: ByteArray = byteArrayOf(0x08, 0x00)

// zip.appendBytes(zipFlags)
// zip.appendBytes(zipCompressionMethod)
data += zipFlags
data += zipCompressionMethod

// File modification time
var timeAsInt: Int = datetime.hour
timeAsInt = timeAsInt shl 6 xor datetime.minute
timeAsInt = timeAsInt shl 5 xor (datetime.second / 2)
// zip.appendBytes(getByteArrayOf2Bytes(timeAsInt))
data += getByteArrayOf2Bytes(timeAsInt)

// File modification date
var dateAsInt: Int = datetime.year - 1980
dateAsInt = dateAsInt shl 4 xor datetime.monthValue
dateAsInt = dateAsInt shl 5 xor datetime.dayOfMonth
// zip.appendBytes(getByteArrayOf2Bytes(dateAsInt))
data += getByteArrayOf2Bytes(dateAsInt)

// CRC32-Checksum
// zip.appendBytes(getByteArrayOf4Bytes(calculateCRC32(file.readBytes())))
data += getByteArrayOf4Bytes(calculateCRC32(file.readBytes()))

// Compressed size
// zip.appendBytes(getByteArrayOf4Bytes(compressedSize))
data += getByteArrayOf4Bytes(compressedSize)

// Uncompressed size
// zip.appendBytes(getByteArrayOf4Bytes(file.length().toInt()))
data += getByteArrayOf4Bytes(file.length().toInt())

// File name length
// zip.appendBytes(getByteArrayOf2Bytes(file.name.length))
data += getByteArrayOf2Bytes(file.name.length)

// Extra field length
// zip.appendBytes(getByteArrayOf2Bytes(0))
data += getByteArrayOf2Bytes(0)

return data
Expand Down

0 comments on commit 4243d96

Please sign in to comment.