WriteFile - Maple Help

FileTools[Compressed]

 WriteFile
 write an entire compressed file

 Calling Sequence WriteFile(gzipfilename, data, opts, gzipopts) WriteFile(target in zipfilename, data, opts)

Parameters

 gzipfilename - string data - string or an rtable of hardware datatype numbers target - string zipfilename - string opts - (optional) options as specified below

Options

 • format : bzip2, zip, or gzip ; specify the output format explicitly
 • output : bytes ; write bytes of string input directly to file, even when target ends in ".m" or ".mpl". even
 • gzipopts : additional compression options to pass to zlib see FileTools[Compressed].

Description

 • WriteFile is an all-in-one command to open a file, write data in a compressed format, and close the file.
 • For bzip2 and gzip formats, the default action is to overwrite the target file.
 • For zip format, the file zipfilename is a container, so a target filename target within the archive zipfilename must be specified. This is done with the syntax target in zipfilename.  If target exists in zipfilename already, it will be replaced.  Replacing a file in a zip archive will force all other open handles to the same zip file to be closed.  If the target does not exist it will be appended to the zip file alongside any other pre-existing data.
 • By default, the bytes of a string or hardware rtable are compressed and written to the target file.  Arbitrary Maple objects, such as non-hardware rtables can be written only if the name of the in-zip file target being written ends in ".m" or ".mpl".  In these cases, the Maple data structure is converted to either external save format, or plain text format before writing.  Specifying output=bytes will override the filename matching, and always write the bytes of a string or Array (and fail on other objects).

Examples

Create a temporary file in gzip format, write data, and retrieve it.

 > $\mathrm{file}≔\mathrm{FileTools}\left[\mathrm{TemporaryFilename}\right]\left(\mathrm{FileTools}\left[\mathrm{JoinPath}\right]\left(\left[\mathrm{FileTools}\left[\mathrm{TemporaryDirectory}\right]\left(\right),"GZtest-"\right]\right)\right)$
 ${\mathrm{file}}{≔}{"/tmp/mpldoc12/GZtest-yYoYxV9R"}$ (1)
 > $\mathrm{FileTools}\left[\mathrm{Compressed}\right]\left[\mathrm{WriteFile}\right]\left(\mathrm{file},"This is a test"\right)$
 ${14}$ (2)
 > $\mathrm{FileTools}\left[\mathrm{Compressed}\right]\left[\mathrm{ReadFile}\right]\left(\mathrm{file},\mathrm{string}\right)$
 ${"This is a test"}$ (3)
 > $\mathrm{FileTools}\left[\mathrm{Compressed}\right]\left[\mathrm{WriteFile}\right]\left(\mathrm{file},\mathrm{Array}\left(1..50,0,\mathrm{datatype}=\mathrm{integer}\left[1\right]\right)\right)$
 ${50}$ (4)
 > $\mathrm{FileTools}\left[\mathrm{Compressed}\right]\left[\mathrm{ReadFile}\right]\left(\mathrm{file}\right)$
 $\left[\begin{array}{cccccccccccccccccccccccccccccccccccccccccccccccccc}{0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}& {0}\end{array}\right]$ (5)
 > $\mathrm{FileTools}\left[\mathrm{Remove}\right]\left(\mathrm{file}\right)$

Create a temporary file in gzip format, write data, and retrieve it.

 > $\mathrm{zipfile}≔\mathrm{FileTools}\left[\mathrm{TemporaryFilename}\right]\left(\mathrm{FileTools}\left[\mathrm{JoinPath}\right]\left(\left[\mathrm{FileTools}\left[\mathrm{TemporaryDirectory}\right]\left(\right),"Ziptest-"\right]\right)\right)$
 ${\mathrm{zipfile}}{≔}{"/tmp/mpldoc12/Ziptest-bx70dpPu"}$ (6)
 > FileTools[Compressed][WriteFile]("inZipName" in zipfile, "This is a zip test");
 ${18}$ (7)
 > $\mathrm{FileTools}\left[\mathrm{Compressed}\right]\left[\mathrm{Contents}\right]\left(\mathrm{zipfile}\right)$
 $\left[{"inZipName"}\right]$ (8)
 > FileTools[Compressed][WriteFile]("data" in zipfile, "This is a second file");
 ${21}$ (9)
 > $\mathrm{FileTools}\left[\mathrm{Compressed}\right]\left[\mathrm{Contents}\right]\left(\mathrm{zipfile}\right)$
 $\left[{"inZipName"}{,}{"data"}\right]$ (10)
 ${"This is a second file"}$ (11)
 ${"This is a zip test"}$ (12)
 > $\mathrm{FileTools}\left[\mathrm{Remove}\right]\left(\mathrm{zipfile}\right)$