Advertisement
Guest User

FLCF

a guest
Jan 25th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. --[[
  2.     FLCF - Fast Lightweight Compressed Files
  3.     An creation of Redall.
  4.    
  5.     Under MIT License:
  6.     Copyright (c) 2017 Alexis B.
  7.  
  8.     Permission is hereby granted, free of charge, to any person obtaining a copy
  9.     of this software and associated documentation files (the "Software"), to deal
  10.     in the Software without restriction, including without limitation the rights
  11.     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12.     copies of the Software, and to permit persons to whom the Software is
  13.     furnished to do so, subject to the following conditions:
  14.  
  15.     The above copyright notice and this permission notice shall be included in all
  16.     copies or substantial portions of the Software.
  17.  
  18.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19.     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20.     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21.     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22.     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23.     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24.     SOFTWARE.
  25. ]]--
  26.  
  27. local object = {};
  28.  
  29. function createArchive(path)
  30.     if (fs.exists(path)) then
  31.         error("Archive already exists.");
  32.         return;
  33.     elseif (fs.isDir(path)) then
  34.         error("Given path is a directory.");
  35.         return;
  36.     end
  37.    
  38.     local structure = {
  39.         type = "flcf",
  40.         content = {},
  41.     };
  42.    
  43.     local archive = fs.open(path, "w");
  44.     archive.write(textutils.serialise(structure));
  45.     archive.close();
  46. end
  47.  
  48. function loadArchive(path)
  49.     if (fs.exists(path) == false) then
  50.         error("Archive does not exists.");
  51.         return;
  52.     elseif (fs.isDir(path)) then
  53.         error("Given path is a directory.");
  54.         return;
  55.     end
  56.    
  57.     local archive = fs.open(path, "r");
  58.     local files = textutils.unserialise(archive.readAll());
  59.     archive.close();
  60.    
  61.     local usableArchive = object;
  62.     usableArchive.archive = files;
  63.     usableArchive.path = path;
  64.    
  65.     return usableArchive;
  66. end
  67.  
  68. function object.addFile(source, dest)
  69.     if (fs.exists(source) == false) then
  70.         error("Source file does not exists.");
  71.         return;
  72.     elseif (fs.isDir(source)) then
  73.         error("Expected file, folder given.");
  74.         return;
  75.     end
  76.    
  77.     local file = fs.open(source, "r");
  78.     local content = file.readAll();
  79.     file.close();
  80.    
  81.     local entry = {
  82.         path = dest,
  83.         content = content,
  84.     }
  85.    
  86.     table.insert(object.archive.content, entry);
  87. end
  88.  
  89. function object.removeFile(path)
  90.     for id, entry in ipairs(object.archive.content) do
  91.         if (entry.path == path) then
  92.             table.remove(object.archive.content, id);
  93.             return;
  94.         end
  95.     end
  96. end
  97.  
  98. function object.extractFile(source, dest)
  99.     for _, entry in ipairs(object.archive.content) do
  100.         if (entry.path == path) then
  101.             local f = fs.open(dest, "w");
  102.             f.write(entry.content);
  103.             f.close();
  104.            
  105.             return;
  106.         end
  107.     end
  108. end
  109.  
  110. function object.saveArchive()
  111.     local archive = fs.open(object.path, "w");
  112.     archive.write(textutils.serialise(object.archive));
  113.     archive.close();
  114. end
  115.  
  116. function object.getFiles()
  117.     return object.archive.content;
  118. end
  119.  
  120. function object.getType()
  121.     return object.archive.type;
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement