Skip to content

Commit

Permalink
Merge pull request #8 from nhz2/nz/clean-up-ZipFile-workaround
Browse files Browse the repository at this point in the history
Cleanup global variable workaround for ZipFile.jl bug and test `zip_dict`
  • Loading branch information
cortner authored Oct 12, 2024
2 parents fa71011 + 576fc5f commit c127fc0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"
ZipArchives = "49080126-0e18-4c2a-b176-c102e4b3760c"

[compat]
julia = "1.8"
JSON = "0.21"
Reexport = "1"
StaticArrays = "1"
YAML = "0.4"
ZipFile = "0.9, 0.10"
ZipArchives = "2"
julia = "1.8"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
26 changes: 11 additions & 15 deletions src/fio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ packages should use these interface functions so that the file formats
can be changed later. """
module FIO

using JSON, YAML, ZipFile
using JSON, YAML
using ZipArchives: ZipReader, ZipWriter, zip_newfile, zip_nentries, zip_name, zip_readentry
using SparseArrays: SparseMatrixCSC
using StaticArrays

Expand Down Expand Up @@ -122,25 +123,20 @@ end


function zip_dict(fname::AbstractString, D::Dict; indent=0)
zipdir = ZipFile.Writer(fname)
fptr = ZipFile.addfile(zipdir, "dict.json"; method=ZipFile.Deflate)
write(fptr, JSON.json(D, indent))
close(fptr)
close(zipdir)
ZipWriter(fname) do zipdir
zip_newfile(zipdir, "dict.json"; compress=true)
write(zipdir, JSON.json(D, indent))
end
return nothing
end

function unzip_dict(fname::AbstractString; verbose=false)
# using global here is a weird workaround for a bug as suggest in
# https://github.com/fhs/ZipFile.jl/issues/14
global _zipdir = ZipFile.Reader(fname)
if length(_zipdir.files) != 1
zipdir = ZipReader(read(fname))
if zip_nentries(zipdir) != 1
error("Expected exactly one file in `$(fname)`")
end
fptr = _zipdir.files[1]
verbose && @show fptr.name
return JSON.parse(fptr)
close(fptr)
close(_zipdir)
verbose && @show zip_name(zipdir, 1)
return JSON.parse(zip_readentry(zipdir, 1, String))
end

#######################################################################
Expand Down
10 changes: 10 additions & 0 deletions test/test_fio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ for O in objects
println_slim(@test all(test_fio(O)))
end

# Test zip_dict and unzip_dict work
for O in objects
tmpf = tempname()
D = write_dict(O)
save_json(tmpf * ".json", D)
Djson = load_json(tmpf * ".json")
zip_dict(tmpf * ".zip", D)
Dzip = unzip_dict(tmpf * ".zip")
println_slim(@test Dzip == Djson)
end

0 comments on commit c127fc0

Please sign in to comment.