]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/zip.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / zip.m
1 ## Copyright (C) 2006-2012 Sylvain Pelissier
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {@var{entries} =} zip (@var{zipfile}, @var{files})
21 ## @deftypefnx {Function File} {@var{entries} =} zip (@var{zipfile}, @var{files}, @var{rootdir})
22 ## Compress the list of files and/or directories specified in @var{files}
23 ## into the archive @var{zipfile} in the same directory.  If @var{rootdir}
24 ## is defined the @var{files} are located relative to @var{rootdir} rather
25 ## than the current directory.
26 ## @seealso{unzip, bzip2, gzip, tar}
27 ## @end deftypefn
28
29 ## Author: Sylvain Pelissier <sylvain.pelissier@gmail.com>
30
31 function entries = zip (zipfile, files, rootdir = ".")
32
33   if (nargin != 2 && nargin != 3)
34     print_usage ();
35   endif
36
37   rootdir = tilde_expand (rootdir);
38
39   if (ischar (files))
40     files = cellstr (files);
41   endif
42
43   if (! ischar (zipfile) && ! iscellstr (files))
44     error ("zip: expecting all arguments to be character strings");
45   endif
46
47   cmd = sprintf ("cd %s; zip -r %s/%s %s", rootdir, pwd (), zipfile,
48                  sprintf (" %s", files{:}));
49
50   [status, output] = system (cmd);
51
52   if (status)
53     error ("zip: zip failed with exit status = %d", status);
54   endif
55
56   if (nargout > 0)
57     cmd = sprintf ("unzip -Z -1 %s", zipfile);
58     [status, entries] = system (cmd);
59     if (status)
60       error ("zip: zipinfo failed with exit status = %d", status);
61     endif
62     if (entries(end) == "\n")
63       entries(end) = [];
64     endif
65     entries = strsplit (entries, "\n");
66   endif
67
68 endfunction