|
| 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 |
0 commit comments