[Linux] Extract LHA (.lzh) Archives with the lha Command

目次

Overview

The lha command is used to manage and extract LHA files (with the .lzh extension), a compression format that was once very popular in Japan. While it is now a legacy format, it is still necessary when handling old data assets or software archives. In modern Linux environments, the standard lha command is typically used.

Note: Since it is not installed by default on many distributions, you will need to install it manually (e.g., sudo apt install lha or dnf install lha).

Specifications (Arguments and Options)

Syntax

lha [command] [options] [archive_file] [target_files...]

Main Commands (Action Specification)

CommandDescription
lList the files in the archive (does not extract).
xExtract files while maintaining the directory structure (Recommended).
eExtract files (may ignore directory structure depending on the implementation).
vShow a detailed list of files.
tTest the integrity of the archive (does not extract).

Main Options (Behavior Modification)

OptionDescription
fForce overwrite if a file with the same name exists.
qQuiet mode; does not display processing messages.
w=[DIR]Specify the destination directory for extraction.

Basic Usage

It is a safe practice to check the file list before extracting. This allows you to verify the contents and avoid accidentally extracting a large number of files.

# Verify the file type (Confirm it is an LHA archive)
file old_data_v1.lzh

# List the contents
lha l old_data_v1.lzh

Example Result

old_data_v1.lzh: LHa 2.x archive data [lh5, v1, OS-9]
 PERMSSN    UID  GID      SIZE  RATIO     STAMP   NAME
---------- ----------- ------- ------ ------------ --------------------
-rw-r--r--  1000/1000     3412  45.2% Jan 21 10:00 readme.txt
-rw-r--r--  1000/1000    15820  21.5% Jan 21 10:00 src/main.c
---------- ----------- ------- ------ ------------ --------------------
 Total         2 files   19232  31.0%

Practical Commands

Extract While Maintaining Directory Structure

Use the x command to keep the folder hierarchy exactly as it is inside the archive.

# Extract to the current directory while preserving structure
lha x old_data_v1.lzh

Specify the Output Directory

To keep your current directory clean, it is recommended to create a new folder and move into it before extracting.

# Create a directory and extract inside it
mkdir extracted_data
cd extracted_data
lha x ../old_data_v1.lzh

Extract Only Specific Files

You can extract only the files you need instead of the entire archive.

# Extract only readme.txt
lha x old_data_v1.lzh readme.txt

Customization Points

  • Forced Overwrite (f): Use the f option for batch processing when you want to overwrite existing files without confirmation.
# Extract and overwrite without confirmation
lha xf backup.lzh

Important Notes

  • Character Encoding Issues: LHA files often save filenames in Shift_JIS (Windows/DOS). Extracting them in a Linux UTF-8 environment can result in garbled filenames. Modern 7z commands may handle these better.
  • Security Risks: Since LHA is an old format, some archives may have security vulnerabilities or malicious paths. Always use the l command to inspect the contents and check for absolute paths (like /etc/...) before extracting untrusted files.
  • Availability: In some distributions like Ubuntu, you may need to enable the non-free or universe repositories. If lha is unavailable, look for the jlha-utils package or use 7z.

Application

Modern Alternative: Extract LHA with 7z

If you cannot install the lha command, the 7z command—standard in many environments—can be used as a substitute.

# Extracting with 7z (requires p7zip-full)
7z x old_data_v1.lzh

Summary

While new LHA files are rarely created today, this command is essential for recovering legacy data. The basic workflow is similar to tar or zip: use l to inspect and x to extract. If you face severe filename corruption, try using the 7z command instead.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次