File

Archives: Opening and Saving

WithFileName opens an archive in the middle of a path the way it opens a folder. export.zip*.csv is the csv files at the top of the zip, export.zipdata*.csv the ones in its data folder, and export.zip eport.csv that one file. The matching files are taken out to the GPAL temp directory when WithFileName runs, and from then on they are ordinary files: the converter, a grid, an upload or CopyTo use them without knowing an archive was involved.

The archive is known by what it holds, not by its name, so an .xpi, a .jar, an .xlsx or a download saved with no extension opens the same way. zip, 7z, rar and tar open, and so does a tar inside a gz, bz2 or xz: .tar.gz, .tgz, .tar.bz2 and .tar.xz. A gz, bz2 or xz holding a single file is a folder with one file in it, named without the extension, so data.csv.gz* is data.csv.

A url works the same when a wildcard follows the archive: https://example.com/export.zip/*.csv is fetched, then opened. Without the wildcard the url is fetched as it stands, so a link like .../tool.zip/download is not mistaken for a path inside a zip.

SaveTo makes an archive, and the destination's extension says which kind. A name is one archive: reports.zip holds every file. A wildcard keeps each file's own name: *.gz makes a.csv.gz, b.csv.gz and so on, and *.zip a zip for each. A folder goes in whole, its subfolders kept and its contents at the top of the archive, which is what an .xpi or a .vsix needs. With no folder in the destination the archive goes beside the source, and for a file taken out of an archive, beside that archive. SaveTo hands back the same files, so .Delete() after it removes the originals.

NOTE

SaveTo writes zip, gz, tar and tar.gz or .tgz. zipExtensions in GPAL.yaml lists the names written as a zip, zip, xpi, jar, vsix and nupkg to start with, and gzExtensions the names written as a gz: gz, gzip and svgz. Add a name to either list to have SaveTo write it. 7z and rar open but are not written; save those as .zip or .tar.gz. GPAL.yaml is rewritten only when it is missing or the GPAL version changes, so an existing one may not show the two lines yet. The defaults apply until it does; add the lines by hand to change them sooner.

WARNING

A gz is one file compressed, not a folder of them. Several files saved to one named .gz become reports.csv.gz, reports_0001.csv.gz and on, numbered with the counter whatever WithNextFilePattern says, since a date pattern would give them all one name. A folder cannot go in a .gz at all; SaveTo publishes an error and saves nothing. Use .zip or .tar.gz for more than one file.

Examples

GPAL Fluent: High-level fluent C# API

//The path through the zip is the whole of the reading side; there is no unzip step. The converter gets orders.csv as an ordinary file. The url opens the tar.gz because a wildcard follows it. Every .log at the top of the tar is in logs.Filenames. The cast is there because SaveTo and Delete are actions on the GPALFile itself, the same as CopyTo. SaveTo returns the same files, which is why Delete after it removes the csv files and not the zip. The extension path is a folder, so the whole tree goes in; *.gz with no folder writes each .gz beside the file it came from.

// one file out of a zip, straight into the converter

GPALFile orders = (GPALFile)GPAL.File

.WithFileName("C:\work\export.zip\orders.csv");


GPAL.Converter

.WithInput(orders)

.WithOutputType(DataFormat.JSON)

.SaveTo("C:\work\orders.json");


// a tar.gz on the web, fetched and opened on one line

GPALFile logs = (GPALFile)GPAL.File

.WithFileName("https://example.com/builds/logs.tar.gz/*.log");


// every csv in one zip, then the originals removed

((GPALFile)GPAL.File

.WithFileName("C:\reports\*.csv"))

.SaveTo("C:\out\reports.zip")

.Delete();


// an extension folder packed as an .xpi, subfolders kept

((GPALFile)GPAL.File

.WithFileName("C:\source\extension\"))

.SaveTo("C:\out\extension.xpi");


// each file gzipped beside itself under its own name

((GPALFile)GPAL.File

.WithFileName("C:\reports\*.csv"))

.SaveTo("*.gz");

💬 Ask GPAL