Skip to content

Commit

Permalink
Implement more compression formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
dourouc05 committed Nov 26, 2019
1 parent 0c53815 commit b82f70c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ version = "0.20.0"

[deps]
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
CodecBzip2 = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd"
CodecXz = "ba30903b-d9e8-5048-a5ec-d1f5b0d4b47b"
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Expand All @@ -13,7 +15,6 @@ MathOptFormat = "f4570300-c277-12e8-125c-4912f86ce65d"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

Expand Down
2 changes: 2 additions & 0 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import MathOptInterface
const MOI = MathOptInterface
const MOIU = MOI.Utilities
using MathOptFormat
import CodecBzip2
import CodecXz
import CodecZlib

import Calculus
Expand Down
20 changes: 14 additions & 6 deletions src/file_formats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ end
List of accepted export compression formats. `AUTOMATIC_FILE_COMPRESSION`
corresponds to a detection from the file name.
"""
@enum(FileCompression, NO_FILE_COMPRESSION, GZIP, AUTOMATIC_FILE_COMPRESSION)
@enum(FileCompression, NO_FILE_COMPRESSION, BZIP2, GZIP, XZ, AUTOMATIC_FILE_COMPRESSION)

function _filename_to_compression(filename::String)
return if endswith(filename, ".gz")
return if endswith(filename, ".bz2")
BZIP2
elseif endswith(filename, ".gz")
GZIP
elseif endswith(filename, ".xz")
XZ
else
NO_FILE_COMPRESSION
end
Expand All @@ -37,8 +41,14 @@ function _open(f::Function, filename::String, mode::String; compression::FileCom
compression = _filename_to_compression(filename)
end

if compression == GZIP
stream = (mode == "r") ? CodecZlib.GzipDecompressorStream : CodecZlib.GzipCompressorStream
if compression == BZIP2 || compression == GZIP || compression == XZ
stream = if compression == BZIP2
(mode == "r") ? CodecBzip2.Bzip2DecompressorStream : CodecBzip2.Bzip2CompressorStream
elseif compression == GZIP
(mode == "r") ? CodecZlib.GzipDecompressorStream : CodecZlib.GzipCompressorStream
else # compression == XZ
(mode == "r") ? CodecXz.XzDecompressorStream : CodecXz.XzCompressorStream
end
return open(f, stream, filename, mode)
else
return open(f, filename, mode)
Expand Down Expand Up @@ -77,8 +87,6 @@ function write_to_file(model::Model, filename::String; format::FileFormat=AUTOMA
format = _filename_to_format(filename)
end

# println((filename, "w", compression))

_open(filename, "w", compression=compression) do io
write_to_file(model, io, format; kwargs...)
end
Expand Down

0 comments on commit b82f70c

Please sign in to comment.