Skip to content

Commit

Permalink
add unzip, untargz
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelforget committed May 6, 2024
1 parent b0347a8 commit 6921421
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ version = "0.2.4"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Tar = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"

[weakdeps]
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
Expand Down
3 changes: 1 addition & 2 deletions src/Dataverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ include("pyDataverse.jl")
export pyDataverse

include("downloads.jl")
import Dataverse.downloads: file_download

import Dataverse.downloads: file_download, unzip, untargz

end
42 changes: 40 additions & 2 deletions src/downloads.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@

module downloads

import Dataverse.restDataverse: file_list
using Downloads, DataFrames
using Tar, CodecZlib, ZipFile

##

OCCA_file_list()=file_list(:OCCA_clim)
ECCO_file_list()=file_list(:ECCO_clim)

##

Expand Down Expand Up @@ -48,7 +55,38 @@ end

##

OCCA_file_list()=file_list(:OCCA_clim)
ECCO_file_list()=file_list(:ECCO_clim)
"""
untargz(fil)
Decompress and extract data from a `.tar.gz` file.
"""
function untargz(fil)
open(fil) do io
Tar.extract(CodecZlib.GzipDecompressorStream(io))
end
end

"""
function unzip(file,exdir="")
Source : @sylvaticus, https://discourse.julialang.org/t/
how-to-extract-a-file-in-a-zip-archive-without-using-os-specific-tools/34585/5
"""
function unzip(file,exdir="")
fileFullPath = isabspath(file) ? file : joinpath(pwd(),file)
basePath = dirname(fileFullPath)
outPath = (exdir == "" ? basePath : (isabspath(exdir) ? exdir : joinpath(pwd(),exdir)))
isdir(outPath) ? "" : mkdir(outPath)
zarchive = ZipFile.Reader(fileFullPath)
for f in zarchive.files
fullFilePath = joinpath(outPath,f.name)
if (endswith(f.name,"/") || endswith(f.name,"\\"))
mkdir(fullFilePath)
else
write(fullFilePath, read(f))
end
end
close(zarchive)
end

end
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ Dataverse.pyDataverse_APIs()
df1,df2=pyDataverse.demo("metadata")
@test size(df1,1)==56
@test size(df2,1)==11

url="https://zenodo.org/records/11062685/files/OCCA2HR1_analysis.tar.gz"
fil=joinpath(tempdir(),"OCCA2HR1_analysis.tar.gz")
Dataverse.downloads.Downloads.download(url,fil)
tmp=Dataverse.untargz(fil)
@test ispath(joinpath(tmp,"OCCA2HR1_analysis"))

url="https://naturalearth.s3.amazonaws.com/110m_cultural/ne_110m_admin_0_countries.zip"
fil=joinpath(tempdir(),"ne_110m_admin_0_countries.zip")
Dataverse.downloads.Downloads.download(url,fil)
Dataverse.unzip(fil,tempdir())
@test ispath(joinpath(tempdir(),"ne_110m_admin_0_countries.shp"))
end

0 comments on commit 6921421

Please sign in to comment.