SyntaxHighlighter

---SyntaxHighlighter ウィジェット---

2015年12月16日水曜日

FlashAirのLuaスクリプトでtarアーカイブを展開する

--展開にはめっちゃ時間かかります(1ファイル最低5秒以上)
--Tiny Tar Extracter.lua
--超簡易版です。ファイル名と、ファイル種別(フォルダorファイル)、サイズ、チェックサムしか見てません。
--(しかもチェックサムおかしくても展開続けます)
--フォルダ作成処理のところを入れ替えればWindows上でも動きます。

MastarPath="/";

f = io.open ("/FlashAir_Tiny_Lua_Editer.tar","rb");
if(f==nil)then
print "Err";
return;
end
print "OPEN";
next_file_head = 0;

while true do
collectgarbage();
now_file_head = next_file_head;
--FileName
f:seek("set",now_file_head);
fname = f:read(100);
--tar末端検出
fname = string.match (fname,"[^\0]+");--null文字削除
if(fname == nil)then break; end;
print("name:"..fname);

--FileSize
f:seek("set",now_file_head+0x7C);
size = f:read(12);--Tar形式は文字列
size = string.match (size,"[^\0]+");--null文字削除
size = tonumber(size,8);--(ただし8進数!)
print("size:"..size);
block = tonumber(string.format ("%d",((size+511)/512))); --ビット演算ダメ
print("blocks:"..block);
next_file_head = now_file_head + ((1+block)*512); -- HEAD+BODY
print("next_file_head:"..next_file_head);

--chksum
f:seek("set",now_file_head+0x94);
chksum = f:read(8);
chksum = string.match (chksum,"[^\0]+");--null文字削除
chksum = tonumber(chksum,8); --これも8進数
print("chksum:"..chksum);

sum=0;
for pos=0,511 do
f:seek("set",now_file_head+pos);
if((pos >= 0x94) and (pos < (0x94+8)))then
sum = sum+0x20;
else
sum = sum+string.byte(f:read(1));
end
end
print("sum:"..sum);

if(sum == chksum)then
print("chksum OK");
else
print("chksum NG");
end

--typeflag
f:seek("set",now_file_head+0x9C);
typeflag = f:read(1);
if(typeflag == "0")then
type = "File";
elseif(typeflag == "5")then
type = "Directory";
else
type = "Unsupported";
end

print("typeflag:"..typeflag.."/"..type);

--出力
if(type == "Directory")then
--windows用
--fname = string.gsub (fname, "/","\\")
--os.execute("mkdir "..fname)
print(string.sub("mkdir:"..MastarPath..fname,1,-2));
    if(lfs.mkdir(string.sub(MastarPath..fname,1,-2)) == true)then --末端/は削る
      print("mkdir OK");
      else
      print("mkdir fail");
      end
end
if(type == "File")then
fo = io.open (MastarPath..fname,"wb");
if(fo==nil)then
print "Output Err";
return;
end

collectgarbage();--メモリのお掃除


    --debug限界:8192+4096+1024+384 = 13696
    --release限界:8192+4096+1024+540 = 13852
Trans_Block =8192; --転送ブロックサイズ。メモリの余裕によって変更。
    pos_save=0;
    if((size-1) > Trans_Block)then --サイズが小さいならループしない
for pos=0,(size-1-Trans_Block),Trans_Block do
collectgarbage();--メモリのお掃除

      --メモリの関係もあるので64バイトづつ書き込む
f:seek("set",now_file_head+512+pos);
fo:write(f:read(Trans_Block));

--if((pos % 10240) == 0)then
io.write("\rputting..."..pos.."/"..size.."    ");
--end
pos_save=pos;
        end
pos_save=pos_save+Trans_Block;--最後の転送分を加算
end
print(size-pos_save)
f:seek("set",now_file_head+512+pos_save);
fo:write(f:read(size-pos_save));

fo:close();
end

print("\n---");
end


f:close()
print "CLOSED";

Copyright (c) 2015, GPS_NMEAAll rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

0 件のコメント:

コメントを投稿