# How to zip and unzip files and folders on your remote server

Moving thousands of small files is slow. Put them in one archive first, transfer
the archive, then extract it on the server.

This guide covers two common formats:

- `.zip` works well across Linux, macOS, and Windows
- `.tar.gz` is common on Linux and usually preserves Unix file permissions

Only extract archives you trust.

## Connect to the server with SSH

Log in with your server username and hostname:

```shell
ssh username@example.com
```

If your host gave you a custom port or a specific private key, pass those too:

```shell
ssh -p 5555 -i ~/.ssh/id_ed25519 username@example.com
```

The custom port is a server setting, not one you can choose in the command.

[How to manage multiple SSH keys with ease](/how-to-setup-and-manage-multiple-ssh-keys)

## Create and extract ZIP archives

On Ubuntu or Debian, install both ZIP commands with:

```shell
sudo apt update
sudo apt install zip unzip
```

Create an archive from one file:

```shell
zip database.zip database.sql
```

Use `-r` to include an entire directory and everything inside it:

```shell
zip -r project.zip project/
```

List an archive before extracting it:

```shell
unzip -l project.zip
```

Extract it into the current directory:

```shell
unzip project.zip
```

Or extract it into a separate directory:

```shell
unzip project.zip -d project-files
```

## Create and extract `.tar.gz` archives

`tar` and gzip do two different jobs. `tar` bundles files into one archive; gzip
compresses that archive. The result is a `.tar.gz` file, also called a tarball.

Create a compressed archive from a directory:

```shell
tar -czf project.tar.gz project/
```

The flags mean:

- `-c`: create an archive
- `-z`: compress it with gzip
- `-f`: use the following archive filename

Add `-v` if you want `tar` to print every file it processes:

```shell
tar -czvf project.tar.gz project/
```

List the contents without extracting them:

```shell
tar -tzf project.tar.gz
```

Extract into the current directory:

```shell
tar -xzf project.tar.gz
```

Or extract into an existing directory:

```shell
mkdir -p project-files
tar -xzf project.tar.gz -C project-files
```

Here, `-x` means extract and `-C` selects the destination directory.

## Transfer the archive to the server

Run `scp` on your local computer to upload an archive over SSH:

```shell
scp project.tar.gz username@example.com:/var/www/
```

For a custom SSH port, `scp` uses an uppercase `-P`:

```shell
scp -P 5555 project.tar.gz username@example.com:/var/www/
```

Then connect with SSH, change to the upload directory, and extract the archive.

## What if you only have FTP or SFTP access?

FTP and SFTP transfer files. They do not let you run `zip`, `unzip`, or `tar` on
the server.

Check whether your hosting control panel has an **Extract** or **Unarchive**
action. If it does not, extract the archive on your computer and upload the
individual files.

Avoid uploading a public PHP “unzipper” script. It creates an unnecessary way
for attackers to write files on your server. Ask your hosting provider to
extract the archive or enable SSH access instead.
