mirror of
https://github.com/0x5eal/luau-unzip.git
synced 2025-04-10 17:20:53 +01:00
fix: avoid duplicate directory entries with trailing slash
Fixed a bug in the directory tree builder where for directories there would be two entries, where one would have a trailing slash, and the other wouldn't. The directory without the trailing slash would get linked to its children in the entries lookup table, while the one with the trailing slash would exist as a "ghost directory" with no children. The fix involved sorting the entries to first handle directories, and linking to the existing parent directory entries for children.
This commit is contained in:
parent
7724af0467
commit
ccc7228278
1 changed files with 53 additions and 35 deletions
|
@ -165,10 +165,9 @@ function ZipReader.parseCentralDirectory(self: ZipReader): ()
|
|||
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 name = buffer.readstring(self.data, pos + 46, nameLength)
|
||||
|
||||
local nameBuffer = buffer.create(nameLength)
|
||||
buffer.copy(nameBuffer, 0, self.data, pos + 46, nameLength)
|
||||
local name = buffer.tostring(nameBuffer)
|
||||
print("got name:", name)
|
||||
|
||||
local entry = ZipEntry.new(name, size, offset, timestamp, crc)
|
||||
table.insert(self.entries, entry)
|
||||
|
@ -178,6 +177,17 @@ function ZipReader.parseCentralDirectory(self: ZipReader): ()
|
|||
end
|
||||
|
||||
function ZipReader.buildDirectoryTree(self: ZipReader): ()
|
||||
-- Sort entries to process directories first; I could either handle
|
||||
-- directories and files in separate passes over the entries, or sort
|
||||
-- the entries so I handled the directories first -- I decided to do
|
||||
-- the latter
|
||||
table.sort(self.entries, function(a, b)
|
||||
if a.isDirectory ~= b.isDirectory then
|
||||
return a.isDirectory
|
||||
end
|
||||
return a.name < b.name
|
||||
end)
|
||||
|
||||
for _, entry in self.entries do
|
||||
local parts = {}
|
||||
-- Split entry path into individual components
|
||||
|
@ -193,16 +203,24 @@ function ZipReader.buildDirectoryTree(self: ZipReader): ()
|
|||
-- Process each path component
|
||||
for i, part in parts do
|
||||
path ..= part
|
||||
if i < #parts then
|
||||
|
||||
if i < #parts or entry.isDirectory then
|
||||
-- Create missing directory entries for intermediate paths
|
||||
if not self.directories[path] then
|
||||
local dir = ZipEntry.new(path, 0, 0, entry.timestamp, 0)
|
||||
if entry.isDirectory and i == #parts then
|
||||
-- Existing directory entry, reuse it
|
||||
self.directories[path] = entry
|
||||
else
|
||||
-- Create new directory entry for intermediate paths or undefined
|
||||
-- parent directories in the ZIP
|
||||
local dir = ZipEntry.new(path .. "/", 0, 0, entry.timestamp, 0)
|
||||
dir.isDirectory = true
|
||||
dir.parent = current
|
||||
self.directories[path] = dir
|
||||
end
|
||||
|
||||
-- Track directory in both lookup table and parent's children
|
||||
self.directories[path] = dir
|
||||
table.insert(current.children, dir)
|
||||
table.insert(current.children, self.directories[path])
|
||||
end
|
||||
|
||||
-- Move deeper into the tree
|
||||
|
|
Loading…
Add table
Reference in a new issue