FileTools[Compressed] - Maple Programming Help

Home : Support : Online Help : Programming : Input and Output : File Manipulation : FileTools : FileTools/Compressed/ReadFile

FileTools[Compressed]

Parameters

 filename - string or string:string outputtype - (optional) either string, ByteArray, or 'Array'(m,n,hwdatatype) opts - (optional) options as specified below

Options

 • format : zip or gzip ; specify the input format explicitly

Description

 • ReadFile is an all-in-one command to open a file, read and decompress its data, and close the file.
 • If the given file is in zip format, and an in-zip name is not specified (using inzipname in zipfilename), then the first member of the zip archive is read.
 • If the second parameter, outputtype, is string then the contents of the file are returned as a string.
 • If the outputtype is not specified and the in-zip file name ends in ".m", then the contents of the zip file will be parsed as if they were in Maple's internal object format.
 • If the outputtype is not specified and the in-zip file name ends in ".mpl", then the contents of the zip file will be parsed as if they were a single Maple-syntax text command.
 • If outputtype is Array, then m and n are integers specifying the dimensions of the Array.  The dimensions are optional, and one or more can be set.  The hwdatatype indicates one of the integer[], float[], or complex[] datatype variations accepted by the 'datatype' option of the Array command.
 • If outputtype is set to bytes or does not match any of the above criteria then an Array of datatype integer[1] is returned.

Examples

Read from a file in gzip format.

 > $\mathrm{file}≔\mathrm{FileTools}[\mathrm{TemporaryFilename}]\left(\mathrm{cat}\left(\mathrm{FileTools}[\mathrm{TemporaryDirectory}]\left(\right),\mathrm{kernelopts}\left(\mathrm{dirsep}\right),"GZtest-"\right)\right)$
 ${\mathrm{file}}{≔}{"/mnt/tmpfs/mpldoc_slot3/mpldoc2/GZtest-yYoYxV9R"}$ (1)
 > $\mathrm{FileTools}[\mathrm{Compressed}][\mathrm{WriteFile}]\left(\mathrm{file},"This is a test"\right)$
 ${14}$ (2)
 > $\mathrm{FileTools}[\mathrm{Compressed}][\mathrm{ReadFile}]\left(\mathrm{file},\mathrm{string}\right)$
 ${"This is a test"}$ (3)
 > $\mathrm{FileTools}[\mathrm{Compressed}][\mathrm{WriteFile}]\left(\mathrm{file},\mathrm{Array}\left(1..50,0,\mathrm{datatype}=\mathrm{integer}[1]\right)\right)$
 ${50}$ (4)
 > $\mathrm{FileTools}[\mathrm{Compressed}][\mathrm{ReadFile}]\left(\mathrm{file}\right)$
 $\left[\begin{array}{c}{\mathrm{1 .. 50}}{\mathrm{Array}}\\ {\mathrm{Data Type:}}{\mathrm{integer}}{[}{1}{]}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (5)
 > $\mathrm{FileTools}[\mathrm{Remove}]\left(\mathrm{file}\right)$

Read from a file in zip format.

 > $\mathrm{zipfile}≔\mathrm{FileTools}[\mathrm{TemporaryFilename}]\left(\mathrm{cat}\left(\mathrm{FileTools}[\mathrm{TemporaryDirectory}]\left(\right),\mathrm{kernelopts}\left(\mathrm{dirsep}\right),"GZtest-"\right),".zip"\right)$
 ${\mathrm{zipfile}}{≔}{"/mnt/tmpfs/mpldoc_slot3/mpldoc2/GZtest-bx70dpPu.zip"}$ (6)
 > FileTools[Compressed][WriteFile]("first" in zipfile, "one");
 ${3}$ (7)
 > FileTools[Compressed][WriteFile]("second" in zipfile, Array(1..50, 0, datatype=integer[1]));
 ${50}$ (8)
 > $\mathrm{FileTools}[\mathrm{Compressed}][\mathrm{Contents}]\left(\mathrm{zipfile}\right)$
 $\left[{"first"}{,}{"second"}\right]$ (9)
 > $\mathrm{FileTools}[\mathrm{Compressed}][\mathrm{ReadFile}]\left(\mathrm{zipfile},\mathrm{string}\right)$
 ${"one"}$ (10)
 $\left[\begin{array}{c}{\mathrm{1 .. 50}}{\mathrm{Array}}\\ {\mathrm{Data Type:}}{\mathrm{integer}}{[}{1}{]}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (11)
 > $\mathrm{FileTools}[\mathrm{Remove}]\left(\mathrm{zipfile}\right)$

Compatibility

 • The FileTools[Compressed][ReadFile] command was introduced in Maple 16.