Home > atmlab > files > uncompress.m

uncompress

PURPOSE ^

% uncompress

SYNOPSIS ^

function filename = uncompress(compfile,varargin)

DESCRIPTION ^

% uncompress

 Purpose: uncompress a file and return the name of the uncompressed file

 IN        compfile        full path to compressed file
           tmpdir          The directory you want to uncompress it into
                           (optional, defaults to atmlab('WORK_AREA'))
           flags           structure with additional flags:
               unidentified --- what to do if compressed file not recognised?
                   'ignore'    default, do nothing
                   'warn'      write a message
                   'error'     issue an error
               tool --- what to use for uncompressing?
                   'shell'     default, use a shell call. Fastest.
                   'builtin'   Matlab builtin. May work when shell fails.

 OUT       filename        The fullpath to the uncompressed filename.
                           Empty if the file was never uncompressed.

 NOTE
           - Known compression formats are .gz, .zip, and .bz2
           - BEWARE: If the input file "compfile" is not uncompressed by this
           function (not recognised as a compressed file), filename = compfile.

 Salomon Eliasson and others
 $Id: uncompress.m 8371 2013-04-23 15:42:12Z gerrit $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

uncompress.m

SOURCE CODE ^

0001 function filename = uncompress(compfile,varargin)
0002 %% uncompress
0003 %
0004 % Purpose: uncompress a file and return the name of the uncompressed file
0005 %
0006 % IN        compfile        full path to compressed file
0007 %           tmpdir          The directory you want to uncompress it into
0008 %                           (optional, defaults to atmlab('WORK_AREA'))
0009 %           flags           structure with additional flags:
0010 %               unidentified --- what to do if compressed file not recognised?
0011 %                   'ignore'    default, do nothing
0012 %                   'warn'      write a message
0013 %                   'error'     issue an error
0014 %               tool --- what to use for uncompressing?
0015 %                   'shell'     default, use a shell call. Fastest.
0016 %                   'builtin'   Matlab builtin. May work when shell fails.
0017 %
0018 % OUT       filename        The fullpath to the uncompressed filename.
0019 %                           Empty if the file was never uncompressed.
0020 %
0021 % NOTE
0022 %           - Known compression formats are .gz, .zip, and .bz2
0023 %           - BEWARE: If the input file "compfile" is not uncompressed by this
0024 %           function (not recognised as a compressed file), filename = compfile.
0025 %
0026 % Salomon Eliasson and others
0027 % $Id: uncompress.m 8371 2013-04-23 15:42:12Z gerrit $
0028 
0029 [tmpdir, flags] = optargs(varargin, {atmlab('WORK_AREA'), struct()});
0030 flags = optargs_struct(flags, struct('unidentified', 'ignore', 'tool', 'shell'));
0031 
0032 [~,tmpfile,fend] = fileparts(compfile);
0033 
0034 switch fend
0035     case '.zip'
0036         switch flags.tool
0037             case 'shell'
0038                 out = exec_system_cmd(sprintf('unzip %s -d %s',compfile,tmpdir),atmlab('DEBUG'));
0039                 filename = strtrim(out{1}(strfind(out{1}, 'inflating:')+length('inflating:')+1:end));
0040             case 'builtin'
0041                 filename = cur(unzip(compfile, tmpdir), 1);
0042             otherwise
0043                 dunno(tool)
0044         end
0045     case '.gz'
0046         filename = fullfile(tmpdir,tmpfile);
0047         exec_system_cmd(sprintf('gunzip -c %s > %s',compfile,filename),atmlab('DEBUG'));
0048     case '.bz2'
0049         filename = fullfile(tmpdir,tmpfile);
0050         exec_system_cmd(sprintf('bunzip2 -c %s > %s',compfile,filename),atmlab('DEBUG'));
0051     otherwise
0052         switch flags.unidentified
0053             case 'ignore'
0054                 % do nothing...
0055             case 'warn'
0056                 logtext(atmlab('OUT'), ...
0057                 'Did not uncompress %s because I didn''t recognise the compression', ...
0058                     compfile);
0059             case 'error'
0060                 error(['atmlab:' mfilename ':unidentified'], ...
0061                     'I tried to uncompress %s, but I don''t know how!', ...
0062                         compfile);
0063             otherwise
0064                 error(['atmlab:' mfilename], 'Unknown flag %s=%s', ...
0065                     'unidentified', flags.unidentified);
0066         end
0067         filename = compfile;
0068 end
0069 
0070 end
0071 
0072 function dunno(tool)
0073 % issue error
0074 ME = MException(['atmlab:' mfilename ':unknown'], ...
0075     'Unknown tool: %s.  Read help.', tool);
0076 ME.throwAsCaller();
0077 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005