Extract File
Extracts a specific file from an archive to the target path without extracting the entire archive.
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 archive file containing the file to extract. Supports Zip, Tar, TarGz, and other archive formats.
-
File - Name of the file to extract from the archive, including its extension. This should be the exact filename as it appears in the archive (case-sensitive on some systems).
-
Target Path - Destination path where the extracted file will be saved. This should be the complete path including the filename.
How It Works
The Extract File node performs selective extraction:
- Opens the archive file specified in Source Path
- Searches for the exact filename specified in the File input
- Extracts only that specific file
- Saves it to the Target Path location
- Leaves the archive file unchanged
This is much more efficient than extracting the entire archive when you only need one or a few specific files.
Examples
Example 1: Extract a Configuration File from Zip
Extract a specific configuration file from a Zip archive:
// Define paths
msg.archiveFile = "/home/user/backups/project-backup.zip";
msg.configFile = "config/settings.json";
msg.outputPath = "/home/user/restore/settings.json";
Configure the Extract File node:
- Source Path:
{{msg.archiveFile}} - File:
{{msg.configFile}} - Target Path:
{{msg.outputPath}}
Example 2: Extract from TarGz Archive
Extract a specific document from a compressed tar archive:
// Extract a document from tar.gz
msg.backupArchive = "/home/user/backups/documents.tar.gz";
msg.fileName = "reports/monthly-report.pdf";
msg.extractedFile = "/home/user/reports/current-report.pdf";
Configure the Extract File node:
- Source Path:
{{msg.backupArchive}} - File:
{{msg.fileName}} - Target Path:
{{msg.extractedFile}}
Example 3: Extract Log File from Archive
Extract a specific log file for analysis:
// Extract a specific log file
msg.logArchive = "/home/user/archives/logs-2024-01.zip";
msg.logFileName = "application.log";
msg.extractPath = "/home/user/analysis/application.log";
Configure the Extract File node:
- Source Path:
{{msg.logArchive}} - File:
{{msg.logFileName}} - Target Path:
{{msg.extractPath}}
Example 4: Extract with Dynamic Filename
Extract files based on date or pattern:
// Get today's date for filename
const today = new Date();
const dateStr = today.toISOString().split('T')[0]; // YYYY-MM-DD
msg.archivePath = "/home/user/backups/daily-reports.zip";
msg.targetFile = `report-${dateStr}.xlsx`;
msg.outputPath = `/home/user/reports/today-report.xlsx`;
Configure the Extract File node:
- Source Path:
{{msg.archivePath}} - File:
{{msg.targetFile}} - Target Path:
{{msg.outputPath}}
Example 5: Extract Multiple Specific Files
Extract several specific files from the same archive:
// Define archive and files to extract
msg.archive = "/home/user/backups/project.zip";
msg.filesToExtract = [
{ file: "src/config.js", output: "/home/user/project/config.js" },
{ file: "src/database.js", output: "/home/user/project/database.js" },
{ file: "package.json", output: "/home/user/project/package.json" }
];
// Start with first file
msg.currentIndex = 0;
msg.currentFile = msg.filesToExtract[msg.currentIndex];
Configure the Extract File node:
- Source Path:
{{msg.archive}} - File:
{{msg.currentFile.file}} - Target Path:
{{msg.currentFile.output}}
Use a Loop to iterate through all files in the array.
Example 6: Extract File from Nested Path
Extract a file from a nested directory structure within the archive:
// Extract from nested path
msg.archiveFile = "/home/user/releases/app-v2.0.zip";
msg.nestedFile = "app/resources/images/logo.png";
msg.outputFile = "/home/user/assets/logo.png";
Configure the Extract File node:
- Source Path:
{{msg.archiveFile}} - File:
{{msg.nestedFile}} - Target Path:
{{msg.outputFile}}
Example 7: Extract with Validation
Extract a file and validate it exists before using:
// Set extraction parameters
msg.archivePath = "/home/user/data/archive.zip";
msg.dataFile = "data/export.csv";
msg.extractedPath = "/home/user/processing/export.csv";
Configure the Extract File node:
- Source Path:
{{msg.archivePath}} - File:
{{msg.dataFile}} - Target Path:
{{msg.extractedPath}}
Add a Function node after extraction:
const fs = require('fs');
// Verify file was extracted
if (fs.existsSync(msg.extractedPath)) {
const stats = fs.statSync(msg.extractedPath);
msg.extractSuccess = true;
msg.extractedSize = stats.size;
msg.message = `Successfully extracted ${msg.dataFile} (${stats.size} bytes)`;
} else {
msg.extractSuccess = false;
msg.message = `Failed to extract ${msg.dataFile}`;
}
Usage Tips
-
File Path Format:
- Use the exact path as it appears in the archive
- Include directory separators (/) for files in subdirectories
- Path is case-sensitive on Unix/Linux systems
- Use forward slashes (/) even on Windows
-
Finding File Names:
- Use the Inspect Archive File node first to see all available files
- This ensures you use the correct filename and path
-
Target Path:
- Specify the complete path including the filename
- The directory must exist or be created beforehand
- You can rename the file during extraction by using a different filename in the target path
-
Performance Benefits:
- Much faster than extracting the entire archive
- Saves disk space by only extracting needed files
- Useful for large archives when you only need specific files
-
Multiple Extractions:
- You can extract multiple files from the same archive
- Each extraction requires a separate node or loop iteration
- The archive remains intact and can be read multiple times
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 path to the archive file.
Error: "Source path does not exist"
Cause: The specified archive file does not exist at the given path.
Solution:
- Verify the archive path is correct
- Check that the archive file exists and hasn't been moved or deleted
- Ensure proper path formatting
Error: "File name cannot be empty"
Cause: The File input is not provided or is empty.
Solution: Provide the name of the file to extract from the archive.
Error: "Target path cannot be empty"
Cause: The Target Path input is not provided or is empty.
Solution: Provide a valid destination path for the extracted file.
Error: File not found in archive
Cause: The specified file does not exist in the archive.
Solution:
- Use the Inspect Archive File node to list all files in the archive
- Verify the filename is correct (including case sensitivity)
- Check if the file includes a directory path (e.g., "folder/file.txt")
- Ensure there are no extra spaces or special characters in the filename
Error: Permission denied
Cause: Insufficient permissions to read the archive or write to the target location.
Solution:
- Ensure read permissions for the archive file
- Verify write permissions for the target directory
- Check file ownership and permissions
Error: Target directory does not exist
Cause: The directory portion of the target path doesn't exist.
Solution:
- Create the target directory before extraction
- Use a File System node to create the directory structure
- Or ensure the parent directories exist
Error: Corrupted archive
Cause: The archive file is corrupted or invalid.
Solution:
- Verify the archive file is valid by opening it with a standard tool
- Re-download or re-create the archive if it's corrupted
- Check if the archive was completely downloaded
Related Nodes
- Inspect Archive File - List all files in an archive to find the exact filename
- Unarchive - Extract all files from an archive
- Archive - Create archives with multiple files
- Decompress - Decompress single compressed files