clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

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

Filed under: Server— Tagged with: shell, ssh

Sometimes you need to move a ton files to a server, it’s 100 times faster to move as chunk than one-by-one.

Ideally you have an SSH access to your server (SSH means Secure shell, read more about it if you like). Meaning you log into your server using the Terminal (Mac) or Putty if your on Windows. If you don’t have SSH access, don’t worry, there is another way (more on that at the end of this article).

Login with SSH

If you’re not sure how to login, here’s how that happens:

$ ssh username@domainname.com

Using a custom port (good idea) and an ssh key:

$ ssh -i ~/.ssh/id_rsa_foo -p 5555 username@domainname.com

Or using an IP rather than a hostname:

$ ssh -i ~/.ssh/id_rsa_foo -p 5555 username@12.12.12.123

That should get you in.

Using zip

There’s many compressing algorithms, zip is one of the popular ones. It uses the .zip file extension.

If you don’t have zip installed, in Ubuntu and Debian it can grabbed with apt-get:

$ sudo apt-get install zip

Compressing using zip

A single file:

$ zip my-database.zip my-database.sql

When zipping a directory, use the recursive flag:

$ zip -r my-directory.zip my-directory/

Here’s the breakdown:

-r
The recursive flag, includes all files and directories.
my-directory.zip
Name of the generated zip file.
my-directory/
Path to the directory that will be compressed.

Decompressing with zip

$ unzip archive.zip

Using gzip

This post looks into the GNU tar application to do Gzipping (there are also others). GNU tar supports both archive compressing through gzip and bzip2.

It’s very similar to zip, but more effective. Gzip files have .tar.gz or .tgz extension, and are usually referred to as tarballs.

Compressing with tar

The tar command syntax is more nebulous, but there’s an effective memory rule:

Screenshot of a message board thread
How to make a tar file: tar -xzf: extract ze files. tar -czf: compress ze files. tar -xzvf: xtract ze vucking files. Source: dev.to/damian/comment/14m7

Compress a directory with tar files (compress ze files):

$ tar -czf target-file.tar.gz /source/directory

A add v for verbose output: -czvf.

Extracting with tar

Decompress a file (xtract ze vucking files):

$ tar -xzvf compressed-file.tar.gz

Unzipping files on an ftp server

If you’re dealing with ShitServer™ (shared hosting) that only provides ftp or sftp access, then you obvs can’t really run any commands on the server. Below are few possible options.

Use the control panel

Sometimes a shared hosting server will provide control panel (Plesk etc.) where you have file browser that can unzip files.

Unzipping using a PHP script

But most shared hosting servers have PHP installed, you can try to use a PHP script for unzipping.

This PHP unzipper1.zip script has worked for me in the past. Unzip it and throw to your server, then navigate to https://yourdomain.com/index2.php/.

Remember to remove that script after your done!!

Or maybe this similar PHP script works better for you. I haven’t tested that, though.

PHP is also able to natively unzip files. I haven’t tried this one but maybe it’s what you’re looking for:

// Assuming `file.zip` is in the same directory as the executing script.
$file = 'file.zip';

// Get the absolute path to $file.
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);

$zip = new ZipArchive;
$res = $zip->open($file);

if ($res === TRUE) {
  // Extract it to the path we determined above
  $zip->extractTo($path);
  $zip->close();
  echo "WOOT! $file extracted to $path";
} else {
  echo "Doh! I couldn't open $file";
}

The PHP snipped is picked up from this StackOverflow post.

Comments would go here, but the commenting system isn’t ready yet, sorry. Tweet me @hiljaa if you want to make a correction etc.

  • © 2021 Antti Hiljá
  • About
  • Follow me in Twatter → @hiljaa
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • console.log('Smash the patriarchy!')
  • I love u!