chore: init, the basics

Implement core `ZipReader`, `ZipEntry` and basic API. Supports simple
zip format deserialization, without advanced features, or decompression.
This commit is contained in:
Erica Marigold 2024-12-28 17:19:37 +00:00
commit b3777ba2b1
Signed by: DevComp
GPG key ID: 429EF1C337871656
8 changed files with 367 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
test.zip
*_packages/

6
.luaurc Normal file
View file

@ -0,0 +1,6 @@
{
"languageMode": "strict",
"aliases": {
"pkg": "./lune_packages"
}
}

6
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,6 @@
{
"luau-lsp.require.mode": "relativeToFile",
"luau-lsp.require.directoryAliases": {
"@lune/": "~/.lune/.typedefs/0.8.9/"
}
}

7
LICENSE.md Normal file
View file

@ -0,0 +1,7 @@
Copyright © 2024 CompeyDev
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

254
lib/init.luau Normal file
View file

@ -0,0 +1,254 @@
local SIGNATURES = table.freeze({
LOCAL_FILE = 0x04034b50,
CENTRAL_DIR = 0x02014b50,
END_OF_CENTRAL_DIR = 0x06054b50,
})
-- TODO: ERROR HANDLING !!
local ZipEntry = {}
export type ZipEntry = typeof(setmetatable({} :: ZipEntryInner, { __index = ZipEntry }))
type ZipEntryInner = {
name: string,
size: number,
offset: number,
timestamp: number,
crc: number,
isDirectory: boolean,
parent: ZipEntry?,
children: { ZipEntry },
getPath: (ZipEntry) -> string,
}
function ZipEntry.new(name, size, offset, timestamp, crc): ZipEntry
return setmetatable(
{
name = name,
size = size,
offset = offset,
timestamp = timestamp,
crc = crc,
isDirectory = string.sub(name, -1) == "/",
parent = nil,
children = {},
} :: ZipEntryInner,
{ __index = ZipEntry }
)
end
function ZipEntry.getPath(self: ZipEntry): string
local path = self.name
local current = self.parent
while current and current.name ~= "/" do
path = current.name .. path
current = current.parent
end
return path
end
local ZipReader = {}
export type ZipReader = typeof(setmetatable({} :: ZipReaderInner, { __index = ZipReader }))
type ZipReaderInner = {
data: buffer,
entries: { ZipEntry },
directories: { [string]: ZipEntry },
root: ZipEntry,
}
function ZipReader.new(data): ZipReader
local root = ZipEntry.new("/", 0, 0, 0, 0)
root.isDirectory = true
local this = setmetatable(
{
data = data,
entries = {},
directories = {},
root = root,
} :: ZipReaderInner,
{ __index = ZipReader }
)
this:parseCentralDirectory()
this:buildDirectoryTree()
return this
end
function ZipReader.parseCentralDirectory(self: ZipReader): ()
local bufSize = buffer.len(self.data)
local pos = bufSize - 22
while pos > 0 do
if buffer.readu32(self.data, pos) == SIGNATURES.END_OF_CENTRAL_DIR then
break
end
pos = pos - 1
end
if pos < 0 then
error("Invalid ZIP file: End of Central Directory not found")
end
local cdOffset = buffer.readu32(self.data, pos + 16)
local cdEntries = buffer.readu16(self.data, pos + 10)
pos = cdOffset
for i = 1, cdEntries do
if buffer.readu32(self.data, pos) ~= SIGNATURES.CENTRAL_DIR then
error("Invalid central directory header")
end
local nameLength = buffer.readu16(self.data, pos + 28)
local extraLength = buffer.readu16(self.data, pos + 30)
local commentLength = buffer.readu16(self.data, pos + 32)
local timestamp = buffer.readu32(self.data, pos + 12)
local crc = buffer.readu32(self.data, pos + 16)
local size = buffer.readu32(self.data, pos + 24)
local offset = buffer.readu32(self.data, pos + 42)
local nameBuffer = buffer.create(nameLength)
buffer.copy(nameBuffer, 0, self.data, pos + 46, nameLength)
local name = buffer.tostring(nameBuffer)
local entry = ZipEntry.new(name, size, offset, timestamp, crc)
table.insert(self.entries, entry)
pos = pos + 46 + nameLength + extraLength + commentLength
end
end
function ZipReader.buildDirectoryTree(self: ZipReader): ()
for _, entry in self.entries do
local parts = {}
for part in string.gmatch(entry.name, "([^/]+)/?") do
table.insert(parts, part)
end
local current = self.root
local path = ""
for i, part in parts do
path ..= part
if i < #parts then
if not self.directories[path] then
local dir = ZipEntry.new(path, 0, 0, entry.timestamp, 0)
dir.isDirectory = true
dir.parent = current
self.directories[path] = dir
table.insert(current.children, dir)
end
current = self.directories[path]
continue
end
entry.parent = current
table.insert(current.children, entry)
end
end
end
function ZipReader.findEntry(self: ZipReader, path: string): ZipEntry
if path == "/" then
return self.root
end
path = string.gsub(path, "^/", ""):gsub("/$", "")
for _, entry in self.entries do
if string.gsub(entry.name, "/$", "") == path then
return entry
end
end
return self.directories[path]
end
function ZipReader.extract(self: ZipReader, entry: ZipEntry): buffer
if entry.isDirectory then
error("Cannot extract directory")
end
local pos = entry.offset
if buffer.readu32(self.data, pos) ~= SIGNATURES.LOCAL_FILE then
error("Invalid local file header")
end
local nameLength = buffer.readu16(self.data, pos + 26)
local extraLength = buffer.readu16(self.data, pos + 28)
pos = pos + 30 + nameLength + extraLength
local content = buffer.create(entry.size)
buffer.copy(content, 0, self.data, pos, entry.size)
-- TODO: decompress data! `buffer.readu16(self.data, entry.offset + 8)`
-- will give the compression method, where method id 8 corresponds to
-- deflate
return content
end
function ZipReader.extractDirectory(self: ZipReader, path: string): { [string]: buffer }
local files = {}
path = string.gsub(path, "^/", "")
for _, entry in self.entries do
if not entry.isDirectory and string.sub(entry.name, 1, #path) == path then
files[entry.name] = self:extract(entry)
end
end
return files
end
function ZipReader.listDirectory(self: ZipReader, path: string): { ZipEntry }
local entry = self:findEntry(path)
if not entry or not entry.isDirectory then
error("Not a directory")
end
return entry.children
end
function ZipReader.walk(self: ZipReader, callback: (entry: ZipEntry, depth: number) -> ()): ()
local function walkEntry(entry: ZipEntry, depth: number)
callback(entry, depth)
for _, child in entry.children do
-- ooo spooky recursion... blame this if shit go wrong
walkEntry(child, depth + 1)
end
end
walkEntry(self.root, 0)
end
export type ZipStatistics = { fileCount: number, dirCount: number, totalSize: number }
function ZipReader.getStats(self: ZipReader): ZipStatistics
local stats: ZipStatistics = {
fileCount = 0,
dirCount = 0,
totalSize = 0,
}
for _, entry in self.entries do
if entry.isDirectory then
stats.dirCount = stats.dirCount + 1
continue
end
stats.fileCount = stats.fileCount + 1
stats.totalSize = stats.totalSize + entry.size
end
return stats
end
return {
load = function(data)
return ZipReader.new(data)
end,
}

39
pesde.lock Normal file
View file

@ -0,0 +1,39 @@
name = "0x5eal/unzip"
version = "0.1.0"
target = "luau"
[graph."jiwonz/pathfs"."0.1.0 lune"]
direct = ["pathfs", { name = "jiwonz/pathfs", version = "^0.1.0", target = "lune" }, "standard"]
resolved_ty = "standard"
[graph."jiwonz/pathfs"."0.1.0 lune".target]
environment = "lune"
lib = "init.luau"
[graph."jiwonz/pathfs"."0.1.0 lune".pkg_ref]
ref_ty = "pesde"
name = "jiwonz/pathfs"
version = "0.1.0"
index_url = "https://github.com/pesde-pkg/index"
[graph."jiwonz/pathfs"."0.1.0 lune".pkg_ref.target]
environment = "lune"
lib = "init.luau"
[graph."lukadev_0/result"."1.2.0 luau"]
direct = ["result", { name = "lukadev_0/result", version = "^1.2.0" }, "standard"]
resolved_ty = "standard"
[graph."lukadev_0/result"."1.2.0 luau".target]
environment = "luau"
lib = "lib/init.luau"
[graph."lukadev_0/result"."1.2.0 luau".pkg_ref]
ref_ty = "pesde"
name = "lukadev_0/result"
version = "1.2.0"
index_url = "https://github.com/pesde-pkg/index"
[graph."lukadev_0/result"."1.2.0 luau".pkg_ref.target]
environment = "luau"
lib = "lib/init.luau"

16
pesde.toml Normal file
View file

@ -0,0 +1,16 @@
name = "0x5eal/unzip"
version = "0.1.0"
description = "unzip implementation in pure Luau"
authors = ["Erica Marigold <hi@devcomp.xyz>"]
repository = "https://github.com/0x5eal/luau-unzip"
license = "MIT"
[target]
environment = "luau"
[indices]
default = "https://github.com/pesde-pkg/index"
[dependencies]
result = { name = "lukadev_0/result", version = "^1.2.0" }
pathfs = { name = "jiwonz/pathfs", version = "^0.1.0", target = "lune" }

37
test.luau Normal file
View file

@ -0,0 +1,37 @@
local fs = require("@lune/fs")
local zip = require("./lib")
local file = fs.readFile("test.zip")
local reader = zip.load(buffer.fromstring(file))
print("Directory structure:")
reader:walk(function(entry, depth)
local prefix = string.rep(" ", depth)
local suffix = if not entry.isDirectory then string.format(" (%d bytes)", entry.size) else ""
print(prefix .. entry.name .. suffix)
end)
print("\nContents of /lib/:")
local assets = reader:listDirectory("/lib/")
for _, entry in ipairs(assets) do
print(entry.name, entry.isDirectory and "DIR" or entry.size)
end
local configEntry = reader:findEntry("config.json")
if configEntry then
local content = reader:extract(configEntry)
print("\nConfig file size:", buffer.len(content))
end
-- Get archive statistics
local stats = reader:getStats()
print(string.format([[
Archive stats:
Files: %d
Directories: %d
Total size: %d bytes]],
stats.fileCount,
stats.dirCount,
stats.totalSize
))