From 576fc5f9cd134f9177d59c5d06b6f6a9f030d0f4 Mon Sep 17 00:00:00 2001 From: nhz2 Date: Fri, 11 Oct 2024 08:58:27 -0400 Subject: [PATCH] Cleanup weird workaround for ZipFile.jl bug and test zip_dict --- Project.toml | 6 +++--- src/fio.jl | 26 +++++++++++--------------- test/test_fio.jl | 10 ++++++++++ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Project.toml b/Project.toml index 63e8874..a683af2 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/fio.jl b/src/fio.jl index fc3241d..a3d32f5 100644 --- a/src/fio.jl +++ b/src/fio.jl @@ -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 @@ -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 ####################################################################### diff --git a/test/test_fio.jl b/test/test_fio.jl index ce28dd6..d39ec37 100644 --- a/test/test_fio.jl +++ b/test/test_fio.jl @@ -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