# How Superfile Handles Compressed File Operations: ZIP Creation and Multi-Format Extraction

> Discover how Superfile efficiently creates ZIP files and extracts multiple archive formats with real-time UI progress. Learn about its robust compressed file operations.

- Repository: [Yorukot/superfile](https://github.com/yorukot/superfile)
- Tags: deep-dive
- Published: 2026-07-26

---

**Superfile handles compressed file operations through dedicated Go routines that support ZIP creation and multi-format extraction including TAR, GZIP, BZIP2, XZ, ZSTD, and legacy .Z archives, all integrated with a real-time UI progress bar.**

The open-source terminal file manager `yorukot/superfile` treats archive handling as a first-class file operation. Its implementation splits compressed file operations into two distinct workflows: ZIP compression via `zipSources` and multi-format extraction via `extractFile`, providing users with seamless integration between backend logic and the terminal UI.

## Compression Workflow in Superfile

The compression functionality in Superfile focuses exclusively on ZIP archive creation. The implementation resides in [`src/internal/file_operations_compress.go`](https://github.com/yorukot/superfile/blob/main/src/internal/file_operations_compress.go) and provides a complete pipeline from validation to UI feedback.

### The zipSources Implementation

The `zipSources` function serves as the primary entry point for creating ZIP archives. It accepts a slice of source paths, a target filename, and a pointer to the UI process bar model. The function walks the supplied paths using `countFiles` to calculate total progress units, then dispatches to `zipSourcesCore` for the actual archiving work.

During archive creation, `writeZipFile` generates `zip.FileHeader` entries with the `Deflate` compression method and streams file data into the archive. Progress updates flow through `processBar.SendAddProcessMsg` with `OpCompress` status, allowing users to monitor large compression jobs in real time.

### Validation and Overwrite Protection

Before creating any archive, Superfile validates that source paths exist and generates a deduplicated target name via `getZipArchiveName`. The system explicitly refuses to overwrite existing files—if `os.Create(target)` encounters an existing file, the operation aborts with a clear error message.

## Extraction Workflow and Supported Formats

Unlike compression, Superfile's extraction capabilities support multiple archive formats through extension-based dispatch in [`handle_file_operations.go`](https://github.com/yorukot/superfile/blob/main/handle_file_operations.go).

### Extension Validation and Dispatch

The `extractFile` function (called from [`handle_file_operations.go`](https://github.com/yorukot/superfile/blob/main/handle_file_operations.go) around line 412) first validates the archive extension using `IsExtensionExtractable` defined in [`src/internal/common/string_function.go`](https://github.com/yorukot/superfile/blob/main/src/internal/common/string_function.go). Based on the detected format, it routes to specialized extractors using standard library packages and third-party compression libraries.

### Supported Archive Formats

Superfile extracts the following compressed file formats:

- **ZIP** — Standard `archive/zip` reader
- **TAR** and **TGZ** — `archive/tar` with optional `compress/gzip` wrapping  
- **GZIP** — `compress/gzip` for standalone .gz files
- **BZIP2** — `github.com/klauspost/compress/bzip2`
- **XZ** — `github.com/klauspost/compress/xz`  
- **ZSTD** — `github.com/klauspost/compress/zstd`
- **.Z (Unix compress)** — `github.com/sshaman1101/dcompress`

Each extractor writes entries to the destination directory while updating the UI process bar, maintaining consistency with the compression workflow.

## UI Integration and Configuration

Superfile binds compressed file operations to configurable hot-keys defined in [`src/superfile_config/hotkeys.toml`](https://github.com/yorukot/superfile/blob/main/src/superfile_config/hotkeys.toml). The default bindings set `compress_file` to `ctrl+a` and `extract_file` to `ctrl+e`. These triggers map to the help menu entries in [`src/internal/ui/helpmenu/data.go`](https://github.com/yorukot/superfile/blob/main/src/internal/ui/helpmenu/data.go) (line 243), which displays "Extract compressed file" alongside other file operations.

On Linux systems, Superfile respects the `FS_NOCOMP_FL` flag defined in [`src/internal/ui/metadata/metadata_linux.go`](https://github.com/yorukot/superfile/blob/main/src/internal/ui/metadata/metadata_linux.go) (line 32), identifying files that should not be compressed to prevent accidental double-compression.

## Practical Code Examples

### Compressing Files Programmatically

To compress files using Superfile's internal API:

```go
import (
    "github.com/yorukot/superfile/src/internal"
    "github.com/yorukot/superfile/src/internal/ui/processbar"
)

func compressDemo() error {
    sources := []string{"./documents", "./README.md"}
    target, _ := internal.GetZipArchiveName("backup")
    bar := processbar.New()
    return internal.ZipSources(sources, target, bar)
}

```

This invokes the same `zipSources` routine used by the UI, automatically handling progress reporting and file deduplication.

### Extracting Archives Programmatically

To extract supported archives:

```go
import (
    "github.com/yorukot/superfile/src/internal"
)

func extractDemo() error {
    archive := "./backup.zip"
    destDir := "./restored"
    // Validates extension and dispatches to appropriate extractor
    return internal.ExtractFile(archive, destDir)
}

```

The `ExtractFile` function automatically detects the archive type and selects the correct extraction library.

### CLI Usage

When using Superfile from the command line:

```bash

# Create ZIP archive (equivalent to ctrl+a in UI)

superfile compress ./src ./config.toml -o project.zip

# Extract archive (equivalent to ctrl+e in UI)

superfile extract project.zip -d ./output

```

## Summary

- Superfile implements compressed file operations through two primary Go routines: `zipSources` for ZIP creation and `extractFile` for multi-format extraction.
- Compression supports only ZIP format with automatic name deduplication and overwrite protection via `os.Create` checks.
- Extraction handles ZIP, TAR, GZIP, BZIP2, XZ, ZSTD, and legacy Unix .Z formats using specialized third-party libraries.
- Both operations integrate with the UI process bar model for real-time progress tracking and cancellation support.
- Default hot-keys `ctrl+a` (compress) and `ctrl+e` (extract) are configurable via [`hotkeys.toml`](https://github.com/yorukot/superfile/blob/main/hotkeys.toml).

## Frequently Asked Questions

### What compression formats does Superfile support?

Superfile creates archives exclusively in **ZIP** format through the `zipSources` function in [`file_operations_compress.go`](https://github.com/yorukot/superfile/blob/main/file_operations_compress.go). However, it extracts a wide variety of formats including ZIP, TAR, GZIP, BZIP2, XZ, ZSTD, and legacy Unix .Z files using format-specific implementations in [`handle_file_operations.go`](https://github.com/yorukot/superfile/blob/main/handle_file_operations.go).

### How do I trigger extract and compress operations in the Superfile UI?

Use the default hot-key **ctrl+a** to trigger compression (configured as `compress_file` in [`hotkeys.toml`](https://github.com/yorukot/superfile/blob/main/hotkeys.toml)) and **ctrl+e** to trigger extraction (configured as `extract_file`). These bindings dispatch to the `zipSources` and `extractFile` routines respectively, displaying real-time progress in the process bar.

### Can Superfile extract legacy Unix .Z files?

Yes. Superfile handles the legacy Unix *compress* format (.Z) through the `github.com/sshaman1101/dcompress` dependency. When `extractFile` encounters the .Z extension, it routes to the appropriate decompressor just like it does for modern formats such as XZ and ZSTD.

### Does Superfile prevent overwriting existing files during compression?

Yes. The `zipSources` implementation in [`file_operations_compress.go`](https://github.com/yorukot/superfile/blob/main/file_operations_compress.go) checks for existing files before calling `os.Create(target)`. If the target already exists, the operation aborts immediately with an error, preventing accidental data loss.