Skip to content

Commit

Permalink
Convenience functions for dealing with .gz files
Browse files Browse the repository at this point in the history
Convenience functions to read/write directly from/to a named file.

I'm doing a cleanup of my local stash of convenience functions and thought that these might be generally useful.

```julia
gzwrite("/tmp/foo.gz", "hello")
gzreadall("/tmp/foo.gz")
"hello"
```
  • Loading branch information
samoconnor committed Jan 7, 2016
1 parent 27332bc commit afbbeef
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Libz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using BufferedStreams

export ZlibInflateInputStream, ZlibDeflateInputStream,
ZlibInflateOutputStream, ZlibDeflateOutputStream,
gzopen, gzwrite, gzreadbytes, gzreadall,
adler32, crc32


Expand All @@ -29,6 +30,24 @@ end
const decompress = inflate


function gzopen(f::Function, filename::AbstractString, mode)
@assert mode == "w"
open(filename, mode) do io
zio = ZlibDeflateOutputStream(io)
try f(zio)
finally close(zio)
end
end
end

function gzopen(f::Function, filename::AbstractString)
open(io->f(ZlibInflateInputStream(io)), filename)
end

gzwrite(filename::AbstractString, data) = gzopen(io->write(io, data), filename, "w")
gzreadall(filename::AbstractString) = gzopen(readall, filename)
gzreadbytes(filename::AbstractString) = gzopen(readbytes, filename)

end # module Libz


0 comments on commit afbbeef

Please sign in to comment.