增加获取文件大小

This commit is contained in:
a158
2026-01-14 22:09:20 +08:00
parent 6615067168
commit 1ad806f8c8

View File

@@ -125,6 +125,16 @@ M.exists_file = function(filepath)
return false
end
end
-- 取文件大小
M.file_size = function(filepath)
local file = io.open(filepath, "rb")
if not file then
return nil
end
local size = file:seek("end")
file:close()
return size
end
-- 取近N个月时间
M.recent_months = function(n)
local mons = {}
@@ -292,4 +302,44 @@ M.traverse_dir = function(dirpath)
end
return files
end
-- 数组是否一致(不考虑顺序)
-- 只考虑 string 和 number/integer 类型,其它类型忽略
M.array_equal = function(a, b)
-- 辅助函数判断是否为有效类型string 或 number
local function is_valid_type(v)
return type(v) == "string" or type(v) == "number"
end
-- 统计数组中有效类型元素的值及其出现次数
local function count_values(arr)
local counts = {}
for _, v in ipairs(arr) do
if is_valid_type(v) then
local key = tostring(v) -- 统一转换为字符串作为key
counts[key] = (counts[key] or 0) + 1
end
end
return counts
end
-- 统计两个数组的有效元素
local counts_a = count_values(a)
local counts_b = count_values(b)
-- 检查 counts_a 中的所有键值对是否在 counts_b 中匹配
for key, count in pairs(counts_a) do
if counts_b[key] ~= count then
return false
end
end
-- 检查 counts_b 中的所有键值对是否在 counts_a 中匹配
for key, count in pairs(counts_b) do
if counts_a[key] ~= count then
return false
end
end
return true
end
return M