# Archiving

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

* [ArchiveName( finfo fi, str root ) str](/stdlib/archive.md#archivename-finfo-fi-str-root-str)
* [CloseTarGz( handle h )](/stdlib/archive.md#closetargz-handle-h)
* [CloseZip( handle h )](/stdlib/archive.md#closezip-handle-h)
* [CreateTarGz( str name ) handle](/stdlib/archive.md#createtargz-str-name-handle)
* [CreateZip( str name ) handle](/stdlib/archive.md#createzip-str-name-handle)
* [CompressFile( handle h, str fname, str packname )](/stdlib/archive.md#compressfile-handle-h-str-fname-str-packname)
* [ReadTarGz( str name ) arr.finfo](/stdlib/archive.md#readtargz-str-name-arr-finfo)
* [ReadZip( str name ) arr.finfo](/stdlib/archive.md#readzip-str-name-arr-finfo)
* [TarGz( str name, str path )](/stdlib/archive.md#targz-str-name-str-path)
* [UnpackTarGz( str name, str path )](/stdlib/archive.md#unpacktargz-str-name-str-path)
* [UnpackTarGz( str name, str path, arr pattern, arr ignore )](/stdlib/archive.md#unpacktargz-str-name-str-path-arr-pattern-arr-ignore)
* [UnpackZip( str name, str path )](/stdlib/archive.md#unpackzip-str-name-str-path)
* [UnpackZip( str name, str path, arr pattern, arr ignore )](/stdlib/archive.md#unpackzip-str-name-str-path-arr-pattern-arr-ignore)
* [Zip( str name, str path )](/stdlib/archive.md#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`)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gentee.org/stdlib/archive.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
