Funciones para unir y dividir archivos *Actualizado
Publicado: 25 Feb 2011 21:35
Aca las funciones que puse en el otro post para evitar que se lean muchos temas buscando las últimas:
Ejemplos de como llamarlas:
function File.SplitBinary(filePath, folderPath, size, CallbackFunction)
local fileSize = File.GetSize(filePath);
local fileName = String.SplitPath(filePath).Filename..String.SplitPath(filePath).Extension..".";
local realpart = io.open(filePath, "rb");
local size = size*(2^20);
if fileSize < size then
return -1, "Split size can't be greater than filesize";
end
local r = fileSize%size;
local n = (r==0) and math.floor((fileSize/size)) or math.floor((fileSize/size))+1;
for x=1, n do
if CallbackFunction ~= nil then
CallbackFunction(x, n, math.floor((x/n)*100, 0));
end
local cfileName = string.format("%0"..#tostring(n).."d", x);
local curFile = io.open(folderPath.."\\"..fileName..cfileName, "wb");
local bytes = (x<n) and size or (x==n) and (r~=0) and r or size;
local curPart = realpart:read(bytes);
curFile:write(curPart);
curFile:close();
end
realpart:close();
return 0, "Success: File splitted in "..n.." parts";
end
function File.JoinBinary(FilePath, OutPath, call)
local counter = 1;
local buffer = 2^12;
if not File.DoesExist(FilePath) then
return -1, "File doesn't exist";
end
local tAt = String.SplitPath(FilePath);
local sF = (String.SplitPath(OutPath).Extension~= "") and OutPath or OutPath.."\\"..tAt.Filename;
local fileout, state_msg = io.open(sF, "ab");
if not fileout or state_msg then
return -1, "Couldn't open destination file";
end
local sD = tAt.Drive..tAt.Folder..tAt.Filename
local n = 1;
while true do
if not File.DoesExist(sD.."."..string.format("%0"..(#tAt.Extension-1).."d", n)) then
break;
else
n = n + 1;
end
end
n = n-1;
while true do
if call then
local a = call(counter, n);
if not a then return -1, "Process cancelled by user" end
end
local sFile = sD.."."..string.format("%0"..(#tAt.Extension-1).."d", counter);
if not File.DoesExist(sFile) then
break;
else
local filesize = File.GetSize(sFile);
local filesource, state_msg = io.open(sFile, "rb");
if not filesource or state_msg or filesize <= 1 then
fileout:close();
return -1, "Couldn't open source file: "..sFile;
else
while true do
local block = filesource:read(buffer);
if not block then
break;
end
fileout:write(block);
end
end
if filesource then
filesource:close();
end
counter = counter+1;
end
end
fileout:close();
return 0, "Success";
end
local code, msg = File.SplitBinary(_SourceFolder.."\\mp3.mp3", _SourceFolder.."\\test", 1, nil);
Dialog.Message("Result", "Return code: "..code..", Msg: "..msg);
local code, msg = File.JoinBinary(_SourceFolder.."\\test\\mp3.mp3.01", _SourceFolder.."\\prueba.mp3") ;
Dialog.Message("Result", "Return code: "..code..", Msg: "..msg);