Compress
Compresses a single file or folder using the appropriate compression format based on the target file extension.
Common Properties
- Name - The custom name of the node.
- Color - The custom color of the node.
- Delay Before (sec) - Waits in seconds before executing the node.
- Delay After (sec) - Waits in seconds after executing node.
- Continue On Error - Automation will continue regardless of any error. The default value is false.
If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.
Inputs
-
Source Path - Path to the source file or folder to compress. Can be a single file or an entire directory.
-
Target Path - Destination path for the compressed file. The file extension determines the compression format to use.
How It Works
The Compress node automatically selects the compression format based on the target file extension. The node:
- Validates that the source path exists
- Detects the compression format from the target file extension
- For archive formats (Zip), compresses the file or folder directly
- For pure compression formats (Gzip, Bzip2, etc.), wraps the content in a tar archive to preserve filenames and directory structure
- Saves the compressed file to the target path
Supported Compression Formats
The following formats are supported based on the target file extension:
Archive Format
- .zip - Universal Zip archive format
- Use for: Maximum compatibility across platforms
- Best for: Multiple files, cross-platform sharing
Compression Formats (with Tar wrapper)
-
.gz, .gzip - Gzip compression
- Use for: Fast compression with good compression ratio
- Best for: General purpose compression, log files
-
.bz2, .bzip2 - Bzip2 compression
- Use for: Better compression ratio than Gzip
- Best for: When file size is more important than speed
-
.zst, .zstd - Zstandard compression
- Use for: Modern compression with excellent speed/ratio balance
- Best for: Large files, backup operations
-
.xz - XZ compression
- Use for: Maximum compression ratio
- Best for: Long-term storage, distribution packages
-
.lz4 - LZ4 compression
- Use for: Extremely fast compression and decompression
- Best for: Real-time compression, temporary files
-
.br - Brotli compression
- Use for: Web content optimization
- Best for: Text files, web assets
Examples
Example 1: Compress a File with Gzip
Compress a log file using Gzip compression:
// Set source and target paths
msg.sourceFile = "/home/user/logs/application.log";
msg.compressedFile = "/home/user/archives/application.log.gz";
Configure the Compress node:
- Source Path:
{{msg.sourceFile}} - Target Path:
{{msg.compressedFile}}
Example 2: Compress a Folder to Zip
Compress an entire directory into a Zip file:
// Compress a project folder
msg.projectDir = "/home/user/project";
msg.zipFile = "/home/user/backups/project.zip";
Configure the Compress node:
- Source Path:
{{msg.projectDir}} - Target Path:
{{msg.zipFile}}
Example 3: Compress with Zstandard for Best Balance
Use Zstandard compression for optimal speed and compression ratio:
// Compress a database backup
msg.dbBackup = "/home/user/backups/database.sql";
msg.compressedBackup = "/home/user/archives/database.sql.zst";
Configure the Compress node:
- Source Path:
{{msg.dbBackup}} - Target Path:
{{msg.compressedBackup}}
Example 4: Maximum Compression with XZ
Use XZ compression for the smallest possible file size:
// Archive data with maximum compression
msg.dataDir = "/home/user/data/reports";
msg.archiveFile = "/home/user/archives/reports.tar.xz";
Configure the Compress node:
- Source Path:
{{msg.dataDir}} - Target Path:
{{msg.archiveFile}}
Example 5: Fast Compression with LZ4
Use LZ4 for extremely fast compression:
// Quickly compress temporary files
msg.tempDir = "/home/user/temp/processing";
msg.compressedTemp = "/home/user/temp/processing.lz4";
Configure the Compress node:
- Source Path:
{{msg.tempDir}} - Target Path:
{{msg.compressedTemp}}
Example 6: Dynamic Compression Format
Choose compression format dynamically based on file size:
const fs = require('fs');
// Get file size
const stats = fs.statSync('/home/user/data/report.txt');
const fileSizeInMB = stats.size / (1024 * 1024);
msg.sourceFile = "/home/user/data/report.txt";
// Use fast compression for small files, maximum compression for large files
if (fileSizeInMB < 10) {
msg.targetFile = "/home/user/archives/report.txt.lz4"; // Fast
} else {
msg.targetFile = "/home/user/archives/report.txt.xz"; // Best compression
}
Configure the Compress node:
- Source Path:
{{msg.sourceFile}} - Target Path:
{{msg.targetFile}}
Usage Tips
-
Format Selection:
- Speed Priority: Use LZ4 for fastest compression/decompression
- Size Priority: Use XZ for smallest file size
- Balanced: Use Zstandard for best overall performance
- Compatibility: Use Zip for maximum compatibility
-
File vs Folder Compression:
- For Zip format, folders are archived and compressed directly
- For other formats (gz, xz, etc.), folders are automatically wrapped in a tar archive
-
Extension Matters: The compression format is automatically determined by the target file extension. Ensure you use the correct extension.
-
Performance Considerations:
- Compression speed varies significantly between formats
- LZ4 is fastest but produces larger files
- XZ is slowest but produces smallest files
- Zstandard offers the best balance
-
Compression Ratio Examples (approximate):
- LZ4: 2-3x compression
- Gzip: 3-5x compression
- Zstandard: 3-6x compression
- Bzip2: 4-6x compression
- XZ: 5-8x compression
Common Errors and Solutions
Error: "Source path cannot be empty"
Cause: The Source Path input is not provided or is empty.
Solution: Ensure you provide a valid file or directory path.
Error: "Source path does not exist"
Cause: The specified source path does not exist on the file system.
Solution:
- Verify the source path is correct
- Check that the file or directory exists
- Ensure proper path formatting (use forward slashes or proper escaping)
Error: "Target path cannot be empty"
Cause: The Target Path input is not provided or is empty.
Solution: Provide a valid destination path with an appropriate file extension.
Error: Unsupported format
Cause: The target file extension is not recognized as a supported compression format.
Solution: Use one of the supported file extensions (.zip, .gz, .bz2, .zst, .xz, .lz4, .br).
Error: Permission denied
Cause: Insufficient permissions to read the source or write to the target location.
Solution:
- Ensure read permissions for the source file/directory
- Verify write permissions for the target directory
- Check file ownership and permissions
Error: Disk space insufficient
Cause: Not enough disk space to create the compressed file.
Solution:
- Free up disk space on the target drive
- Choose a different target location with more available space
- Use a higher compression format to reduce output size
Related Nodes
- Decompress - Decompress a compressed file
- Archive - Create archives with advanced options
- Unarchive - Extract files from archives
- Inspect Archive File - List contents of compressed archives