Skip to content

Commit ce3c0a0

Browse files
committed
add fallback zlib/glib support on Octave via file-based zip/unzip
1 parent 7ab1b6e commit ce3c0a0

File tree

7 files changed

+148
-4
lines changed

7 files changed

+148
-4
lines changed

Contents.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
% match_bracket - [endpos, maxlevel] = match_bracket(str,startpos,brackets)
3535
% mergestruct - s=mergestruct(s1,s2)
3636
% nestbracket2dim - [dims, isndarray, maxlevel, count] = nestbracket2dim(str,brackets)
37+
% octavezz - output = octavezz(input, iscompress, zipmethod)
3738
% savebj - bjd=savebj(obj)
3839
% savejd - savejd(rootname, obj, outputfile)
3940
% savejson - json=savejson(obj)

INDEX

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Compression and Decompression
3737
lzmaencode
3838
zlibdecode
3939
zlibencode
40+
octavezz
4041
Helper Functions
4142
decodevarname
4243
encodevarname

gzipdecode.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
end
4545
return;
4646
elseif(isoctavemesh)
47-
error('You must install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
47+
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
48+
[varargout{1:nargout}]=octavezz(varargin{1}, 0, 'gzip');
49+
return;
4850
end
4951
error(javachk('jvm'));
5052

gzipencode.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
[varargout{1:nargout}]=zmat(varargin{1},1,'gzip');
4141
return;
4242
elseif(isoctavemesh)
43-
error('You must install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
43+
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
44+
[varargout{1:nargout}]=octavezz(varargin{1}, 1, 'gzip');
45+
return;
4446
end
4547

4648
error(javachk('jvm'));

octavezz.m

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
function varargout=octavezz(data, iscompress, zipmethod)
2+
%
3+
% output = octavezz(input, iscompress, zipmethod)
4+
% or
5+
% [output, info] = octavezz(input, iscompress, zipmethod)
6+
% unzipdata = octavezz(zipdata, info)
7+
%
8+
% Compress or decompress zlib and gzip memory buffers using zip/unzip/gzip/gunzip on Octave
9+
% in case ZMat toolbox (http://github.com/NeuroJSON/zmat) was not installed (ZMat will be much faster)
10+
%
11+
% Copyright (c) 2023, Qianqian Fang (q.fang <at> neu.edu)
12+
%
13+
% input:
14+
% input: the input data (can be either compressed or before compression),
15+
% can be a string, a numerical vector or array
16+
% iscompress: (optional) if iscompress is 1, zmat compresses/encodes the input,
17+
% if 0, it decompresses/decodes the input. Default value is 1.
18+
%
19+
% if iscompress is set to a negative integer, (-iscompress) specifies
20+
% the compression level. For zlib/gzip, default level is 6 (1-9); for
21+
% lzma/lzip, default level is 5 (1-9); for lz4hc, default level is 8 (1-16).
22+
% the default compression level is used if iscompress is set to 1.
23+
%
24+
% zmat removes the trailing newline when iscompress=2 and method='base64'
25+
% all newlines are kept when iscompress=3 and method='base64'
26+
%
27+
% if one defines iscompress as the info struct (2nd output of zmat), zmat
28+
% will perform a decoding/decompression operation and recover the original
29+
% input using the info stored in the info structure.
30+
% method: (optional) compression method, only the below two methods are supported
31+
% 'zlib': zlib/zip based data compression (default)
32+
% 'gzip': gzip formatted data compression
33+
%
34+
% output:
35+
% output: the decompressed byte stream stored in a uint8 vector; if info is
36+
% given, output will restore the original data's type and dimensions
37+
% info: (optional) a struct storing additional info regarding the input data, may have
38+
% 'type': the class of the input array
39+
% 'size': the dimensions of the input array
40+
% 'byte': the number of bytes per element in the input array
41+
% 'method': a copy of the 3rd input indicating the encoding method
42+
% 'status': the zlib/lzma/lz4 compression/decompression function return value,
43+
% including potential error codes; see documentation of the respective
44+
% libraries for details
45+
% 'level': a copy of the iscompress flag; if non-zero, specifying compression
46+
% level, see above
47+
%
48+
% examples:
49+
% [ss,info]=octavezz(ones(10))
50+
% orig=octavezz(ss,info)
51+
%
52+
% license:
53+
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
54+
%
55+
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
56+
%
57+
58+
if(nargin < 1)
59+
fprintf(1,'Format: output = octavezz(data, iscompress, zipmethod)\n');
60+
return;
61+
end
62+
63+
if(nargin < 2)
64+
iscompress=1;
65+
end
66+
67+
if(nargin < 3)
68+
zipmethod='zlib';
69+
end
70+
71+
if(isstruct(iscompress))
72+
inputinfo = iscompress;
73+
iscompress = 0;
74+
end
75+
76+
fname=tempname;
77+
tmpfile=fname;
78+
outputfile=fname;
79+
80+
suff=struct('zlib', '.zip', 'gzip', '.gz');
81+
82+
if(~isfield(suff,zipmethod))
83+
error('zipmethod is not supported')
84+
end
85+
86+
if(~iscompress)
87+
tmpfile=[fname suff.(zipmethod)];
88+
end
89+
90+
fd=fopen(tmpfile,'wb');
91+
if(~fd)
92+
error('unable to create temporary file');
93+
end
94+
fwrite(fd, data, 'uint8');
95+
fclose(fd);
96+
97+
if(iscompress)
98+
outputfile=[fname suff.(zipmethod)];
99+
end
100+
101+
if(~iscompress)
102+
if(strcmp(zipmethod, 'zlib'))
103+
outputfile=unzip(tmpfile, tempdir);
104+
outputfile=[tempdir filesep outputfile{1}];
105+
elseif(strcmp(zipmethod, 'gzip'))
106+
gunzip(tmpfile);
107+
end
108+
else
109+
if(strcmp(zipmethod, 'zlib'))
110+
zip(outputfile, tmpfile);
111+
elseif(strcmp(zipmethod, 'gzip'))
112+
gzip(tmpfile);
113+
end
114+
end
115+
116+
fd=fopen(outputfile, 'rb');
117+
if(~fd)
118+
error('failed to unzip buffer');
119+
end
120+
varargout{1}=fread(fd, [1 inf], 'uint8');
121+
fclose(fd);
122+
123+
if(nargout>1)
124+
varargout{2}=struct('type',class(data),'size',size(data),'method',zipmethod,'status',0, 'level', iscompress);
125+
end
126+
127+
if (exist('inputinfo', 'var') && isfield(inputinfo, 'type'))
128+
if(strcmp(inputinfo.type,'logical'))
129+
varargout{1} = logical(varargout{1});
130+
else
131+
varargout{1} = typecast(varargout{1}, inputinfo.type);
132+
end
133+
varargout{1} = reshape(varargout{1}, inputinfo.size);
134+
end

zlibdecode.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
end
4646
return;
4747
elseif(isoctavemesh)
48-
error('You must install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
48+
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
49+
[varargout{1:nargout}]=octavezz(varargin{1}, 0, 'zlib');
50+
return;
4951
end
5052
error(javachk('jvm'));
5153

zlibencode.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
[varargout{1:nargout}]=zmat(varargin{1},1,'zlib');
4040
return;
4141
elseif(isoctavemesh)
42-
error('You must install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
42+
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
43+
[varargout{1:nargout}]=octavezz(varargin{1}, 1, 'zlib');
44+
return;
4345
end
4446

4547
error(javachk('jvm'));

0 commit comments

Comments
 (0)