Extracting Files
Purpose
This guide covers the fundamental extraction operations for extracting archive contents to disk.
Concepts
Extraction is the process of decompressing and writing archive contents to a target directory. Excavate handles format detection, directory creation, and file writing automatically.
Basic Extraction
Extract to Auto-Created Directory
The simplest extraction creates a directory based on the archive name:
require 'excavate'
archive = Excavate::Archive.new('package.zip')
archive.extract
# Creates ./package/ directory with contents
# Returns: "/path/to/package"The directory name is derived from the archive filename without extension.
Target Directory Rules
Excavate enforces safety rules for target directories:
Directory Must Not Exist
If the target file or directory exists, an error is raised:
begin
Excavate::Archive.new('package.zip').extract
rescue Excavate::TargetExistsError => e
puts "Target exists: #{e.message}"
endSupported Archive Types
Excavate automatically detects the archive format by extension:
| Extension | Format | Notes |
|---|---|---|
| ZIP | Standard ZIP format |
| TAR | Unix tape archive |
| TAR + GZIP | Compressed tarball |
| TAR + XZ | XZ-compressed tarball |
| 7-Zip | High compression format |
| Microsoft CAB | Cabinet archive |
| Windows Installer | OLE compound document |
| RPM Package | Red Hat package |
| XAR/PKG | macOS installer |
| Self-extracting | 7-Zip or CAB SFX |
Working with Different Formats
TAR Files
# Plain TAR
Excavate::Archive.new('archive.tar').extract
# Compressed TAR
Excavate::Archive.new('archive.tar.gz').extract
Excavate::Archive.new('archive.tar.xz').extractCLI Usage
Extract from the command line:
# Basic extraction
excavate package.zip
# Extract specific files
excavate archive.tar.gz file1.txt file2.txtError Handling
Handle extraction errors gracefully:
begin
Excavate::Archive.new('archive.zip').extract('/target')
rescue Excavate::TargetExistsError
puts "Target already exists"
rescue Excavate::TargetNotEmptyError
puts "Target directory is not empty"
rescue Excavate::UnknownArchiveError
puts "Unknown or unsupported archive format"
rescue Excavate::Error => e
puts "Extraction failed: #{e.message}"
endNext Steps
-
Learn Selective Extraction for extracting specific files
-
Explore Filter Patterns for pattern-based extraction
-
See File Iteration for processing files during extraction