Mercurial > hg > octave-lyh
comparison scripts/miscellaneous/gzip.m @ 6869:f9c893831e68
[project @ 2007-09-06 16:38:44 by dbateman]
author | dbateman |
---|---|
date | Thu, 06 Sep 2007 16:38:44 +0000 |
parents | |
children | 8b0cfeb06365 |
comparison
equal
deleted
inserted
replaced
6868:975fcdfb0d2d | 6869:f9c893831e68 |
---|---|
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 | |
7 ## the Free Software Foundation; either version 2, or (at your option) | |
8 ## 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, write to the Free | |
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 ## 02110-1301, USA. | |
19 | |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {@var{entries} =} gzip (@var{files}) | |
22 ## @deftypefnx {Function File} {@var{entries} =} gzip (@var{files}, @var{outdir}) | |
23 ## Compress the list of files and/or directories specified in @var{files}. | |
24 ## Each file is compressed separately and a new file with a '.gz' extension | |
25 ## is create. The original file is not touch. If @var{rootdir} is defined | |
26 ## the compressed versions of the files are placed in this directory. | |
27 ## @seealso{gunzip, zip, tar} | |
28 ## @end deftypefn | |
29 | |
30 function entries = gzip (files, outdir) | |
31 | |
32 if (nargin == 1 || nargin == 2) | |
33 | |
34 if (nargin == 2 && ! exist (outdir, "dir")) | |
35 error ("gzip: output directory does not exist"); | |
36 endif | |
37 | |
38 if (ischar (files)) | |
39 files = cellstr (files); | |
40 endif | |
41 | |
42 if (nargin == 1) | |
43 outdir = tmpnam (); | |
44 mkdir (outdir); | |
45 endif | |
46 | |
47 cwd = pwd(); | |
48 unwind_protect | |
49 if (iscellstr (files)) | |
50 files = glob (files); | |
51 | |
52 ## Ignore any file with a .gz extension | |
53 files (cellfun (@(x) strcmp (x(end-2:end), ".gz"), files)) = []; | |
54 | |
55 copyfile (files, outdir); | |
56 [d, f] = myfileparts(files); | |
57 cd (outdir); | |
58 | |
59 cmd = sprintf ("gzip -r %s", sprintf (" %s", f{:})); | |
60 | |
61 [status, output] = system (cmd); | |
62 | |
63 if (status == 0) | |
64 | |
65 if (nargin == 2) | |
66 gzfiles = cellfun(@(x) fullfile (outdir, sprintf ("%s.gz", x)), ... | |
67 f, "UniformOutput", false); | |
68 else | |
69 movefile (cellfun(@(x) sprintf ("%s.gz", x), f, ... | |
70 "UniformOutput", false), cwd); | |
71 gzfiles = cellfun(@(x) sprintf ("%s.gz", x), ... | |
72 files, "UniformOutput", false); | |
73 endif | |
74 | |
75 if (nargout > 0) | |
76 entries = gzfiles; | |
77 endif | |
78 else | |
79 error ("gzip: failed with exit status = %d", status); | |
80 endif | |
81 | |
82 else | |
83 error ("gzip: expecting all arguments to be character strings"); | |
84 endif | |
85 unwind_protect_cleanup | |
86 cd(cwd); | |
87 if (nargin == 1) | |
88 crr = confirm_recursive_rmdir (); | |
89 unwind_protect | |
90 confirm_recursive_rmdir (false); | |
91 rmdir (outdir, "s"); | |
92 unwind_protect_cleanup | |
93 confirm_recursive_rmdir (crr); | |
94 end_unwind_protect | |
95 endif | |
96 end_unwind_protect | |
97 else | |
98 print_usage (); | |
99 endif | |
100 | |
101 endfunction | |
102 | |
103 function [d, f] = myfileparts (x) | |
104 [d, f, ext] = cellfun (@(x) fileparts (x), x, "UniformOutput", false); | |
105 f = cellfun (@(x, y) sprintf ("%s%s", x, y), f, ext, ... | |
106 "UniformOutput", false); | |
107 idx = cellfun (@(x) isdir (x), x); | |
108 d(idx) = ""; | |
109 f(idx) = x(idx); | |
110 endfunction |