bhylu.com

Advertisements

Bash zip and unzip

Plan a fresh archive and extraction destination without claiming unavailable tools ran.

How it works

ZIP is another archive format used for exchanging files. Creating and extracting an archive are different operations, and encryption options are not a substitute for a reviewed confidentiality design. This environment includes unzip but no zip creator, so the complete ZIP round trip below is a syntax-checked walkthrough for a learning system with both tools installed.

Run the code

Reviewed with Git for Windows Bash 5.2.37. Open Git Bash, save the download with LF line endings in a separate learning folder, check it with bash -n lesson-name.sh, then run bash lesson-name.sh using the actual filename. Bash 4 or newer is required for the full course. Linux-only notes identify unavailable tools or POSIX behaviour. There is no Bash browser runner; Apache and MySQL are not required.

This Linux walkthrough was syntax-checked only: the required tool or POSIX behaviour was not available in the Windows review. Its expected section describes the intended result, not captured execution.

The complete source creates a fresh private temporary workspace and checks its physical path before cleanup. All file fixtures belong to that workspace; do not remove the guards or point it at existing documents.

Download complete source

Example

zip-unzip.sh

#!/usr/bin/env bash
set -u
set -o pipefail
export LC_ALL=C

# Own a new directory; verify physical paths before cleanup.
parent=$(cd -- "${TMPDIR:-/tmp}" && pwd -P) || exit 1
work=$(mktemp -d "$parent/bhylu-bash.XXXXXX") || exit 1
work=$(cd -- "$work" && pwd -P) || exit 1
case "$work" in "$parent"/bhylu-bash.??????) ;; *) exit 1 ;; esac
cleanup() {
    cd -- "$parent" || return 1
    [[ -d $work && ! -L $work ]] || return 1
    [[ $(cd -- "$work" && pwd -P) == "$work" ]] || return 1
    case "$work" in "$parent"/bhylu-bash.??????) rm -r -- "$work" ;; *) return 1 ;; esac
}
trap cleanup EXIT
cd -- "$work" || exit 1

mkdir -- source restored
printf "map revision 1\n" > source/map.txt
( cd -- source && zip ../maps.zip map.txt ) || exit 1
unzip -l maps.zip
unzip maps.zip -d restored || exit 1
cmp -- source/map.txt restored/map.txt || exit 1

Focus on the lesson commands

This excerpt omits temporary-workspace setup and cleanup. Use the complete source above to run the example.

Command excerpt

mkdir -- source restored
printf "map revision 1\n" > source/map.txt
( cd -- source && zip ../maps.zip map.txt ) || exit 1
unzip -l maps.zip
unzip maps.zip -d restored || exit 1
cmp -- source/map.txt restored/map.txt || exit 1

Expected result

With zip and unzip installed, the listing creates an archive, lists its entries and checks the restored file against the source. Utility chatter varies. ZIP creation/extraction was not executed in this review.

The subshell limits the temporary directory change and the extraction destination starts empty. Review archive entries before extracting any external download and avoid overwrite prompts in unattended work by designing a fresh destination. This teaching archive is unrelated to a Bhylu publishing ZIP; none is created for the website.

Primary technical reference: Bash documentation.

Advertisements