diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index 7d34393d..ecea4c23 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -212,12 +212,7 @@ static int l_file_gzip_compress(lua_State* L) { if (fs::is_regular_file(path)) { size_t length = static_cast(fs::file_size(path)); - auto bytesPtr = files::read_bytes(path, length); - std::vector bytes (bytesPtr.get(), bytesPtr.get() + length); - - std::vector compressed_bytes; - - compressed_bytes = gzip::compress(bytes.data(), bytes.size()); + auto compressed_bytes = gzip::compress(files::read_bytes(path, length).get(), length); lua_pushboolean(L, files::write_bytes(path, compressed_bytes.data(), compressed_bytes.size())); return 1; @@ -225,6 +220,19 @@ static int l_file_gzip_compress(lua_State* L) { throw std::runtime_error("file does not exist " + util::quote(path.u8string())); } +static int l_file_gzip_decompress(lua_State* L) { + fs::path path = resolve_path(state->requireString(1)); + if (fs::is_regular_file(path)) { + size_t length = static_cast(fs::file_size(path)); + + auto decompressed_bytes = gzip::decompress(files::read_bytes(path, length).get(), length); + + lua_pushboolean(L, files::write_bytes(path, decompressed_bytes.data(), decompressed_bytes.size())); + return 1; + } + throw std::runtime_error("file does not exist " + util::quote(path.u8string())); +} + const luaL_Reg filelib [] = { {"exists", lua_wrap_errors}, {"find", lua_wrap_errors}, @@ -241,6 +249,7 @@ const luaL_Reg filelib [] = { {"resolve", lua_wrap_errors}, {"write_bytes", lua_wrap_errors}, {"write", lua_wrap_errors}, - {"gzip_compress_write", lua_wrap_errors}, + {"gzip_compress", lua_wrap_errors}, + {"gzip_decompress", lua_wrap_errors}, {NULL, NULL} };