IOUtilities.jl
An earlier version used the GZip package. The current version switch to TranscodingStreams and has some codecs as dependencies.
Read and write compressed files as with, e.g., Base.open
.
Installation
julia> using Pkg
julia> Pkg.Registry.add("https://visual2.cs.ovgu.de/roessl/VCJuliaRegistry.git")
julia> Pkg.add("IOUtilities")
julia> using IOUtilities
Compressed files
Compression algorithms are selected only by the file name extension and not contents of streams.
@assert IOUtilities.isgzipped("/path/to/file.txt.gz")
@assert IOUtilities.iscompressed("/path/to/file.txt.gz")
@assert IOUtiscompressed.!iscompressed("/path/to/file.txt")
isgzipped
and iscompressed
determine if a file is compressed (and the compression algorithm is supported).
@assert IOUtilities.iscompressionext(".gz")
@assert IOUtilities.splitcompressionext("/path/to/file.txt.gz") == ("/path/to/file.txt", ".gz")
@assert IOUtilities.splitcompressionext("/path/to/file.txt") == ("/path/to/file.txt", "")
iscompressionext
and splitcompressionext
handle file extensions
@assert IOUtilities.splitrealext("/path/to/file.txt.gz") == ("/path/to/file", ".txt", ".gz")
@assert IOUtilities.splitrealext("/path/to/file.txt") == ("/path/to/file", ".txt", "")
@assert IOUtilities.splitrealext("/path/to/file.gz") == ("/path/to/file", "", ".gz")
@assert IOUtilities.splitrealext("/path/to/file") == ("/path/to/file", "", "")
splitrealext
splits into base path, "real extension" and "compression extension".
Working with compressed files
IOUtilities.open("/path/to/file.zst", "w+") do f
print(f, "Hello!")
end
IOUtilities.open("/path/to/file.zst") do f
@assert readline(f) == "Hello!"
end
open
files for reading and writing and apply compression or decompression based on the file name extension.
Conveniently reading and writing a file
IOUtilities.write("/path/to/file.txt", "No compression!") # see file extension
IOUtilities.write("/path/to/file.txt.xz", "Hello!")
@assert IOUtilities.read("/path/to/file.txt.xz", String) == "Hello!"
@assert IOUtilities.readlines("/path/to/file.txt.xz", String) |> join == "Hello!"
read
, readlines
and write
open a file given a path and read/write contents. (You don't need to open
the file explicitly.)
Compress and uncompress files
IOUtilities.compress("/path/to/file.txt", ".bz2") # writes /path/to/file.txt.bz2
IOUtilities.uncompress("/path/to/file.txt.bz2") # writes /path/to/file.txt
IOUtilities.gzip("/path/to/file.txt") # writes /path/to/file.txt.gz
IOUtilities.gunzip("/path/to/file.txt.gz") # writes /path/to/file.txt
compress
and uncompress
read a file and write compressed or uncompressed contents to a file with file name extension added or removed, respectively.
gzip
and gunzip
are provided for convenience.
IOUtilities.transcode("/path/to/file.txt.gz", "/path/to/file.txt.lz4")
Use transcode
to "change" the compression algorithm.
All methods return the path of the written file. There is no action if input and output file paths are identical.
zip-archives
files = unzip("archive.zip"[, what=:all]) # all files
files = unzip("archive.zip", what=:first) # first file
files = unzip(==("this.file"), "archive.zip") # files matchin predicate
The returned files::Vector{String}
stores the file names of extracted files as they appear in the zip-archive.
Internals
makeistream
and [makeostream
] construct input and output streams <:IO
that decode and encode, respectively. These streams are passed to _open
which ensures that streams will be closed.
Methods
IOUtilities.compress
— Functionnewfile = compress(filepath, ext)
Compress filepath
using an algorithm determined by the file extension ext
(e.g., ".gz"
) and return name of compressed file with ext
appended.
If filepath
is already a compressed file, return filepath
without action. The compression method may differ from ext
. Use transcode
changing compression schemes.
Errors if ext
is unknown (by iscompressionext
).
See also iscompressionext
, uncompress
, open
, transcode
IOUtilities.gunzip
— Methodnewfile = ungzip(filepath)
Convenient for uncompress(filepath)
.
IOUtilities.gzip
— Methodnewfile = gzip(filepath)
Convenient for compress(filepath, ".gz")
. Calls transcode
s if filepath
is compressed by a different algorithm (and outputs a warning message).
IOUtilities.iscompressed
— Methodiscompressed(filepath)
Is filepath
compressed with a supported codec?
See also iscompressionext
, splitcxompressionext
IOUtilities.iscompressionext
— Methodiscompressionext(ext)
Does the file extension ext
indicate a compressed file (e.g., ext == ".gz"
)?
See also iscompressed
IOUtilities.isgzipped
— MethodIOUtilities.makeistream
— Methodio = makeistream(filepath)
Helper that constructs a possibly decompressed input stream.
See also iscompressed
IOUtilities.makeostream
— Functionio = makeistream(filepath)
Helper that constructs a possibly compressed output stream.
See also iscompressed
.
IOUtilities.open
— Functionopen(filepath [, mode="r"]) do ... end
Open filename
which is possibly decompressed (mode "r"
) or compressed (mode "w"
and "w+"
.
See also iscompressed
, makeistream
, makeostream
IOUtilities.read
— Methoddata =IOUtilities.read(filepath[, String])
Short for
IOUtilities.open(io -> Base.read(io[, String]), filepath)
Same as Base.read(filename,...)
but decode compressed file in case.
IOUtilities.readlines
— Methoddata =IOUtilities.readlines(filepath)
Short for
IOUtilities.open(Base.readlines, filepath)
Same as Base.readlines(filename)
but decode compressed file in case.
IOUtilities.splitcompressionext
— Method path, ext = splitcompressionext(filepath)
Splits filepath
by removing the file extension if denotes a compressed format (iscompressionext
](@ref): Same as splitext
for compressed files, otherwise returns (filepath, "")
.
Example
splitcompressionext("/a/b/c.txt.gz") -> ("a/b/c.txt", ".gz")
splitcompressionext("/a/b/c.txt") -> ("a/b/c.txt", "")
See also iscompressionext
IOUtilities.splitrealext
— Methodpath, rext, cext = splitrealext(filepath)
Split filepath
similarly to splitext
and splitcompressionext
but into three parts: the base filename, the "real" file extension and the compression extension.
Example
splitrealext(""/a/b/c.txt.gz") -> ("a/b/c", ".txt", ".gz")
splitcompressionext("/a/b/c.txt") -> ("a/b/c", ".txt", "")
splitcompressionext("/a/b/c.gz") -> ("a/b/c", "", ".gz")
See also splitcompressionext
IOUtilities.transcode
— Methodnewfile = transcode(filepath, newext)
Changes compression of filepath
to algorithm specified by newext
. Returns filepath
if file extension is equal to newext
.
transcode
fails with an error if no pair of codecs for compression and decompression is found, e.g., transcode("x.txt", ".csv")
fails.
Returns filepath
with no action of the file has already extension newext
.
See also compress
, uncompress
, iscompressed
, iscompressionext
IOUtilities.uncompress
— Methodnewfile = uncompress(filepath)
Uncompress filepath
and return name of uncompressed file with extension (e.g., ".gz") removed.
If filepath
is not a compressed file as of its file extension (examined by iscompressed
), uncompress(filepath)
just returns filepath
without action.
See also iscompressed
, compress
, open
IOUtilities.write
— Methodnb = IOUtilities.write(filepath, content)
Short for
nb = IOUtilities.open(io -> Base.write(io, content), filepath, "w+")
where nb
is the number of bytes written.
Same as Base.write(filename, content)
but write compressed file in case.