Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

permitting deflate level to be set #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/ZipFile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -596,16 +596,20 @@ end
"""
addfile(w::Writer, name::AbstractString; method::Integer=Store, mtime::Float64=-1.0)

Add a new file named name into the ZIP file writer w, and return the
Add a new file named `name` into the ZIP file writer `w`, and return the
WritableFile for the new file. We don't allow concurrrent writes,
thus the file previously added using this function will be closed.

Method specifies the compression method that will be used (Store for
Method specifies the compression `method` that will be used (Store for
uncompressed or Deflate for compressed).

Mtime is the modification time of the file.
Method provides a `deflate_level` option which if provided implies a
`Deflate` compression method. When `Deflate` is specified as the `method`,
the default `deflate_level` is 6.

`Mtime` is the modification time of the file.
"""
function addfile(w::Writer, name::AbstractString; method::Integer=Store, mtime::Float64=-1.0)
function addfile(w::Writer, name::AbstractString; method::Integer=Store, mtime::Float64=-1.0, deflate_level=nothing)
if w._current !== nothing
close(w._current)
w._current = nothing
Expand Down Expand Up @@ -635,8 +639,8 @@ function addfile(w::Writer, name::AbstractString; method::Integer=Store, mtime::
_writele(w._io, b)

f._datapos = position(w._io)
if f.method == Deflate
f._zio = Zlib.Writer(f._io, true)
if f.method == Deflate || !isnothing(deflate_level)
f._zio = Zlib.Writer(f._io, something(deflate_level, 6), true)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the f.method is being changed to Deflate here, this must also be recorded when writing the local and central directory record. See lines 630, and 424. Otherwise reading will fail.

Also, if the compression level is changed, the general purpose bit flag should also be changed.

See: https://github.com/JuliaIO/ZipArchives.jl/blob/9e8c54f19577ca2db5c8f89e80e0fb657b9f63fd/src/constants.jl#L7-L21

And https://github.com/madler/zipflow/blob/2bef2123ebe519c17b18d2d0c3c71065088de952/zipflow.c#L214

end
w.files = [w.files; f]
w._current = f
Expand Down