mtx-unzip.lua /size: 3969 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['mtx-unzip'] = {
2    version   = 1.001,
3    comment   = "companion to mtxrun.lua",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- maybe --pattern
10
11local format, find = string.format, string.find
12
13local helpinfo = [[
14<?xml version="1.0"?>
15<application>
16 <metadata>
17  <entry name="name">mtx-unzip</entry>
18  <entry name="detail">Simple Unzipper</entry>
19  <entry name="version">0.10</entry>
20 </metadata>
21 <flags>
22  <category name="basic">
23   <subcategory>
24    <flag name="list"><short>list files in archive</short></flag>
25    <flag name="extract"><short>extract files [--silent --steps]</short></flag>
26   </subcategory>
27  </category>
28 </flags>
29</application>
30]]
31
32local application = logs.application {
33    name     = "mtx-unzip",
34    banner   = "Simple Unzipper 0.10",
35    helpinfo = helpinfo,
36}
37
38local report = application.report
39
40scripts          = scripts          or { }
41scripts.unzipper = scripts.unzipper or { }
42
43local function validfile()
44    local filename = environment.files[1]
45    if filename and filename ~= "" then
46        filename = file.addsuffix(filename,'zip')
47        if lfs.isfile(filename) then
48            return filename
49        else
50            report("invalid zip file: %s",filename)
51        end
52    else
53        report("no zip file")
54    end
55    return false
56end
57
58function scripts.unzipper.list()
59    local filename = validfile()
60    if filename then
61        local zipfile = utilities.zipfiles.open(filename)
62        if zipfile then
63            local list = utilities.zipfiles.list(zipfile)
64            if list then
65                local n = 0
66                for i=1,#list do
67                    local l = list[i]
68                    if #l.filename > n then
69                        n = #l.filename
70                    end
71                end
72                local files, paths, compressed, uncompressed = 0, 0, 0, 0
73                local template_a =   "%-" .. n .."s"
74                local template_b =   "%-" .. n .."s  % 9i  % 9i"
75                local template_c = "\n%-" .. n .."s  % 9i  % 9i"
76                for i=1,#list do
77                    local l = list[i]
78                    local f = l.filename
79                    if find(f,"/$") then
80                        paths = paths + 1
81                        print(format(template_a, f))
82                    else
83                        files = files + 1
84                        local cs = l.compressed
85                        local us = l.uncompressed
86                        if cs > compressed then
87                            compressed = cs
88                        end
89                        if us > uncompressed then
90                            uncompressed = us
91                        end
92                        print(format(template_b,f,cs,us))
93                    end
94                end -- check following pattern, n is not enough
95                print(format(template_c,files .. " files, " .. paths .. " directories",compressed,uncompressed))
96            end
97            utilities.zipfiles.close(zipfile)
98        else
99            report("invalid zip file: %s",filename)
100        end
101    end
102end
103
104function scripts.unzipper.extract()
105    local filename = validfile()
106    if validfile then
107        -- todo --junk
108        local silent = environment.arguments["silent"]
109        local steps  = environment.arguments["steps"]
110        utilities.zipfiles.unzipdir {
111            zipname = filename,
112            path    = ".",
113            verbose = not silent and (steps and "steps" or true),
114        }
115    end
116end
117
118if environment.arguments["list"] then
119    scripts.unzipper.list()
120elseif environment.arguments["extract"] then
121    scripts.unzipper.extract()
122elseif environment.arguments["exporthelp"] then
123    application.export(environment.arguments["exporthelp"],environment.files[1])
124else
125    application.help()
126end
127