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, ftp

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

Ideally you have an SSH access to your server (SSH means Secure shell, read more about SHH here). 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 compression 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 you can install it 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 program for Gzipping (there’s also others). GNU tar supports compression through gzip and bzip2. It’s very similar to zip, but slightly 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 than the straightforward zip command, 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 a ShitServer™ (shared hosting) which only provides ftp or sftp access, then you obvs can’t run any commands on the server. Below are few possible options.

Use a control panel

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

Unzipping using a PHP script

Most shared hosting servers have PHP installed, so 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 ftp that sucker to your server, then navigate to ithttps://yourdomain.com/index2.php.

Remember to remove that script after you’re done!! Or it’ll be a major security hazard.

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

PHP also has an ability to unzip files natively. Likewise 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.

  • © 2022 Antti Hiljá
  • About
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!