Skip to content

Commit

Permalink
support disabling of compression
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Jan 11, 2024
1 parent 9f9bb1b commit 9802bcb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ local z = mtzip.zip(f)
-- add a sample file with dummy content
z:add("test.txt", "test123")

-- add a file and disable compression
z:add("test.txt", "mycontent", { disable_compression = true })

-- close and finalize the zip file
z:close()
-- close the file (flush pending changes)
Expand Down
10 changes: 6 additions & 4 deletions zip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ function mtzip.zip(file)
return setmetatable(self, ZippedFile_mt)
end

local function write_file(file, filename, data)
local function write_file(file, filename, data, opts)
opts = opts or {}

local offset = file:seek("end")
local compressed = false
local date, time = mtzip.toDosTime(os.date("*t"))
local crc = mtzip.crc32(data)

local uncompressed_size = #data
if uncompressed_size > 10 then
if not opts.disable_compression then
compressed = true
data = minetest.compress(data, "deflate", 9)
-- strip zlib header
Expand Down Expand Up @@ -102,8 +104,8 @@ local function write_eocd(file, count, offset, cd_size)
file:write(char(0x00, 0x00)) -- Comment length (n)
end

function ZippedFile:add(filename, data)
self.headers[filename] = write_file(self.file, filename, data)
function ZippedFile:add(filename, data, opts)
self.headers[filename] = write_file(self.file, filename, data, opts)
end

function ZippedFile:close()
Expand Down

0 comments on commit 9802bcb

Please sign in to comment.