# Accessing ZIP Files

> Source: https://manual.pvxplus.com/pxplus-user-guide/file-handling/processing-data-files/accessing-zip-files

A ZIP file (.ZIP) is a compressed archive file format. A ZIP file usually contains a single or multiple file/directory entries that have been compressed. ZIP files are often used for email attachments and backup archival purposes.

***PxPlus ZIP File Support***

Using PxPlus, you can:

- Create and/or open a ZIP file using the **[OPEN \[CREATE\]](https://manual.pvxplus.com/directives/open)** directive.
- Get metadata about all of the files/directories contained within the ZIP archive using the **[FIN( )](https://manual.pvxplus.com/functions/fin)** system function.
- Extract and read the compressed data of the files within the ZIP archive using the **[READ RECORD](https://manual.pvxplus.com/directives/read-record)** or **[EXTRACT FILE](https://manual.pvxplus.com/directives/extract-file)** directive.
- Compress and append files/directories to the ZIP archive using the **[WRITE RECORD](https://manual.pvxplus.com/directives/write-record)** or **[INSERT FILE](https://manual.pvxplus.com/directives/insert-file)** directive.
- Remove files/directories from the ZIP archive using the **[REMOVE](https://manual.pvxplus.com/directives/remove)** directive.

PxPlus uses a record/index to represent a file/directory entry within a ZIP file. The order of the records depends on the order that the file/directory entries were added to the ZIP archive.

PxPlus uses the key to represent the file/directory name of an entry in the ZIP file.

*(PxPlus ZIP file support was added in PxPlus 2014.)*

## Creating a ZIP File

To ***create*** a ZIP file, use the **[OPEN CREATE](https://manual.pvxplus.com/directives/open)** directive with OPT="ZIP":

```
OPEN CREATE (chan, OPT="ZIP") "myZIPFile.zip"
```

## Opening a ZIP File

To ***open*** a ZIP file, use the standard **[OPEN](https://manual.pvxplus.com/directives/open)** directive:

```
OPEN (chan) "myZIPFile.zip"
```

## Getting ZIP Metadata

Use the **[FIN( )](https://manual.pvxplus.com/functions/fin)** system function to get metadata about the compressed files/directories within the ZIP archive. The file/directory entry that you get metadata about is the next record. If there is no next record, a null value is returned.

The "ZIPFilename" keyword returns the file/directory name of the next ZIP archive entry. Directory names will always end in a '**/**' *(forward slash).*

**FIN** (*chan*, "ZIPFilename")

The "ZIPInfo" keyword will return all of the metadata about a file/directory in a ZIP archive.

**FIN** (*chan*, "ZIPInfo")

The key system functions **[KEF( )](https://manual.pvxplus.com/functions/kef)**, **[KEP( )](https://manual.pvxplus.com/functions/kep)**, **[KEC( )](https://manual.pvxplus.com/functions/kec)**, **[KEY( )](https://manual.pvxplus.com/functions/key)**, **[KEN( )](https://manual.pvxplus.com/functions/ken)** and **[KEL( )](https://manual.pvxplus.com/functions/kel)** can also be used to return the file/directory name of ZIP archive entries. KEY(*chan*) and FIN(*chan*, "ZIPFilename"**)** are equivalent and will return the same file/directory name.

## Extracting from a ZIP File

You can ***extract* a *file*** in two ways:

- You can use the **[EXTRACT FILE](https://manual.pvxplus.com/directives/extract-file)** directive, which will decompress the file in the archive and put it on your system. ***OR***
- You can use the **[READ RECORD](https://manual.pvxplus.com/directives/read-record)** directive, which will return the decompressed binary data of the compressed file in the archive.

If the file/record is a *directory*, the **EXTRACT FILE** directive will result in an Error #3: Input/Output error on file. The **READ RECORD** directive will return an empty string.

To use **EXTRACT FILE**, you must know the name of the file in the ZIP archive you want to extract.

```
EXTRACT FILE "new_filename" FROM (chan, KEY="filename")
```

If the path and filename of the file to *extract* are the same as what you want to put on your system, then you can leave out the *KEY=* option.

```
EXTRACT FILE "filename" FROM (chan)
```

PxPlus supports the following uses of **READ RECORD** for ZIP files:

- ***Sequential -*** Where the files/directories entries in the ZIP archive are read sequentially in the order that they were added.

```
READ RECORD (chan) data$
```

- ***By Key -*** Where the key is the filename of the file within the ZIP archive.

```
READ RECORD (chan, KEY="filename") data$
```

- ***By Record Number -*** Where the record number assigned to the files/directory entries is based on the order that they were added into the ZIP archive. Record numbers start at one.

```
READ RECORD (chan, RNO=recNum) data$
```

- ***By Index Number -*** Where the index number assigned to the files/directory entries is based on the order that they were added into the ZIP archive. Index numbers start at zero.

```
READ RECORD (chan, IND=indNum) data$
```

Note:  
It is also possible to use the [SELECT RECORD](https://manual.pvxplus.com/directives/select) directive to extract from a ZIP file. As the entries in a ZIP file are not sortable, the use of BEGIN and END with the SELECT RECORD is *not* supported.

## Appending to a ZIP File

You can *append a file* to the end of the ZIP archive in two ways:

- Use the **[INSERT FILE](https://manual.pvxplus.com/directives/insert-file)** directive, which will compress a file on your system and append it to the end of the ZIP archive.

***or***

- Use the **[WRITE RECORD](https://manual.pvxplus.com/directives/write-record)** directive, which will compress binary data and append it to the end of the ZIP archive.

If the file/record is a *directory*, the **[INSERT FILE](https://manual.pvxplus.com/directives/insert-file)** directive will result in an Error #3: Input/Output error on file.

To use **INSERT FILE**, you must know the name of the file you want to put in the ZIP archive.

```
INSERT FILE "filename" TO (chan, KEY="new_filename")
```

Note:  
UNIX/Linux does *not* support appending a file to the ZIP archive if the filename contains either a \\ (backslash) or a : (colon).

If the path and filename of the file to *zip* are the same as what you want to put in the ZIP archive, then you can leave out the *KEY=* option.

```
INSERT FILE "filename" TO (chan)
```

PxPlus supports appending both files and directories:

- To *append a file*, use the **[WRITE RECORD](https://manual.pvxplus.com/directives/write-record)** directive, where the *key* is the name of the *file* to append to the ZIP archive, and *contents* contains the binary data of the file to add to the ZIP archive.

```
WRITE RECORD (chan, KEY="filename") contents$
```

- To *append a directory* to the end of the ZIP archive, issue a **[WRITE RECORD](https://manual.pvxplus.com/directives/write-record)** directive, where the *key* is the name of the *directory* to append to the ZIP archive, and *contents* is an empty string. Directory names must end in a '**/**' *(forward slash)* to be recognized as directory names.

```
WRITE RECORD (chan, KEY="dir/") ""
```

## Removing from a ZIP File

To *remove a file/directory entry* from a ZIP archive, you can use the **[REMOVE](https://manual.pvxplus.com/directives/remove)** directive. PxPlus supports the following uses of **REMOVE** for ZIP files:

- ***Sequential -*** Where the files/directories entries in the ZIP archive are removed sequentially in the order that they were added.

```
REMOVE(chan)
```

- ***By Key -*** Where the key is the name of the file/directory entry within the ZIP archive. If it is a directory name, it must end in a '**/**' *(forward slash).*

```
REMOVE (chan, KEY="filename")
```

- ***By Index Number -*** Where the index number assigned to the files/directory entries is based on the order that they were added into the ZIP archive. Index numbers start at zero.

```
REMOVE (chan, IND=indNum)
```

## See Also

**[\*TOOLS/ZIP](https://manual.pvxplus.com/utilities/zipcontents)** and **[\*TOOLS/UNZIP](https://manual.pvxplus.com/utilities/unzipcontents)** Utilities  
**[EXTRACT FILE Directive](https://manual.pvxplus.com/directives/extract-file)**  
**[INSERT FILE Directive](https://manual.pvxplus.com/directives/insert-file)**  
**[READ RECORD Directive](https://manual.pvxplus.com/directives/read-record)  
[REMOVE Directive](https://manual.pvxplus.com/directives/remove)**  
**[WRITE RECORD Directive](https://manual.pvxplus.com/directives/write-record)**
