# Archiving

The functions for working with **zip** and **tar.gz** archives are described here.

* [ArchiveName( finfo fi, str root ) str](#archivename-finfo-fi-str-root-str)
* [CloseTarGz( handle h )](#closetargz-handle-h)
* [CloseZip( handle h )](#closezip-handle-h)
* [CreateTarGz( str name ) handle](#createtargz-str-name-handle)
* [CreateZip( str name ) handle](#createzip-str-name-handle)
* [CompressFile( handle h, str fname, str packname )](#compressfile-handle-h-str-fname-str-packname)
* [ReadTarGz( str name ) arr.finfo](#readtargz-str-name-arr-finfo)
* [ReadZip( str name ) arr.finfo](#readzip-str-name-arr-finfo)
* [TarGz( str name, str path )](#targz-str-name-str-path)
* [UnpackTarGz( str name, str path )](#unpacktargz-str-name-str-path)
* [UnpackTarGz( str name, str path, arr pattern, arr ignore )](#unpacktargz-str-name-str-path-arr-pattern-arr-ignore)
* [UnpackZip( str name, str path )](#unpackzip-str-name-str-path)
* [UnpackZip( str name, str path, arr pattern, arr ignore )](#unpackzip-str-name-str-path-arr-pattern-arr-ignore)
* [Zip( str name, str path )](#zip-str-name-str-path)

## Functions

### ArchiveName(finfo fi, str root) str

The *ArchiveName* function concatenates the *Name* and *Dir* fields in a variable of *finfo* type and returns the file path for the archive relative to the root path *root*.

### CloseTarGz(handle h)

The *CloseTarGz* function finishes creating the *.tar.gz* archive. The *h* parameter is the identifier that was returned by the **CreateTarGz** function.

### CloseZip(handle h)

The *CloseZip* function finishes creating the *.zip* archive. The *h* parameter is the identifier that was returned by the **CreateZip** function.

### CreateTarGz(str name) handle

The *CreateTarGz* function starts creating a **.tar.gz** archive with the specified name. You can add files to this archive with the **CompressFile** function. The function returns an identifier, which you will need to close with the *CloseTarGz* function.

### CreateZip(str name) handle

The *CreateZip* function starts creating a **.tar.gz** archive with the specified name. You can add files to this archive with the **CompressFile** function. The function returns an identifier, which you will need to close with the *CloseZip* function.

### CompressFile(handle h, str fname, str packname)

The *CompressFile* function adds the specified *fname* file to the created archive. The *packname* parameter contains the relative path and file name to be saved in the archive. Use **/** symbol as a separator. The archive must be previously created using the **CreateZip** or **CreateTarGz** functions.

```go
    handle zip = CreateZip(`my.zip`)
    CompressFile(zip, `../data/my.txt`, `my.txt`)
    CompressFile(zip, `/home/user/folder/copy.txt`, `folder/copy.txt`)
    CloseZip(zip)
```

### ReadTarGz(str name) arr.finfo

The *ReadTarGz* function returns the list of files in the specified **tar.gz** archive. The *Name* field contains the file name together with the relative path.

```go
   arr.finfo list = ReadTarGz(`my.tar.gz`)
   for fi in list : Println( "\{fi.Name} \{fi.Size}")
```

### ReadZip(str name) arr.finfo

The *ReadZip* function returns the list of files in the specified **.zip** archive. The *Name* field contains the file name together with the relative path.

### TarGz(str name, str path)

The *TarGz* function packs the file or the contents of the *path* directory into a **.tar.gz** archive named *name*.

```go
   TarGz("/home/user/out/my.tar.gz", `/home/user/docs`)
```

### UnpackTarGz(str name, str path)

The *UnpackTarGz* function unpacks a **.tar.gz** archive named *name* into the *path* directory.

```go
   UnpackTarGz("/home/user/out/my.tar.gz", `/home/user/olddocs`)
```

### UnpackTarGz(str name, str path, arr pattern, arr ignore)

The *UnpackTarGz* function selectively unpacks a **.tar.gz** archive named *name* into the *path* directory. The *pattern* array contains file patterns to be unpacked. The *ignore* array contains file patterns to skip. The *pattern* and *ignore* parameters can be empty arrays. If the pattern begins and ends with **/**, then it is treated as a regular expression.

```go
   arr empty
   arr doc = {`*.docx`, `/.txt$/`}
   UnpackTarGz("/home/user/out/my.tar.gz", `/home/user/tmp`, doc, empty )
```

### UnpackZip(str name, str path)

The *UnpackZip* function unpacks a **.zip** archive named *name* into the *path* directory.

```go
   UnpackZip("/home/user/out/my.zip", `/home/user/olddocs`)
```

### UnpackZip(str name, str path, arr pattern, arr ignore)

The *UnpackZip* function selectively unpacks a **.tar.gz** archive named *name* into the *path* directory. The *pattern* array contains file patterns to be unpacked. The *ignore* array contains file patterns to skip. The *pattern* and *ignore* parameters can be empty arrays. If the pattern begins and ends with **/**, then it is treated as a regular expression.

```go
   arr empty
   arr skip = {`/temp.pdf/`, `/.txt$/`}
   UnpackZip("/home/user/out/my.tar.gz", `/home/user/tmp`, empty, skip )
```

### Zip(str name, str path)

The *Zip* function packs the file or the contents of the *path* directory into a **.zip** archive named *name*.

```go
   Zip("/home/user/out/mydoc.zip", `/home/user/docs/important.docx`)
```
