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

switch to ZipArchives #31

Merged
merged 3 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
StaticStrings = "4db0a0c5-418a-4e1d-8806-cb305fe13294"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"
ZipArchives = "49080126-0e18-4c2a-b176-c102e4b3760c"

[compat]
AbstractTrees = "0.4"
Expand All @@ -30,5 +30,5 @@ StaticArraysCore = "1"
StaticStrings = "0.2"
StructArrays = "0.6"
TranscodingStreams = "0.9"
ZipFile = "0.10"
ZipArchives = "0.3"
julia = "1.8"
12 changes: 5 additions & 7 deletions src/readers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Doesn't support deleting, or changing data already written.

using ArgCheck
using ZipFile
using ZipArchives


abstract type AbstractReader end
Expand Down Expand Up @@ -47,19 +47,17 @@ end


struct BufferedZipReader <: AbstractReader
zipfile::ZipFile.Reader
zipfile::ZipBufferReader{Vector{UInt8}}
function BufferedZipReader(path)
@argcheck isfile(path)
io = IOBuffer(read(path, String))
zipfile = ZipFile.Reader(io)
new(zipfile)
new(ZipBufferReader(read(path)))
end
end

function key_names(d::BufferedZipReader)::Vector{String}
return map(f->f.name, d.zipfile.files)
return zip_names(d.zipfile)
end

function read_key_idx(d::BufferedZipReader, idx::Int)::Vector{UInt8}
read(d.zipfile.files[idx])
zip_readentry(d.zipfile, idx)
end
8 changes: 4 additions & 4 deletions src/saving.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ function _save_attrs(writer::AbstractWriter, key_prefix::String, z::Union{ZArray
if isempty(attrs(z))
return
end
write_key(writer, key_prefix*".zattrs", sprint(io->JSON3.pretty(io,attrs(z); allow_inf=true)))
write_key(writer, key_prefix*".zattrs", codeunits(sprint(io->JSON3.pretty(io,attrs(z); allow_inf=true))))
return
end

function _save_zgroup(writer::AbstractWriter, key_prefix::String, z::ZGroup)
group_key = key_prefix*".zgroup"
write_key(writer, group_key, "{\"zarr_format\":2}")
write_key(writer, group_key, codeunits("{\"zarr_format\":2}"))
_save_attrs(writer, key_prefix, z)
for (k,v) in pairs(children(z))
@argcheck !isempty(k)
Expand Down Expand Up @@ -93,7 +93,7 @@ function _save_zarray(writer::AbstractWriter, key_prefix::String, z::ZArray)
end
# store array meta data
write_key(writer, key_prefix*".zarray",
"""
codeunits("""
{
"chunks": [$(join(z.chunks, ", "))],
"compressor": $(JSON3.write(norm_compressor; allow_inf=true)),
Expand All @@ -104,6 +104,6 @@ function _save_zarray(writer::AbstractWriter, key_prefix::String, z::ZArray)
"shape": [$(join(shape, ", "))],
"zarr_format": 2
}
"""
""")
)
end
10 changes: 4 additions & 6 deletions src/writers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Doesn't support deleting, or changing data already written.

using ArgCheck
using ZipFile
using ZipArchives


abstract type AbstractWriter end
Expand Down Expand Up @@ -41,29 +41,27 @@ Write to an in memory zipfile, that gets saved to disk on close.
This writer will overwrite any existing file at `path`
"""
struct BufferedZipWriter <: AbstractWriter
zipfile::ZipFile.Writer
zipfile::ZipWriter{IOBuffer}
path::String
iobuffer::IOBuffer
function BufferedZipWriter(path)
@argcheck !isdir(path)
@argcheck !isdirpath(path)
mkpath(dirname(path))
iobuffer = IOBuffer()
zipfile = ZipFile.Writer(iobuffer)
zipfile = ZipWriter(iobuffer)
new(zipfile, abspath(path), iobuffer)
end
end

function write_key(d::BufferedZipWriter, key::AbstractString, data)::Nothing
f = ZipFile.addfile(d.zipfile, key);
write(f, data)
zip_writefile(d.zipfile, key, data)
nothing
end

function closewriter(d::BufferedZipWriter)
close(d.zipfile)
open(d.path, "w") do f
write(f, take!(d.iobuffer))
close(d.iobuffer)
end
end