Repository: sxyazi/yazi
File: yazi-plugin/preset/plugins/archive.lua (Lua)

Code before:
		if next == "\n" or next == "\r\n" then
			if files[#files].path ~= empty then
				M.treelize(files, parents)
				files[#files + 1] = M.make_file()
			end
			goto continue
		end

		key, value = next:match("^(%u[%a ]+) = (.-)[\r\n]+")
		if key == "Path" then
			files[#files].path = Path.os(value)
		elseif key == "Size" then
			files[#files].size = tonumber(value) or 0
		elseif key == "Packed Size" then
			files[#files].packed_size = tonumber(value) or 0
		elseif key == "Attributes" then
			files[#files].attr = value
		elseif key == "Folder" then
			files[#files].folder = value
		end

		::continue::
	until #files > skip + limit

	if files[#files].path == Path.os("") then
		files[#files] = nil
	end
	if #stderr ~= 0 then
		err = Err("7-zip errored out while listing files, stderr: %s", table.concat(stderr, "\n"))
	end
	return files, err
end

---Convert a flat list of files into a tree structure
---@param files table
---@param parents Path[]
function M.treelize(files, parents)
	local f = table.remove(files)
	while #parents > 0 and not f.path:starts_with(parents[#parents]) do
		parents[#parents] = nil
	end

	local buf, it = {}, f.path.parent
	while it and it ~= parents[#parents] do
		buf[#buf + 1], it = it, it.parent
	end
	for i = #buf, 1, -1 do
		files[#files + 1] = M.make_file { path = buf[i], depth = #parents, is_dir = true }
		parents[#parents + 1] = buf[i]
	end


Review comment (suggestion):
If the last file in the stream doesn't end with a blank line, it won't go through the treelize function (which is only called at line 254 when a blank line is encountered). This means the last file may not have the `depth` and `is_dir` fields set, which will cause issues when rendering (line 41 uses `f.depth`, line 25 uses `f.is_dir`). Consider calling treelize for the last file if it has a non-empty path before checking at line 276.
```suggestion

	-- Ensure the last file is treelized if it represents a real entry
	if files[#files].path ~= empty then
		M.treelize(files, parents)
	end
```

Code after:
		end

		if next == "\n" or next == "\r\n" then
			if files[#files].path ~= empty then
				M.treelize(files, parents)
				files[#files + 1] = M.make_file()
			end
			goto continue
		end

		key, value = next:match("^(%u[%a ]+) = (.-)[\r\n]+")
		if key == "Path" then
			files[#files].path = Path.os(value)
		elseif key == "Size" then
			files[#files].size = tonumber(value) or 0
		elseif key == "Packed Size" then
			files[#files].packed_size = tonumber(value) or 0
		elseif key == "Attributes" then
			files[#files].attr = value
		elseif key == "Folder" then
			files[#files].folder = value
		end

		::continue::
	until #files > skip + limit

	if files[#files].path == empty then
		files[#files] = nil
	else
		M.treelize(files, parents)
	end

	if #stderr ~= 0 then
		err = Err("7-zip errored out while listing files, stderr: %s", table.concat(stderr, "\n"))
	end
	return files, err
end

---Convert a flat list of files into a tree structure
---@param files table
---@param parents Path[]
function M.treelize(files, parents)
	local f = table.remove(files)
	while #parents > 0 and not f.path:starts_with(parents[#parents]) do
		parents[#parents] = nil
	end

	local buf, it = {}, f.path.parent
	while it and it ~= parents[#parents] do
		buf[#buf + 1], it = it, it.parent
	end