Command-Line Archive Utility

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-05-25

Problem Statement


Design and implement a command-line archive utility similar to tar or zip, which can compress, extract, and list files from an archive.


The focus is on object-oriented design, extensibility, and clean class structure rather than implementing actual compression algorithms.


๐Ÿ”น Problem Context


You are asked to design a CLI-based file archiving tool that can perform three core functions:


1.Archive (compress) a directory or file into a single archive file.
2.Extract an archive file back into its original structure.
3.List the contents of an existing archive without extracting it.


Your goal is to design the classes, interfaces, and command flow that make this utility modular and extensible โ€” e.g., support for new compression algorithms in the future.


โœ… Functional Requirements


๐Ÿงฑ Core Features


1.Create Archive
โ€ขCommand: archive create <source_path> <destination_path> [--algo gzip|zip]
โ€ขCompress files/folders into a single archive file.
โ€ขSupport at least two compression algorithms (e.g., ZIP, GZIP).
โ€ขMaintain metadata like file name, size, and timestamp.
2.Extract Archive
โ€ขCommand: archive extract <archive_path> <destination_path>
โ€ขExtracts all files and directories to the destination.
โ€ขPreserve folder hierarchy.
3.List Contents
โ€ขCommand: archive list <archive_path>
โ€ขLists all files with their size, modification date, and compression type.
4.Help / Error Handling
โ€ขShow usage instructions if invalid input is passed.
โ€ขHandle missing files, invalid paths, and corrupted archives gracefully.


โš™๏ธ Non-Functional Requirements


RequirementDescription


Modularity

Easily extendable to support new compression algorithms.


Reusability

Shared logic for file operations, compression, and metadata handling.


Performance

Efficient file read/write (stream-based).


CLI Design

Intuitive command structure and argument parsing.


Error Handling

Robust handling for missing/invalid inputs and IO exceptions.


Testability

Classes should be unit-testable independently.


๐Ÿงฉ Core Classes and Responsibilities


Class / InterfaceDescription


ArchiveUtility (Main)

Entry point for CLI. Parses arguments and delegates to the appropriate command.


Command (Interface)

Defines a execute() method for all commands (Create, Extract, List).


CreateCommand

Implements logic for compressing files and generating an archive.


ExtractCommand

Handles extraction of archive contents.


ListCommand

Lists files and metadata inside an archive.


Compressor (Interface)

Defines methods for compression/decompression.


ZipCompressor

Implements Compressor using ZIP algorithm.


GzipCompressor

Implements Compressor using GZIP algorithm.


FileMetadata

Holds file name, size, last modified date, and compression info.


Archive

Represents an archive file containing metadata and file data.


FileManager

Utility class for file IO operations (read/write/stream).


CompressorFactory

Returns appropriate Compressor implementation based on selected algorithm.


๐Ÿง  Design Patterns to Consider


โ€ขCommand Pattern โ†’ For executing CLI commands like create, extract, and list.
โ€ขStrategy Pattern โ†’ For supporting multiple compression algorithms (ZipCompressor, GzipCompressor).
โ€ขFactory Pattern โ†’ To dynamically choose compression strategy (CompressorFactory).
โ€ขTemplate Method โ†’ For common archive creation/extraction logic.


๐Ÿงพ Example Command Usage


โž• Create Archive

1 2$ archive create /user/docs /user/docs_backup.zip --algo zip

Output:

1 2Compressing 3 files... 3Created archive successfully at /user/docs_backup.zip (Size: 4.2 MB)

๐Ÿ“‚ Extract Archive

1 2$ archive extract /user/docs_backup.zip /user/restore/

Output:

1 2Extracting archive... 3Extraction complete: 3 files restored to /user/restore/

๐Ÿ“‹ List Contents

1 2$ archive list /user/docs_backup.zip

Output:

1 2Contents of docs_backup.zip: 3------------------------------------- 41. report.pdf 2.1 MB 2025-10-12 52. summary.txt 14 KB 2025-09-29 63. notes.docx 55 KB 2025-10-01 7------------------------------------- 8Total files: 3

๐Ÿงฉ Example Class Diagram (Text Form)

1 2 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 3 โ”‚ ArchiveUtility โ”‚ 4 โ”‚ - commands: Map<> โ”‚ 5 โ”‚ + main(args[]) โ”‚ 6 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 7 โ”‚ 8 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 9 โ”‚ โ”‚ โ”‚ 10โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 11โ”‚ CreateCommand โ”‚ โ”‚ ExtractCommand โ”‚ โ”‚ ListCommand โ”‚ 12โ”‚ + execute() โ”‚ โ”‚ + execute() โ”‚ โ”‚ + execute() โ”‚ 13โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 14 โ”‚ โ”‚ โ”‚ 15 โ–ผ โ–ผ โ–ผ 16โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 17โ”‚ Compressor โ”‚ โ”‚ Archive โ”‚ โ”‚ FileManager โ”‚ 18โ”‚ + compress() โ”‚ โ”‚ + metadata โ”‚ โ”‚ + read/write() โ”‚ 19โ”‚ + extract() โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 20โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 21 โ”‚ 22 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 23 โ”‚ZipCompressor โ”‚ 24 โ”‚GzipCompressorโ”‚ 25 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜


๐Ÿงฉ Sample Flow


1.User runs command โ†’ ArchiveUtility parses arguments.
2.Chooses the correct command class (CreateCommand, ExtractCommand, ListCommand).
3.CompressorFactory selects the right compression algorithm.
4.FileManager handles reading and writing file data streams.
5.Output displayed via CLI.


๐Ÿ”ฎ Extensions


โ€ขSupport for encryption during compression.
โ€ขProgress bar for large archives.
โ€ขAdd multi-threaded compression for better performance.
โ€ขAllow partial extraction of specific files.
โ€ขAdd checksum verification for integrity.


๐Ÿ’ฌ Interview Prompt Summary

Design a Command-Line Archive Utility that supports creating, extracting, and listing compressed files.
Focus on extensible design using design patterns (Command, Strategy, Factory).
Ensure modular separation between CLI logic, compression algorithms, and file handling.






Solution

Comments