Skip to content
Snippets Groups Projects
Select Git revision
  • 436d84e395f3a2df1798ecc77b75d42f6503ebe9
  • master default protected
  • v2.0
  • v1.9.8
  • v1.9
  • v1.8
  • v1.5
  • v0.5.0
  • v0.5.1
  • v0.8.0
  • v0.8.1
  • v0.9.0
  • v0.9.1
  • v0.9.8
  • v0.9.8-1
  • v0.9.9
  • v1.0-RC1
  • v1.0-RC2
  • v1.0
  • v1.1
  • v1.2
21 results

gzipdecode.m

Blame
  • user avatar
    Qianqian Fang authored
    64f98c0b
    History
    gzipdecode.m 2.24 KiB
    function varargout = gzipdecode(varargin)
    %
    % output = gzipdecode(input)
    %    or
    % output = gzipdecode(input,info)
    %
    % Decompressing a GZIP-compressed byte-stream to recover the original data
    % This function depends on JVM in MATLAB or, can optionally use the ZMat 
    % toolbox (http://github.com/fangq/zmat)
    %
    % Copyright (c) 2012, Kota Yamaguchi
    % URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
    %
    % Modified by: Qianqian Fang (q.fang <at> neu.edu)
    %
    % input:
    %      input: a string, int8/uint8 vector or numerical array to store the GZIP-compressed data
    %      info (optional): a struct produced by the zmat/lz4hcencode function during 
    %            compression; if not given, the inputs/outputs will be treated as a
    %            1-D vector
    %
    % output:
    %      output: the decompressed byte stream stored in a uint8 vector; if info is 
    %            given, output will restore the original data's type and dimensions
    %
    % examples:
    %      [bytes, info]=gzipencode(eye(10));
    %      orig=gzipdecode(bytes,info);
    %
    % license:
    %     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
    %
    % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
    %
    
    if(nargin==0)
        error('you must provide at least 1 input');
    end
    if(exist('zmat','file')==2 || exist('zmat','file')==3)
        if(nargin>1)
            [varargout{1:nargout}]=zmat(varargin{1},varargin{2:end});
        else
            [varargout{1:nargout}]=zmat(varargin{1},0,'gzip',varargin{2:end});
        end
        return;
    elseif(isoctavemesh)
        error('You must install the ZMat toolbox (http://github.com/fangq/zmat) to use this function in Octave');
    end
    error(javachk('jvm'));
    
    if(ischar(varargin{1}))
        varargin{1}=uint8(varargin{1});
    end
    
    input=typecast(varargin{1}(:)','uint8');
    
    gzip = java.util.zip.GZIPInputStream(java.io.ByteArrayInputStream(input));
    buffer = java.io.ByteArrayOutputStream();
    org.apache.commons.io.IOUtils.copy(gzip, buffer);
    gzip.close();
    
    if(nargout>0)
        varargout{1} = typecast(buffer.toByteArray(), 'uint8')';
        if(nargin>1 && isstruct(varargin{2}) && isfield(varargin{2},'type'))
            inputinfo=varargin{2};
    	varargout{1}=typecast(varargout{1},inputinfo.type);
    	varargout{1}=reshape(varargout{1},inputinfo.size);
        end
    end