#!Lua-5.0.exe -- FileSplitJoint.lua: Split a file in several smaller parts or joint parts of a file previously splitted. -- -- It tries to be "smart" by checking which files are here: -- If the given filename exists, it will split it in files with same name, -- but with suffixes .001 .002 etc. -- If the given filename.001 exists, it will joint the parts into filename. -- If both exists, it issues an error. -- -- by Philippe Lhoste http://Phi.Lho.free.fr -- v. 1.0 -- 2003/11/20 local filename = arg and arg[1] local partSize = 1024 * 1024 -- One megabyte fits well on a floppy disk... --local partSize = 4 * 1024 -- For Test local part local partNb = 0 -- If file has no read access or doesn't exist, return nil. -- Otherwise, return the size of the file, in bytes (can be 0). function GetFileSize(filename) local res local f = io.open(filename, 'rb') if f == nil then return nil end res = f:seek('end') f:close() return res end function DoSplit(filenameIn) print("Splitting '" .. filenameIn .. "'...") local filenameOut, fo local fi = io.open(filenameIn, 'rb') if fi == nil then return nil, "Cannot open " .. filenameIn end repeat partNb = partNb + 1 filenameOut = filenameIn .. "." .. string.format("%03d", partNb) part = fi:read(partSize) if part ~= nil then fo = io.open(filenameOut, 'wb') if fo == nil then return nil, "Cannot open '" .. filenameOut .. "'" end print("Writing '" .. filenameOut .. "' (" .. string.len(part) .. " bytes)") fo:write(part) fo:close() end until part == nil fi:close() return true end function DoJoint(filenameOut) print("Joining files to '" .. filenameOut .. "'...") local filenameIn, fi local fo = io.open(filenameOut, 'wb') if fo == nil then return nil, "Cannot open " .. filenameOut end repeat partNb = partNb + 1 filenameIn = filenameOut .. "." .. string.format("%03d", partNb) fi = io.open(filenameIn, 'rb') if fi == nil then fo:close() if partNb == 1 then -- Not even a first file? return nil, "Cannot find '" .. filenameIn .. "'" end return true -- No more files end -- Read whole file part = fi:read('*a') print("Read: '" .. filenameIn .. "' (" .. string.len(part) .. " bytes)") fi:close() if part ~= nil and part ~= "" then fo:write(part) end until part == nil or part == "" fo:close() return true end function FindAction(filename) if filename == nil then return nil, "I need a file name!" end local toSplit = GetFileSize(filename) local toJoint = GetFileSize(filename .. ".001") if toSplit == nil and toJoint == nil then return nil, "Cannot find '" .. filename .. "' nor '" .. filename .. ".001' in current path." end if toSplit ~= nil and toJoint ~= nil then return nil, "Both '" .. filename .. "' and '" .. filename .. ".001' exist in current path, I don't know what to do!" end if toSplit ~= nil then if toSplit <= partSize then return nil, "'" .. filename .. "' is smaller (" .. toSplit .." bytes) than the split size (" .. partSize .." bytes), nothing to do!" else return "split" end else toJoint = GetFileSize(filename .. ".002") if toJoint == nil then return nil, "'" .. filename .. ".002' not found in current path, I need at least two files to joint!" else return "joint" end end end action, err = FindAction(filename) if action == "split" then result, err = DoSplit(filename) elseif action == "joint" then result, err = DoJoint(filename) else -- Error in FindAction result = nil end if not result then print("Error: " .. (err or '?')) else print("Done.") end