initial commit
This commit is contained in:
240
example.js
Normal file
240
example.js
Normal file
@@ -0,0 +1,240 @@
|
||||
const { PakUnpacker } = require('./PakUnpacker');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* Example 1: Extract all files from a PAK archive
|
||||
*/
|
||||
async function extractAllFiles() {
|
||||
const pakFile = path.join(__dirname, 'data.pak'); // Adjust path as needed
|
||||
const outputDir = path.join(__dirname, '../output');
|
||||
|
||||
try {
|
||||
console.log('Loading PAK file...');
|
||||
const unpacker = new PakUnpacker(pakFile);
|
||||
await unpacker.load();
|
||||
|
||||
console.log(`Found ${unpacker.entries.length} files in the archive.`);
|
||||
|
||||
console.log('\nExtracting files...');
|
||||
await unpacker.extract(outputDir);
|
||||
|
||||
console.log('\nExtraction complete!');
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example 2: List all files without extracting
|
||||
*/
|
||||
async function listFiles() {
|
||||
const pakFile = path.join(__dirname, '../test.pak');
|
||||
|
||||
try {
|
||||
const unpacker = new PakUnpacker(pakFile);
|
||||
await unpacker.load();
|
||||
|
||||
const files = unpacker.listFiles();
|
||||
|
||||
console.log(`\nFiles in archive (${files.length} total):\n`);
|
||||
files.forEach(file => {
|
||||
const compressed = file.compressed ? '[Zlib]' : '[None]';
|
||||
console.log(`${compressed} ${file.name} (${file.size} -> ${file.originalSize} bytes)`);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example 3: Extract a specific file
|
||||
*/
|
||||
async function extractSingleFile() {
|
||||
const pakFile = path.join(__dirname, 'data.pak');
|
||||
const fileName = 'config.json'; // Change to your target file
|
||||
const outputPath = path.join(__dirname, '../output', fileName);
|
||||
|
||||
try {
|
||||
const unpacker = new PakUnpacker(pakFile);
|
||||
await unpacker.load();
|
||||
|
||||
// Method 1: Extract file directly to disk
|
||||
const success = await unpacker.extractFile(fileName, outputPath);
|
||||
|
||||
if (success) {
|
||||
console.log(`\nSuccessfully extracted ${fileName}`);
|
||||
} else {
|
||||
console.log(`File not found: ${fileName}`);
|
||||
}
|
||||
|
||||
// Method 2: Get file data in memory (without saving)
|
||||
// const fileData = await unpacker.getFile(fileName);
|
||||
// if (fileData) {
|
||||
// console.log(`\nFound file: ${fileName}`);
|
||||
// console.log(`Size: ${fileData.length} bytes`);
|
||||
// // If it's a text file, you can print its content
|
||||
// console.log('\nContent:');
|
||||
// console.log(fileData.toString('utf8'));
|
||||
// }
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example 4: Use with Express.js API
|
||||
*/
|
||||
function expressApiExample() {
|
||||
// This is just a code example, not executable
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
app.get('/api/pak/list/:pakName', async (req, res) => {
|
||||
try {
|
||||
const pakFile = path.join(__dirname, '../paks', req.params.pakName + '.pak');
|
||||
const unpacker = new PakUnpacker(pakFile);
|
||||
await unpacker.load();
|
||||
|
||||
const files = unpacker.listFiles();
|
||||
res.json({ success: true, files });
|
||||
} catch (error) {
|
||||
res.status(500).json({ success: false, error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/pak/extract/:pakName/:fileName(*)', async (req, res) => {
|
||||
try {
|
||||
const pakFile = path.join(__dirname, '../paks', req.params.pakName + '.pak');
|
||||
const unpacker = new PakUnpacker(pakFile);
|
||||
await unpacker.load();
|
||||
|
||||
const fileData = await unpacker.getFile(req.params.fileName);
|
||||
|
||||
if (fileData) {
|
||||
res.setHeader('Content-Type', 'application/octet-stream');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="${path.basename(req.params.fileName)}"`);
|
||||
res.send(fileData);
|
||||
} else {
|
||||
res.status(404).json({ success: false, error: 'File not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ success: false, error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(3000, () => {
|
||||
console.log('API server running on port 3000');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Example 5: Extract files by pattern (e.g., all *.conf files)
|
||||
*/
|
||||
async function extractByPattern() {
|
||||
const pakFile = path.join(__dirname, 'data.pak');
|
||||
const pattern = '*.conf'; // You can use: "*.conf", "*.json", "config/*.xml", "**/*.txt"
|
||||
const outputDir = path.join(__dirname, '../output');
|
||||
|
||||
try {
|
||||
console.log('Loading PAK file...');
|
||||
const unpacker = new PakUnpacker(pakFile);
|
||||
await unpacker.load();
|
||||
|
||||
console.log(`\nSearching for files matching: ${pattern}`);
|
||||
const extractedFiles = await unpacker.extractByPattern(pattern, outputDir);
|
||||
|
||||
if (extractedFiles.length > 0) {
|
||||
console.log(`\nSuccessfully extracted ${extractedFiles.length} file(s):`);
|
||||
extractedFiles.forEach(file => console.log(` - ${file}`));
|
||||
} else {
|
||||
console.log('\nNo files matched the pattern.');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example 6: Synchronous version (blocking)
|
||||
*/
|
||||
function syncExample() {
|
||||
const pakFile = path.join(__dirname, '../test.pak');
|
||||
const outputDir = path.join(__dirname, '../output-sync');
|
||||
|
||||
try {
|
||||
console.log('Loading PAK file (sync)...');
|
||||
const unpacker = new PakUnpacker(pakFile);
|
||||
unpacker.loadSync();
|
||||
|
||||
console.log(`Found ${unpacker.entries.length} files.`);
|
||||
|
||||
console.log('\nExtracting files (sync)...');
|
||||
unpacker.extractSync(outputDir);
|
||||
|
||||
console.log('\nExtraction complete!');
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function extractConfFiles() {
|
||||
const fs = require('fs');
|
||||
const pakDir = path.join(__dirname, 'data'); // Ordner mit den PAK-Dateien
|
||||
const outputDir = './output';
|
||||
const filePattern = '**/*.c'; // Pattern für die zu extrahierenden Dateien
|
||||
|
||||
try {
|
||||
// Alle .pak Dateien im Ordner finden
|
||||
const files = fs.readdirSync(pakDir).filter(file => file.endsWith('.pak'));
|
||||
|
||||
if (files.length === 0) {
|
||||
console.log('Keine .pak Dateien gefunden im Ordner:', pakDir);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Gefunden: ${files.length} PAK-Datei(en)\n`);
|
||||
|
||||
let totalExtracted = 0;
|
||||
|
||||
// Jede PAK-Datei nacheinander verarbeiten
|
||||
for (const pakFileName of files) {
|
||||
const pakFile = path.join(pakDir, pakFileName);
|
||||
console.log(`Verarbeite: ${pakFileName}`);
|
||||
|
||||
const unpacker = new PakUnpacker(pakFile);
|
||||
await unpacker.load();
|
||||
|
||||
const extractedFiles = await unpacker.extractByPattern(filePattern, outputDir);
|
||||
console.log(` → ${extractedFiles.length} Datei(en) extrahiert`);
|
||||
|
||||
totalExtracted += extractedFiles.length;
|
||||
}
|
||||
|
||||
console.log(`\nGesamt: ${totalExtracted} Datei(en) aus ${files.length} PAK-Archiv(en) extrahiert`);
|
||||
} catch (error) {
|
||||
console.error('Fehler:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Run examples
|
||||
if (require.main === module) {
|
||||
console.log('=== PAK Unpacker Examples ===\n');
|
||||
|
||||
// Uncomment the example you want to run:
|
||||
//extractAllFiles();
|
||||
//listFiles();
|
||||
//extractSingleFile();
|
||||
|
||||
extractConfFiles();
|
||||
//syncExample();
|
||||
|
||||
console.log('Uncomment an example function to run it.');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
extractAllFiles,
|
||||
listFiles,
|
||||
extractSingleFile,
|
||||
extractByPattern,
|
||||
syncExample
|
||||
};
|
||||
Reference in New Issue
Block a user