20
20
21
21
-- imports
22
22
import (" core.base.option" )
23
+ import (" core.base.bytes" )
23
24
import (" core.compress.lz4" )
24
25
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
+
25
58
-- decompress file
26
59
function _decompress_file (archivefile , outputfile , opt )
27
60
lz4 .decompress_file (archivefile , outputfile )
@@ -37,6 +70,7 @@ function main(archivefile, outputdir, opt)
37
70
opt = opt or {}
38
71
39
72
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 )
41
75
os .tryrm (archivefile_tmp )
42
76
end
0 commit comments