summaryrefslogtreecommitdiff
path: root/xmake/modules/utils/archive/extract_xmz.lua
blob: 7f4ce58e5faf3d309fbc0aed69a10911d463a4bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        extract_xmz.lua
--

-- imports
import("core.base.option")
import("core.base.bytes")
import("core.compress.lz4")

-- extract files
function _extract_files(archivefile, outputdir, opt)
    local inputfile = io.open(archivefile, "rb")
    local filesize = inputfile:size()
    local readsize = 0
    while readsize < filesize do
        local data = inputfile:read(2)
        local size = bytes(data):u16be(1)
        local filepath
        if size > 0 then
            filepath = inputfile:read(size)
        end
        readsize = readsize + 2 + size
        data = inputfile:read(4)
        size = bytes(data):u32be(1)
        local filedata
        if size > 0 then
            filedata = inputfile:read(size)
        end
        readsize = readsize + 4 + size
        if filepath then
            vprint("extracting %s, %d bytes", filepath, filedata and #filedata or 0)
            if filedata then
                io.writefile(path.join(outputdir, filepath), filedata, {encoding = "binary"})
            else
                os.touch(filepath)
            end
        end
    end
    inputfile:close()
end

-- decompress file
function _decompress_file(archivefile, outputfile, opt)
    lz4.decompress_file(archivefile, outputfile)
end

-- extract file
--
-- @param archivefile   the archive file. e.g. *.tar.gz, *.zip, *.7z, *.tar.bz2, ..
-- @param outputdir     the output directory
-- @param options       the options, e.g.. {excludes = {"*/dir/*", "dir/*"}}
--
function main(archivefile, outputdir, opt)
    opt = opt or {}

    local archivefile_tmp = os.tmpfile({ramdisk = false})
    _decompress_file(archivefile, archivefile_tmp, opt)
    _extract_files(archivefile_tmp, outputdir, opt)
    os.tryrm(archivefile_tmp)
end