Advertisement
Alakazard12

gunzip

Mar 25th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.69 KB | None | 0 0
  1. --[[
  2.  bin.gunzip
  3.  gunzip command partially reimplemented in Lua.
  4.  
  5.  Note: this does not implement all of the GNU
  6.  gunzip[1] command-line options and might have
  7.  slightly different behavior.
  8.  
  9.  This is designed to be called from a shell script:
  10.  
  11.    #!/bin/env lua
  12.    package.path = '?.lua;' .. package.path
  13.    require 'dmlib.command_gunzip' (...)
  14.  
  15.  References
  16.  
  17.    [1] http://www.gnu.org/software/gzip/
  18.  
  19. (c) 2008-2011 David Manura.  Licensed under the same terms as Lua (MIT).
  20. --]]
  21.  
  22. local assert = assert
  23. local error = error
  24. local ipairs = ipairs
  25. local require = require
  26. local xpcall = xpcall
  27. local type = type
  28. local io = io
  29. local os = os
  30. local string = string
  31. local debug = require "debug"
  32. local debug_traceback = debug.traceback
  33. local _G = _G
  34.  
  35. local DEFLATE = require "compress.deflatelua"
  36.  
  37. local OptionParser = require "pythonic.optparse" . OptionParser
  38.  
  39. local version = '0.1'
  40.  
  41.  
  42. local function runtime_assert(val, msg)
  43.   if not val then error({msg}, val) end
  44.   return val
  45. end
  46.  
  47.  
  48. local function runtime_error(s, level)
  49.   level = level or 1
  50.   error({s}, level+1)
  51. end
  52.  
  53.  
  54. local function file_exists(filename)
  55.   local fh = io.open(filename)
  56.   if fh then fh:close(); return true end
  57.   return false
  58. end
  59.  
  60.  
  61. -- Run gunzip command, given command-line arguments.
  62. local function call(...)
  63.   local opt = OptionParser{usage="%prog [options] [gzip-file...]",
  64.                            version=string.format("gunzip %s", version),
  65.                            add_help_option=false}
  66.   opt.add_option{"-h", "--help", action="store_true", dest="help",
  67.                  help="give this help"}
  68.   opt.add_option{
  69.     "-c", "--stdout", dest="stdout", action="store_true",
  70.     help="write on standard output, keep original files unchanged"}
  71.   opt.add_option{
  72.     "-f", "--force", dest="force", action="store_true",
  73.     help="force overwrite of output file"}
  74.   opt.add_option{
  75.     "--disable-crc", dest="disable_crc", action="store_true",
  76.     help="skip CRC check (faster performance)"}
  77.  
  78.  
  79.   local options, args = opt.parse_args()
  80.  
  81.   local gzipfiles = args
  82.  
  83.   if options.help then
  84.     opt.print_help()
  85.     os.exit()
  86.   end
  87.  
  88.   local ok, err = xpcall(function()
  89.     local outfile_of = {}
  90.     local out_of = {}
  91.  
  92.     for _,gzipfile in ipairs(gzipfiles) do
  93.       local base = gzipfile:match('(.+)%.[gG][zZ]$')
  94.       if not base then
  95.         runtime_error(gzipfile .. ': unknown suffix')
  96.       end
  97.       outfile_of[gzipfile] = base
  98.  
  99.       out_of[gzipfile] =
  100.         (options.stdout or not gzipfile) and assert(io.stdout)
  101.         or outfile_of[gzipfile]
  102.  
  103.       if type(out_of[gzipfile]) == 'string' then
  104.         if file_exists(out_of[gzipfile]) then
  105.           io.stderr:write(out_of[gzipfile] ..
  106.             ' already exists; do you wish to overwrite(y or n)? ')
  107.           if not io.stdin:read'*l':match'^[yY]' then
  108.             runtime_error 'not overwritten'
  109.           end
  110.         end
  111.       end
  112.     end
  113.  
  114.     for _,gzipfile in ipairs(gzipfiles) do
  115.       local fh = gzipfile and runtime_assert(io.open(gzipfile, 'rb'))
  116.                  or assert(io.stdin)
  117.       local ofh = type(out_of[gzipfile]) == 'string' and
  118.         runtime_assert(io.open(out_of[gzipfile], 'wb'))
  119.         or out_of[gzipfile]
  120.  
  121.       DEFLATE.gunzip {input=fh, output=ofh,
  122.         disable_crc=options.disable_crc}
  123.     end
  124.  
  125.     if not options.stdout then
  126.       for _,gzipfile in ipairs(gzipfiles) do
  127.         assert(os.remove(gzipfile))
  128.       end
  129.     end
  130.  
  131.   end, debug_traceback)
  132.   if not ok then
  133.     if type(err) == 'table' then err = err[1] end
  134.     io.stderr:write('error: ' .. err, '\n')
  135.   end
  136. end
  137.  
  138.  
  139. return call
  140.  
  141. --[[
  142. LICENSE
  143.  
  144. Copyright (C) 2008, David Manura.
  145.  
  146. Permission is hereby granted, free of charge, to any person obtaining a copy
  147. of this software and associated documentation files (the "Software"), to deal
  148. in the Software without restriction, including without limitation the rights
  149. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  150. copies of the Software, and to permit persons to whom the Software is
  151. furnished to do so, subject to the following conditions:
  152.  
  153. The above copyright notice and this permission notice shall be included in
  154. all copies or substantial portions of the Software.
  155.  
  156. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  157. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  158. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  159. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  160. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  161. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  162. THE SOFTWARE.
  163.  
  164. (end license)
  165. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement