octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: decompress any compressed type


From: Bill Denney
Subject: Re: decompress any compressed type
Date: Tue, 17 Oct 2006 07:22:52 -0400
User-agent: Thunderbird 1.5.0.7 (Windows/20060909)

David Bateman wrote:
Bill Denney wrote:
scripts/ChangeLog:

2006-10-17  Bill Denney  <address@hidden>
   * miscellaneous/decompress.m: new function, decompress any file
based on its extension
This is a good idea. However, rather than throwing an error if the
extension is not recognized, can you set "files = file" and return.....
Here is that version (also with a slightly better help description).

Bill
## Copyright (C) 2005 Bill Denney
## 
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, write to the Free
## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
## 02110-1301, USA.

## -*- texinfo -*-
## @deftypefn {Function File} address@hidden =} decompress (@var{file}, 
@var{dir})
## Unpack the archive @var{file} based on its extension to the directory
## @var{dir}.  If @var{file} is a cellstr, then all files will be
## handled individually.  If @var{dir} is not specified, it defaults to
## the current directory.  It returns a list of @var{files} decompressed.
## @seealso{tar, untar, gzip, gunzip, zip, unzip}
## @end deftypefn

## Author: Bill Denney <address@hidden>

function files = decompress (file, directory)

  if (nargin == 1 || nargin == 2)

    if (nargin == 1)
      directory = ".";
    endif

    if ischar (file)
      [pathstr, name, ext] = fullfile (file);

      ## check to see if it's .tar.gz, .tar.Z, or .tar.bz2
      if strcmp (ext, ".gz") || strcmp (ext, ".Z") || strcmp (ext, ".bz2")
        [tmppathstr, tmpname, tmpext] = fullfile (name);
        if strcmp(tmpext, ".tar")
          name = tmpname;
          ext = [tmpext ext];
        endif
      endif

    elseif iscellstr (file)
      files = {};
      for i = 1:numel (file)
        tmpfiles = decompress (file{i}, directory);
        files = {files{:} tmpfiles{:}};
      endfor

    else
      error ("decompress: invalid input file class, %s", class(file));
    endif

    switch lower(ext)
      case {".gz" ".Z"}
        files = gunzip (file, directory);
      case {".tar" ".tar.gz" ".tgz" ".tar.Z"}
        files = untar (file, directory);
      case {".zip"}
        files = unzip (file, directory);
      ## case {".bz2"}
      ##   files = bunzip2 (file, directory);
      otherwise
        warning ("decompress:filetype", "unrecognised file type, %s", ext);
        files = file;
    endswitch

  else
    print_usage ();
  endif

endfunction

reply via email to

[Prev in Thread] Current Thread [Next in Thread]