Files
smartarchive/ts/classes.tartools.ts

52 lines
1.7 KiB
TypeScript

import * as plugins from './plugins.js';
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';
/**
* Extended TAR archive utilities with Node.js filesystem support
*/
export class TarTools extends SharedTarTools {
/**
* Pack a directory into a TAR buffer (Node.js only)
*/
public async packDirectory(directoryPath: string): Promise<Uint8Array> {
const fileTree = await plugins.listFileTree(directoryPath, '**/*');
const entries: IArchiveEntry[] = [];
for (const filePath of fileTree) {
const absolutePath = plugins.path.join(directoryPath, filePath);
const content = await plugins.fsPromises.readFile(absolutePath);
entries.push({
archivePath: filePath,
content: new Uint8Array(content),
});
}
return this.packFiles(entries);
}
/**
* Pack a directory into a TAR.GZ buffer (Node.js only)
*/
public async packDirectoryToTarGz(
directoryPath: string,
compressionLevel?: TCompressionLevel
): Promise<Uint8Array> {
const tarBuffer = await this.packDirectory(directoryPath);
const gzipTools = new GzipTools();
return gzipTools.compress(tarBuffer, compressionLevel);
}
/**
* Pack a directory into a TAR.GZ stream (Node.js only)
*/
public async packDirectoryToTarGzStream(
directoryPath: string,
compressionLevel?: TCompressionLevel
): Promise<plugins.stream.Readable> {
const buffer = await this.packDirectoryToTarGz(directoryPath, compressionLevel);
return plugins.stream.Readable.from(buffer);
}
}