1- function output = gzipdecode(input )
2- % GZIPDECODE Decompress input bytes using GZIP.
1+ function varargout = gzipdecode(varargin )
32%
4- % output = gzipdecode(input)
3+ % output = gzipdecode(input)
4+ % or
5+ % output = gzipdecode(input,info)
56%
6- % The function takes a compressed byte array INPUT and returns inflated
7- % bytes OUTPUT. The INPUT is a result of GZIPENCODE function. The OUTPUT
8- % is always an 1-by-N uint8 array. JAVA must be enabled to use the function.
9- %
10- % See also gzipencode typecast
7+ % Decompressing a GZIP-compressed byte-stream to recover the original data
8+ % This function depends on JVM in MATLAB or, can optionally use the ZMat
9+ % toolbox (http://github.com/fangq/zmat)
1110%
1211% Copyright (c) 2012, Kota Yamaguchi
1312% URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
14- % License : BSD, see LICENSE_*.txt
13+ %
14+ % Modified by: Qianqian Fang (q.fang <at> neu.edu)
15+ %
16+ % input:
17+ % input: a string, int8/uint8 vector or numerical array to store the GZIP-compressed data
18+ % info (optional): a struct produced by the zmat/lz4hcencode function during
19+ % compression; if not given, the inputs/outputs will be treated as a
20+ % 1-D vector
21+ %
22+ % output:
23+ % output: the decompressed byte stream stored in a uint8 vector; if info is
24+ % given, output will restore the original data's type and dimensions
25+ %
26+ % examples:
27+ % [bytes, info]=gzipencode(eye(10));
28+ % orig=gzipdecode(bytes,info);
29+ %
30+ % license:
31+ % BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
32+ %
33+ % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
1534%
1635
1736if (nargin == 0 )
1837 error(' you must provide at least 1 input' );
1938end
2039if (exist(' zmat' ,' file' )==2 || exist(' zmat' ,' file' )==3 )
21- output= zmat(uint8(input ),0 ,' gzip' );
40+ if (nargin > 1)
41+ [varargout{1 : nargout }]=zmat(varargin{1 },varargin{2 : end });
42+ else
43+ [varargout{1 : nargout }]=zmat(varargin{1 },0 ,' gzip' ,varargin{2 : end });
44+ end
2245 return ;
46+ elseif (isoctavemesh )
47+ error(' You must install the ZMat toolbox (http://github.com/fangq/zmat) to use this function in Octave' );
2348end
2449error(javachk(' jvm' ));
25- if ischar(input )
26- warning(' gzipdecode:inputTypeMismatch' , ...
27- ' Input is char, but treated as uint8.' );
28- input = uint8(input );
29- end
30- if ~isa(input , ' int8' ) && ~isa(input , ' uint8' )
31- error(' Input must be either int8 or uint8.' );
50+
51+ if (ischar(varargin{1 }))
52+ varargin{1 }=uint8(varargin{1 });
3253end
3354
55+ input= typecast(varargin{1 }(: )' ,' uint8' );
56+
3457gzip = java .util .zip .GZIPInputStream(java .io .ByteArrayInputStream(input ));
3558buffer = java .io .ByteArrayOutputStream();
3659org .apache .commons .io .IOUtils .copy(gzip , buffer );
3760gzip .close();
38- output = typecast(buffer .toByteArray(), ' uint8' )' ;
3961
40- end
62+ if (nargout > 0)
63+ varargout{1 } = typecast(buffer .toByteArray(), ' uint8' )' ;
64+ if (nargin > 1 && isstruct(varargin{2 }) && isfield(varargin{2 },' type' ))
65+ inputinfo= varargin{2 };
66+ varargout{1 }=typecast(varargout{1 },inputinfo .type );
67+ varargout{1 }=reshape(varargout{1 },inputinfo .size );
68+ end
69+ end
0 commit comments