Skip to content

Commit 7b05f6e

Browse files
committed
impl extract files
1 parent 8c55e95 commit 7b05f6e

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

xmake/modules/utils/archive/archive_xmz.lua

+4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020

2121
-- imports
2222
import("core.base.option")
23+
import("core.base.bytes")
2324
import("core.compress.lz4")
2425

2526
-- archive files
2627
function _archive_files(archivefile, inputfiles, opt)
2728
local outputfile = io.open(archivefile, "wb")
2829
for _, inputfile in ipairs(inputfiles) do
30+
outputfile:write(bytes(2):u16be_set(1, #inputfile))
2931
outputfile:write(inputfile)
3032
local data = io.readfile(inputfile, {encoding = "binary"})
33+
vprint("archiving %s, %d bytes", inputfile, data and #data or 0)
34+
outputfile:write(bytes(4):u32be_set(1, #data))
3135
outputfile:write(data)
3236
end
3337
outputfile:close()

xmake/modules/utils/archive/extract_xmz.lua

+35-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,41 @@
2020

2121
-- imports
2222
import("core.base.option")
23+
import("core.base.bytes")
2324
import("core.compress.lz4")
2425

26+
-- extract files
27+
function _extract_files(archivefile, outputdir, opt)
28+
local inputfile = io.open(archivefile, "rb")
29+
local filesize = inputfile:size()
30+
local readsize = 0
31+
while readsize < filesize do
32+
local data = inputfile:read(2)
33+
local size = bytes(data):u16be(1)
34+
local filepath
35+
if size > 0 then
36+
filepath = inputfile:read(size)
37+
end
38+
readsize = readsize + 2 + size
39+
data = inputfile:read(4)
40+
size = bytes(data):u32be(1)
41+
local filedata
42+
if size > 0 then
43+
filedata = inputfile:read(size)
44+
end
45+
readsize = readsize + 4 + size
46+
if filepath then
47+
vprint("extracting %s, %d bytes", filepath, filedata and #filedata or 0)
48+
if filedata then
49+
io.writefile(path.join(outputdir, filepath), filedata, {encoding = "binary"})
50+
else
51+
os.touch(filepath)
52+
end
53+
end
54+
end
55+
inputfile:close()
56+
end
57+
2558
-- decompress file
2659
function _decompress_file(archivefile, outputfile, opt)
2760
lz4.decompress_file(archivefile, outputfile)
@@ -37,6 +70,7 @@ function main(archivefile, outputdir, opt)
3770
opt = opt or {}
3871

3972
local archivefile_tmp = os.tmpfile({ramdisk = false})
40-
_decompress_file(archivefile, archivefile_tmp)
73+
_decompress_file(archivefile, archivefile_tmp, opt)
74+
_extract_files(archivefile_tmp, outputdir, opt)
4175
os.tryrm(archivefile_tmp)
4276
end

xmake/repository/xmake.lua

-3
This file was deleted.

0 commit comments

Comments
 (0)