6869
|
1 ## Copyright (C) 2007 David Bateman |
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6869
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6869
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {@var{entries} =} gzip (@var{files}) |
|
21 ## @deftypefnx {Function File} {@var{entries} =} gzip (@var{files}, @var{outdir}) |
|
22 ## Compress the list of files and/or directories specified in @var{files}. |
|
23 ## Each file is compressed separately and a new file with a '.gz' extension |
7001
|
24 ## is created. The original file is not touched. If @var{rootdir} is defined |
6869
|
25 ## the compressed versions of the files are placed in this directory. |
|
26 ## @seealso{gunzip, zip, tar} |
|
27 ## @end deftypefn |
|
28 |
|
29 function entries = gzip (files, outdir) |
|
30 |
|
31 if (nargin == 1 || nargin == 2) |
|
32 |
|
33 if (nargin == 2 && ! exist (outdir, "dir")) |
|
34 error ("gzip: output directory does not exist"); |
|
35 endif |
|
36 |
|
37 if (ischar (files)) |
|
38 files = cellstr (files); |
|
39 endif |
|
40 |
|
41 if (nargin == 1) |
|
42 outdir = tmpnam (); |
|
43 mkdir (outdir); |
|
44 endif |
|
45 |
|
46 cwd = pwd(); |
|
47 unwind_protect |
|
48 if (iscellstr (files)) |
|
49 files = glob (files); |
|
50 |
|
51 ## Ignore any file with a .gz extension |
|
52 files (cellfun (@(x) strcmp (x(end-2:end), ".gz"), files)) = []; |
|
53 |
|
54 copyfile (files, outdir); |
|
55 [d, f] = myfileparts(files); |
|
56 cd (outdir); |
|
57 |
|
58 cmd = sprintf ("gzip -r %s", sprintf (" %s", f{:})); |
|
59 |
|
60 [status, output] = system (cmd); |
|
61 |
|
62 if (status == 0) |
|
63 |
|
64 if (nargin == 2) |
|
65 gzfiles = cellfun(@(x) fullfile (outdir, sprintf ("%s.gz", x)), ... |
|
66 f, "UniformOutput", false); |
|
67 else |
|
68 movefile (cellfun(@(x) sprintf ("%s.gz", x), f, ... |
|
69 "UniformOutput", false), cwd); |
|
70 gzfiles = cellfun(@(x) sprintf ("%s.gz", x), ... |
|
71 files, "UniformOutput", false); |
|
72 endif |
|
73 |
|
74 if (nargout > 0) |
|
75 entries = gzfiles; |
|
76 endif |
|
77 else |
|
78 error ("gzip: failed with exit status = %d", status); |
|
79 endif |
|
80 |
|
81 else |
|
82 error ("gzip: expecting all arguments to be character strings"); |
|
83 endif |
|
84 unwind_protect_cleanup |
|
85 cd(cwd); |
|
86 if (nargin == 1) |
|
87 crr = confirm_recursive_rmdir (); |
|
88 unwind_protect |
|
89 confirm_recursive_rmdir (false); |
|
90 rmdir (outdir, "s"); |
|
91 unwind_protect_cleanup |
|
92 confirm_recursive_rmdir (crr); |
|
93 end_unwind_protect |
|
94 endif |
|
95 end_unwind_protect |
|
96 else |
|
97 print_usage (); |
|
98 endif |
|
99 |
|
100 endfunction |
|
101 |
|
102 function [d, f] = myfileparts (x) |
|
103 [d, f, ext] = cellfun (@(x) fileparts (x), x, "UniformOutput", false); |
|
104 f = cellfun (@(x, y) sprintf ("%s%s", x, y), f, ext, ... |
|
105 "UniformOutput", false); |
|
106 idx = cellfun (@(x) isdir (x), x); |
|
107 d(idx) = ""; |
|
108 f(idx) = x(idx); |
|
109 endfunction |