2023-11-06 18:14:21 +01:00
|
|
|
import * as plugins from './plugins.js';
|
2026-01-01 23:09:06 +00:00
|
|
|
import type { IArchiveEntry, TCompressionLevel } from '../ts_shared/interfaces.js';
|
|
|
|
|
import { TarTools as SharedTarTools } from '../ts_shared/classes.tartools.js';
|
|
|
|
|
import { GzipTools } from '../ts_shared/classes.gziptools.js';
|
2023-11-06 18:14:21 +01:00
|
|
|
|
2025-11-25 12:32:13 +00:00
|
|
|
/**
|
2026-01-01 23:09:06 +00:00
|
|
|
* Extended TAR archive utilities with Node.js filesystem support
|
2025-11-25 12:32:13 +00:00
|
|
|
*/
|
2026-01-01 23:09:06 +00:00
|
|
|
export class TarTools extends SharedTarTools {
|
2025-11-25 12:32:13 +00:00
|
|
|
/**
|
2026-01-01 23:09:06 +00:00
|
|
|
* Pack a directory into a TAR buffer (Node.js only)
|
2025-11-25 12:32:13 +00:00
|
|
|
*/
|
2026-01-01 23:09:06 +00:00
|
|
|
public async packDirectory(directoryPath: string): Promise<Uint8Array> {
|
2025-11-25 11:59:11 +00:00
|
|
|
const fileTree = await plugins.listFileTree(directoryPath, '**/*');
|
2026-01-01 23:09:06 +00:00
|
|
|
const entries: IArchiveEntry[] = [];
|
2025-11-25 12:32:13 +00:00
|
|
|
|
2024-06-08 10:30:03 +02:00
|
|
|
for (const filePath of fileTree) {
|
|
|
|
|
const absolutePath = plugins.path.join(directoryPath, filePath);
|
2026-01-01 23:09:06 +00:00
|
|
|
const content = await plugins.fsPromises.readFile(absolutePath);
|
|
|
|
|
entries.push({
|
|
|
|
|
archivePath: filePath,
|
|
|
|
|
content: new Uint8Array(content),
|
2024-06-08 10:30:03 +02:00
|
|
|
});
|
|
|
|
|
}
|
2025-11-25 12:32:13 +00:00
|
|
|
|
2026-01-01 23:09:06 +00:00
|
|
|
return this.packFiles(entries);
|
2024-06-08 10:30:03 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-25 12:32:13 +00:00
|
|
|
/**
|
2026-01-01 23:09:06 +00:00
|
|
|
* Pack a directory into a TAR.GZ buffer (Node.js only)
|
2025-11-25 12:32:13 +00:00
|
|
|
*/
|
|
|
|
|
public async packDirectoryToTarGz(
|
|
|
|
|
directoryPath: string,
|
|
|
|
|
compressionLevel?: TCompressionLevel
|
2026-01-01 23:09:06 +00:00
|
|
|
): Promise<Uint8Array> {
|
|
|
|
|
const tarBuffer = await this.packDirectory(directoryPath);
|
2025-11-25 12:32:13 +00:00
|
|
|
const gzipTools = new GzipTools();
|
2026-01-01 23:09:06 +00:00
|
|
|
return gzipTools.compress(tarBuffer, compressionLevel);
|
2025-11-25 12:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-01 23:09:06 +00:00
|
|
|
* Pack a directory into a TAR.GZ stream (Node.js only)
|
2025-11-25 12:32:13 +00:00
|
|
|
*/
|
|
|
|
|
public async packDirectoryToTarGzStream(
|
|
|
|
|
directoryPath: string,
|
|
|
|
|
compressionLevel?: TCompressionLevel
|
|
|
|
|
): Promise<plugins.stream.Readable> {
|
2026-01-01 23:09:06 +00:00
|
|
|
const buffer = await this.packDirectoryToTarGz(directoryPath, compressionLevel);
|
|
|
|
|
return plugins.stream.Readable.from(buffer);
|
2025-11-25 12:32:13 +00:00
|
|
|
}
|
2023-11-06 18:14:21 +01:00
|
|
|
}
|