summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-05-08 23:18:34 +0800
committerruki <[email protected]>2022-05-08 23:18:34 +0800
commit3b1e2a5299a4c0e5f88798acf51fe74aed840081 (patch)
tree90821ac3a249fc2aaf1910bbf2fc244d7941f6ea
parentbd81aa4cd750f668f325ca1269236981f5cdd4c5 (diff)
compress file
-rw-r--r--xmake/core/base/io.lua8
-rw-r--r--xmake/core/compress/lz4.lua24
-rw-r--r--xmake/core/sandbox/modules/import/core/compress/lz4.lua16
3 files changed, 46 insertions, 2 deletions
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index 16b0efc46..128790c98 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -494,7 +494,7 @@ function io.readfile(filepath, opt)
opt = opt or {}
-- open file
- local file, errors = io.open(filepath, "r", opt)
+ local file, errors = io.open(tostring(filepath), "r", opt)
if not file then
return nil, errors
end
@@ -540,7 +540,7 @@ function io.writefile(filepath, data, opt)
opt = opt or {}
-- open file
- local file, errors = io.open(filepath, "w", opt)
+ local file, errors = io.open(tostring(filepath), "w", opt)
if not file then
return false, errors
end
@@ -595,6 +595,7 @@ function io.open(filepath, mode, opt)
mode = mode or "r"
-- open it
+ filepath = tostring(filepath)
local file = io.file_open(filepath, mode .. (opt.encoding or ""))
if file then
return _file.new(filepath, file)
@@ -610,6 +611,7 @@ function io.openlock(filepath)
assert(filepath)
-- open it
+ filepath = tostring(filepath)
local lock = io.filelock_open(filepath)
if lock then
return _filelock.new(filepath, lock)
@@ -628,6 +630,7 @@ function io.save(filepath, object, opt)
assert(filepath and object)
opt = opt or {}
+ filepath = tostring(filepath)
local file, err = io.open(filepath, "wb", opt)
if err then
return false, err
@@ -646,6 +649,7 @@ function io.load(filepath, opt)
assert(filepath)
opt = opt or {}
+ filepath = tostring(filepath)
local file, err = io.open(filepath, "rb", opt)
if err then
return nil, err
diff --git a/xmake/core/compress/lz4.lua b/xmake/core/compress/lz4.lua
index a153d434a..2f7f4ed1d 100644
--- a/xmake/core/compress/lz4.lua
+++ b/xmake/core/compress/lz4.lua
@@ -69,6 +69,30 @@ function lz4.decompress(data, opt)
return bytes(result)
end
+-- compress file data
+function lz4.compress_file(srcpath, dstpath, opt)
+ local data, errors = io.readfile(srcpath)
+ if data then
+ data, errors = lz4.compress(data, opt)
+ if data then
+ return io.writefile(dstpath, data)
+ end
+ end
+ return false, errors or "compress failed"
+end
+
+-- decompress file data
+function lz4.decompress_file(srcpath, dstpath, opt)
+ local data, errors = io.readfile(srcpath)
+ if data then
+ data, errors = lz4.decompress(data, opt)
+ if data then
+ return io.writefile(dstpath, data)
+ end
+ end
+ return false, errors or "decompress failed"
+end
+
-- compress block data
--
-- @param data the data
diff --git a/xmake/core/sandbox/modules/import/core/compress/lz4.lua b/xmake/core/sandbox/modules/import/core/compress/lz4.lua
index ec959aa71..5287cc47f 100644
--- a/xmake/core/sandbox/modules/import/core/compress/lz4.lua
+++ b/xmake/core/sandbox/modules/import/core/compress/lz4.lua
@@ -43,6 +43,22 @@ function sandbox_core_compress_lz4.decompress(data, opt)
return result
end
+-- compress file data
+function sandbox_core_compress_lz4.compress_file(srcpath, dstpath, opt)
+ local result, errors = lz4.compress_file(srcpath, dstpath, opt)
+ if not result and errors then
+ raise(errors)
+ end
+end
+
+-- decompress file data
+function sandbox_core_compress_lz4.decompress_file(srcpath, dstpath, opt)
+ local result, errors = lz4.decompress_file(srcpath, dstpath, opt)
+ if not result and errors then
+ raise(errors)
+ end
+end
+
-- compress block data
function sandbox_core_compress_lz4.block_compress(data, opt)
local result, errors = lz4.block_compress(data, opt)