DZIP - losslessly compress data into smaller memory space USAGE: Z = dzip(M) VARIABLES: M = variable to compress Z = compressed output NOTES: (1) The input variable M can be a scalar, vector, matrix, or n-dimensional matrix (2) The input variable must be a non-complex and full (meaning matrices declared as type "sparse" are not allowed) (3) Permitted input types include: double, single, logical, char, int8, uint8, int16, uint16, int32, uint32, int64, and uint64. (4) In testing, DZIP compresses several megabytes of data per second. (5) In testing, random matrices of type double compress to about 75% of their original size. Sparsely populated matrices or matrices with regular structure can compress to less than 1% of their original size. The realized compression ratio is heavily dependent on the data. (6) Variables originally occupying very little memory (less than about half of one kilobyte) are handled correctly, but the compression requires some overhead and may actually increase the storage size of such small data sets. One exception to this rule is noted below. (7) LOGICAL variables are compressed to a small fraction of their original sizes. (8) The DUNZIP function decompresses the output of this function and restores the original data, including size and class type. (9) This function uses the public domain ZLIB Deflater algorithm. (10) Carefully tested, but no warranty; use at your own risk. (11) Michael Kleder, Nov 2005
0001 function Z = dzip(M) 0002 % DZIP - losslessly compress data into smaller memory space 0003 % 0004 % USAGE: 0005 % Z = dzip(M) 0006 % 0007 % VARIABLES: 0008 % M = variable to compress 0009 % Z = compressed output 0010 % 0011 % NOTES: (1) The input variable M can be a scalar, vector, matrix, or 0012 % n-dimensional matrix 0013 % (2) The input variable must be a non-complex and full (meaning 0014 % matrices declared as type "sparse" are not allowed) 0015 % (3) Permitted input types include: double, single, logical, 0016 % char, int8, uint8, int16, uint16, int32, uint32, int64, 0017 % and uint64. 0018 % (4) In testing, DZIP compresses several megabytes of data per 0019 % second. 0020 % (5) In testing, random matrices of type double compress to about 0021 % 75% of their original size. Sparsely populated matrices or 0022 % matrices with regular structure can compress to less than 0023 % 1% of their original size. The realized compression ratio 0024 % is heavily dependent on the data. 0025 % (6) Variables originally occupying very little memory (less than 0026 % about half of one kilobyte) are handled correctly, but 0027 % the compression requires some overhead and may actually 0028 % increase the storage size of such small data sets. 0029 % One exception to this rule is noted below. 0030 % (7) LOGICAL variables are compressed to a small fraction of 0031 % their original sizes. 0032 % (8) The DUNZIP function decompresses the output of this function 0033 % and restores the original data, including size and class type. 0034 % (9) This function uses the public domain ZLIB Deflater algorithm. 0035 % (10) Carefully tested, but no warranty; use at your own risk. 0036 % (11) Michael Kleder, Nov 2005 0037 0038 s = size(M); 0039 c = class(M); 0040 cn = strmatch(c,{'double','single','logical','char','int8','uint8',... 0041 'int16','uint16','int32','uint32','int64','uint64'}); 0042 if cn == 3 | cn == 4 0043 M=uint8(M); 0044 end 0045 M=typecast(M(:),'uint8'); 0046 M=[uint8(cn);uint8(length(s));typecast(s(:),'uint8');M(:)]; 0047 f=java.io.ByteArrayOutputStream(); 0048 g=java.util.zip.DeflaterOutputStream(f); 0049 g.write(M); 0050 g.close; 0051 Z=typecast(f.toByteArray,'uint8'); 0052 f.close; 0053 return