41 #if defined (HAVE_CONFIG_H)
55 #if defined (HAVE_BZLIB_H)
59 #if defined (HAVE_ZLIB_H)
93 fp = std::fopen (path.c_str (), mode.c_str ());
95 throw std::runtime_error (
"unable to open file");
102 throw std::runtime_error (
"unable to close file");
113 #if defined (HAVE_BZ2)
128 : source (source_path,
"rb"), dest (dest_path,
"wb")
130 bz = BZ2_bzWriteOpen (&status, dest.fp, 9, 0, 30);
132 throw std::runtime_error (
"failed to open bzip2 stream");
138 const std::size_t buf_len = 8192;
141 while ((n_read = std::fread (buf,
sizeof (buf[0]), buf_len, source.fp)) != 0)
143 if (std::ferror (source.fp))
144 throw std::runtime_error (
"failed to read from source file");
145 BZ2_bzWrite (&status, bz, buf, n_read);
146 if (status == BZ_IO_ERROR)
147 throw std::runtime_error (
"failed to write or compress");
149 if (std::ferror (source.fp))
150 throw std::runtime_error (
"failed to read from source file");
156 int abandon = (status == BZ_IO_ERROR) ? 1 : 0;
157 BZ2_bzWriteClose (&status, bz, abandon, 0, 0);
159 throw std::runtime_error (
"failed to close bzip2 stream");
170 BZ2_bzWriteClose (&status, bz, 1, 0, 0);
175 static const constexpr
char* extension =
".bz2";
180 bz2::zipper z (source_path, dest_path);
231 p =
new Bytef[str.length () +1];
232 std::strcpy (reinterpret_cast<char*> (p), str.c_str ());
235 ~uchar_array (
void) {
delete[]
p; }
238 class gzip_header :
public gz_header
246 : basename (
octave::sys::env::base_pathname (source_path))
250 throw std::runtime_error (
"unable to stat source file");
255 time = uLong (source_stat.mtime ().unix_time ());
301 #if defined (__WIN32__)
304 #elif defined (__APPLE__)
323 : source (source_path,
"rb"), dest (dest_path,
"wb"),
324 header (source_path), strm (new z_stream)
326 strm->zalloc = Z_NULL;
327 strm->zfree = Z_NULL;
328 strm->opaque = Z_NULL;
340 int status = deflateInit2 (strm, 8, Z_DEFLATED, 31, 8,
343 throw std::runtime_error (
"failed to open zlib stream");
345 deflateSetHeader (strm, &header);
347 const std::size_t buf_len = 8192;
348 unsigned char buf_in[buf_len];
349 unsigned char buf_out[buf_len];
355 strm->avail_in = std::fread (buf_in,
sizeof (buf_in[0]),
358 if (std::ferror (source.fp))
359 throw std::runtime_error (
"failed to read source file");
361 strm->next_in = buf_in;
362 flush = std::feof (source.fp) ? Z_FINISH : Z_NO_FLUSH;
369 strm->avail_out = buf_len;
370 strm->next_out = buf_out;
371 status = ::deflate (strm, flush);
372 if (status == Z_STREAM_ERROR)
373 throw std::runtime_error (
"failed to deflate");
375 std::fwrite (buf_out,
sizeof (buf_out[0]),
376 buf_len - strm->avail_out, dest.fp);
377 if (std::ferror (dest.fp))
378 throw std::runtime_error (
"failed to write file");
380 while (strm->avail_out == 0);
382 if (strm->avail_in != 0)
383 throw std::runtime_error (
"failed to write file");
385 }
while (flush != Z_FINISH);
387 if (status != Z_STREAM_END)
388 throw std::runtime_error (
"failed to write file");
394 if (deflateEnd (strm) != Z_OK)
395 throw std::runtime_error (
"failed to close zlib stream");
412 static const constexpr
char* extension =
".gz";
417 gz::zipper z (source_path, dest_path);
431 std::list<std::string> dest_paths;
433 std::function<void(const std::string&)> walk;
434 walk = [&walk, &mk_dest_path, &dest_paths] (
const std::string& path) ->
void
448 if (dirlist(
i) !=
"." && dirlist(
i) !=
"..")
458 X::zip (path, dest_path);
470 dest_paths.push_front (dest_path);
492 const std::function<std::string(const std::string&)> mk_dest_path
495 return source_path + ext;
497 return xzip<X> (source_patterns, mk_dest_path);
505 const std::function<std::string(const std::string&)> mk_dest_path
517 return xzip<X> (source_patterns, mk_dest_path);
525 if (nargin < 1 || nargin > 2)
529 =
args(0).xcellstr_value (
"%s: FILES must be a character array or cellstr",
569 return octave::xzip<octave::gz> (
"gzip",
args);
573 octave_unused_parameter (
args);
610 #if defined (HAVE_BZ2)
612 return octave::xzip<octave::bz2> (
"bzip2",
args);
616 octave_unused_parameter (
args);
Octave interface to the compression and uncompression libraries.
string_vector xzip(const Array< std::string > &source_patterns, const std::function< std::string(const std::string &)> &mk_dest_path)
int unlink(const std::string &name)
OCTINTERP_API void print_usage(void)
octave_idx_type numel(void) const
Number of elements in the array.
octave_idx_type length(void) const
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
static std::string tilde_expand(const std::string &)
static std::string basename(const std::string &s, bool strip_path=false)
RIIA wrapper for std::FILE*.
static std::string base_pathname(const std::string &s)
static std::string concat(const std::string &, const std::string &)
int mkdir(const std::string &nm, mode_t md)
=val(i)}if ode{val(i)}occurs in table i
is longer than or if then or only for unique occurrences of the complete pattern(false).The default is true.If a cell array of strings ar
octave::sys::file_stat fs(filename)
#define DEFUN_DLD(name, args_name, nargout_name, doc)
OCTAVE_EXPORT octave_value_list it applies a binary function and expands as necessary singleton dimensions in either input argument function
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
CFile(const std::string &path, const std::string &mode)
string_vector glob(void) const
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))