As a developer working on remote servers, you might encounter situations where:
- The
unzip
package isn't installed - You lack root/sudo privileges
- Package installation isn't immediately possible
Ubuntu comes with several utilities that can handle zip files:
# Using Python (works on most Ubuntu installations)
python3 -m zipfile -e archive.zip target_directory/
# Using 7z (if p7zip-full is installed)
7z x archive.zip -o/target/directory
# Using file-roller (GUI method)
file-roller --extract-here archive.zip
For more control, consider these scripting approaches:
#!/bin/bash
# Simple extraction using pure bash (for small files)
while read -r line; do
# Process each file entry
echo "Extracting: $line"
done < <(unzip -l archive.zip)
When standard methods fail, try these:
# Rename .zip to .jar and use jar command
mv archive.zip archive.jar
jar xvf archive.jar
# Use Perl's Archive::Zip module
perl -MArchive::Zip -e 'Archive::Zip->new("archive.zip")->extractTree("", "target/");'
Consider keeping these in your toolbox:
- A statically compiled unzip binary in your home directory
- Python scripts with zipfile module
- Personal script collection for such emergencies
Remember that different methods may handle various zip features differently (encryption, large files, etc.). Always verify your extracted files.
When working on Ubuntu servers, we occasionally encounter situations where:
- The
unzip
utility isn't installed - Root access isn't available for package installation
- Immediate administrative support is unavailable
Ubuntu comes with several pre-installed tools that can handle zip files:
# Using Python (works on minimal installations)
python3 -c "import zipfile; zipfile.ZipFile('archive.zip').extractall()"
# Using Perl
perl -MArchive::Zip -e 'Archive::Zip->new("archive.zip")->extractTree("")'
# Using 7z (if p7zip-full is installed)
7z x archive.zip
Method 1: Pure Bash Implementation
For simple zip files, this bash function can extract contents:
unzip_bash() {
local zipfile=$1
local dest=${2:-.}
mkdir -p "$dest"
while read -r line; do
[[ $line =~ ^inflating:\ (.*)$ ]] &&
printf '%s\n' "${BASH_REMATCH[1]}"
done < <(unzip -l "$zipfile")
}
Method 2: Using jar (Java Archive Tool)
If Java runtime is present:
jar xvf archive.zip
Partial Extraction
When you only need specific files:
# Extract just one file using Python
python3 -c "import zipfile; z=zipfile.ZipFile('archive.zip'); z.extract('specific_file.txt')"
# Using bsdtar (often available)
bsdtar -x -f archive.zip path/to/file
Network-Based Solutions
For temporary needs, consider these approaches:
# Download and pipe through curl (if zip contains single file)
curl -Ls https://example.com/archive.zip | python3 -c "import sys, zipfile, io; zipfile.ZipFile(io.BytesIO(sys.stdin.read())).extractall()"
When you regain admin access, consider:
- Installing
unzip
in your home directory - Setting up a local Python virtual environment with zip tools
- Keeping static binaries of extraction utilities