Andy73

AndyCode Archiver v.0.4.1

May 5th, 2014
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.09 KB | None | 0 0
  1. --[[
  2. Pure Lua archiver made by Andy73 a.k.a. viluon
  3.  
  4. Copyright 2014
  5. version 0.4.1 BETA (6th public release)
  6.  
  7. usage: archiver <compress|extract|help> <file input path> <file output path>
  8.  
  9. Dev notes:
  10. -that bugfix promised&needed
  11. -fixed % calc
  12. -gonna make the use of serialization for org
  13.  
  14. WIP
  15. ]]
  16.  
  17. local additionalMessage=nil
  18. local s="?"
  19. local tArgs={...}
  20. local t=nil
  21. local tOrigin={}
  22. local tDictionaryA={
  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. local tDictionaryB={
  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. "œ",
  77. "õ",
  78. "ů",
  79. "ú",
  80. "û",
  81. "ü",
  82. "ù",
  83. "ū",
  84. "ý",
  85. "ÿ",
  86.  
  87. "ř",
  88. "ť",
  89. "ź",
  90. "ž",
  91. "ż",
  92. "š",
  93. "ß",
  94. "ś",
  95. "ď",
  96. "ć",
  97. "č",
  98. "ç",
  99. "ń",
  100. "ň",
  101. "ñ",
  102. "`", }
  103.  
  104.  
  105. local function help()
  106. print("Pure Lua archiver by Andy a.k.a. viluon copyright 2014")
  107. print("version 0.4.1 BETA")
  108. print("")
  109. print("Current dictionary size: "..s.." keywords")
  110. print("") term.setBackgroundColor(colors.cyan)
  111. print(string.rep("-",term.getSize())) term.setTextColor(colors.black)
  112. print("FAQ: I am getting an error when accessing a file in ../ directory")
  113. print("  Yes, that is a common problem, I am working on a fix now. Please use the current directory until I fix this (some weird behaviour in shell.resolve :( )") term.setBackgroundColor(colors.black);term.setTextColor(colors.white)
  114. print("Please note:Beta versions can cause unexpected data changes. Andy is not responsible for any data corruption or removals caused by this program. Use at your own risk.")
  115. end
  116.  
  117. local function usage()
  118. print("Usage:")
  119. print(" archiver <compress|extract|help> <file input path> <file output path>")
  120. print("'compress','extract' or 'help' can be shortened to 'c','e' or '?'")
  121. end
  122.  
  123. local function loadOrg()
  124. local iorg=0
  125. local org=fs.open(shell.resolve(".").."/org","r")
  126. if org then
  127. line=org.readLine()
  128. while line do
  129. iorg=iorg+1
  130. table.insert(tOrigin,iorg,line)
  131. line=org.readLine()
  132. --print(iorg)
  133. end
  134. org:close()
  135. s=iorg
  136. else
  137. error("Origin file doesn't exist (\"./org\"). Download it using 'pastebin get xj4ZABRH org'.")
  138. end
  139. end
  140.  
  141. local function r(a,b)
  142. t=string.gsub(t,a,b)
  143. end
  144.  
  145. --COMPRESS
  146. local function compress(...)
  147. local args={...}--ARGS ARE SOLVED BEFORE!!!
  148. local file=fs.open(shell.resolve(tostring(args[1])),"r")
  149. print("FileSize original:"..fs.getSize(shell.resolve(tostring(args[1])))) --print("asd")
  150. if file then t=file.readAll() else error("Can't open:Requested source file doesn't exist.") end
  151. file:close()
  152. local b=0
  153. for a=1,#tDictionaryA do
  154.     for index=1,#tDictionaryB do
  155.      b=b+1
  156.      if b>#tOrigin then break end
  157.      
  158.      r(tostring(tOrigin[b]),tostring(tDictionaryA[a])..tostring(tDictionaryB[index]))
  159.     end
  160.    end
  161.  
  162. local target=fs.open(shell.resolve(tostring(args[2])),"w")
  163. if not target then error("Can't write:Taget path is invalid. Check the help for more info.") end
  164. target.write(t)
  165. target:close()
  166.  
  167. print("FileSize comprimed:"..fs.getSize(shell.resolve(args[2])))
  168. local one=tonumber(fs.getSize(shell.resolve(args[1])))/100
  169. local size=fs.getSize(shell.resolve(args[2]))
  170. local calc=tonumber(size/one)
  171.  
  172. --[[DEBUG
  173. print("fs.getSize(...):"..fs.getSize(shell.resolve(tostring(args[1]))))
  174. print("one:"..one)
  175. print("size:"..size)
  176. print("size/one:"..tonumber(size/one))
  177. print("calc:"..calc)
  178. --/DEBUG]]
  179. if calc>99 then calc=math.ceil(calc) additionalMessage="Was file already compressed?" end
  180. print("comprimed by "..(100-tonumber(calc)).."%")
  181. print(additionalMessage)
  182. end
  183.  
  184. --EXTRACT
  185. local function extract(...)
  186. local args={...}--ARGS ARE SOLVED BEFORE!!!
  187. local file=fs.open(shell.resolve(tostring(args[1])),"r") --print("asd")
  188. if file then t=file.readAll() else error("Can't open:Requested source file doesn't exist.") end
  189. file:close()
  190.  
  191. local b=0
  192.    for a=1,#tDictionaryA do
  193.     for index=1,#tDictionaryB do
  194.      b=b+1
  195.      if b>#tOrigin then break end
  196.      r(tostring(tDictionaryA[a])..tostring(tDictionaryB[index]),tostring(tOrigin[b]))
  197.     end
  198.    end
  199.  
  200. local target=fs.open(shell.resolve(tostring(args[2])),"w")
  201. if not target then error("Can't write:Taget path is invalid. Check the help for more info.") end
  202. target.write(t)
  203. target:close()
  204.  
  205. print("done!")
  206. end
  207.  
  208. local function main()
  209. if not fs.exists(shell.resolve(".").."/org") then
  210.   pcall(function() print("Attempting to download origin file...") shell.run("pastebin","get","xj4ZABRH","org") end)
  211. end
  212. loadOrg()
  213. if tostring(tArgs[1])=="compress" or tostring(tArgs[1])=="c" then
  214. compress(tArgs[2],tArgs[3])
  215. elseif tostring(tArgs[1])=="extract" or tostring(tArgs[1])=="e" then
  216. extract(tArgs[2],tArgs[3])
  217. elseif tostring(tArgs[1])=="help" or tostring(tArgs[1])=="?" then
  218. help()
  219. else
  220. usage()
  221. end
  222. end
  223. ------------------------------------------------------------------------------------------------------------
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230. main()
Advertisement
Add Comment
Please, Sign In to add comment