# Compress

Compress the source file or folder to the destination file or folder using the specified format:

\- zip - gzip (It will all files separately to the destination folder)

The {@code includeBaseFolder} argument is used to include the base folder as the root of the compressed file. The default is {@code true}.

The {@code overwrite} argument is used to overwrite the destination file if it already exists, else it will throw an exception. The default is {@code false}.

## Compression Levels

The {@code compressionLevel} argument is used to specify the compression level to use for the compression. The default is {@code 6}, which is a good balance between speed and compression ratio. The valid range is from {@code 0} (no compression) to {@code 9} (maximum compression).

## Method Signature

```
Compress(format=[string], source=[string], destination=[string], includeBaseFolder=[boolean], overwrite=[boolean], prefix=[string], filter=[any], recurse=[boolean], compressionLevel=[integer])
```

### Arguments

| Argument            | Type      | Required | Description                                                                                                                  | Default |
| ------------------- | --------- | -------- | ---------------------------------------------------------------------------------------------------------------------------- | ------- |
| `format`            | `string`  | `true`   | The format to use for the compression: zip or gzip. Default is zip.                                                          | `zip`   |
| `source`            | `string`  | `true`   | The absolute path to the source file or folder to compress.                                                                  |         |
| `destination`       | `string`  | `true`   | The absolute path with a file name to save as the compressed file. Extension is optional.                                    |         |
| `includeBaseFolder` | `boolean` | `false`  | Whether to include the base folder as the root of the compressed file. Default is true.                                      | `true`  |
| `overwrite`         | `boolean` | `false`  | Whether to overwrite the destination file if it already exists. Default is false.                                            | `false` |
| `prefix`            | `string`  | `false`  | The prefix directory to store the compressed files under. Default is empty.                                                  |         |
| `filter`            | `any`     | `false`  | A regular expression to filter the files to compress or a function that receives the file name and returns a boolean.        |         |
| `recurse`           | `boolean` | `false`  | Whether to compress the files recursively. Default is true.                                                                  | `true`  |
| `compressionLevel`  | `integer` | `false`  | The compression level to use for the compression. Default is 6, which is a good balance between speed and compression ratio. | `6`     |

## Examples

### Compress a file

Compress the file "example.txt" to a zip-file.

```java
compress( "zip", "example.txt", "output.zip" );

```

### Compress a directory

Compress the "example-directory" to a zip-file.

```java
compress( "zip", "example-directory", "output.zip" );

```

### Additional Examples

## Related

* [Extract](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/zip/extract)
* [IsZipFile](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/zip/iszipfile)


---

# 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://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/zip/compress.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.
