smartarchive/readme.md

283 lines
9.6 KiB
Markdown
Raw Permalink Normal View History

2023-07-26 14:13:33 +00:00
# @push.rocks/smartarchive
2024-06-08 10:49:02 +00:00
`@push.rocks/smartarchive` is a powerful library designed for managing archive files. It provides utilities for compressing and decompressing data in various formats such as zip, tar, gzip, and bzip2. This library aims to simplify the process of handling archive files, making it an ideal choice for projects that require manipulation of archived data.
2020-03-13 20:22:56 +00:00
2024-04-14 15:20:20 +00:00
## Install
2024-06-08 10:49:02 +00:00
To install `@push.rocks/smartarchive`, you can either use npm or yarn. Run one of the following commands in your project directory:
2024-04-14 15:20:20 +00:00
```shell
npm install @push.rocks/smartarchive --save
```
or if you prefer yarn:
```shell
yarn add @push.rocks/smartarchive
```
This will add `@push.rocks/smartarchive` to your project's dependencies.
2020-03-13 20:22:56 +00:00
## Usage
2024-06-08 10:49:02 +00:00
`@push.rocks/smartarchive` provides an easy-to-use API for extracting, creating, and analyzing archive files. Below, we'll cover how to get started and explore various features of the module.
2020-03-13 20:22:56 +00:00
2024-06-08 10:49:02 +00:00
### Importing SmartArchive
2024-04-14 15:20:20 +00:00
First, import `SmartArchive` from `@push.rocks/smartarchive` using ESM syntax:
2020-03-13 20:22:56 +00:00
2024-04-14 15:20:20 +00:00
```typescript
import { SmartArchive } from '@push.rocks/smartarchive';
2020-03-13 20:22:56 +00:00
```
2024-04-14 15:20:20 +00:00
### Extracting Archive Files
2024-06-08 10:49:02 +00:00
You can extract archive files from different sources using `SmartArchive.fromArchiveUrl`, `SmartArchive.fromArchiveFile`, and `SmartArchive.fromArchiveStream`. Here's an example of extracting an archive from a URL:
2024-04-14 15:20:20 +00:00
```typescript
2024-06-08 10:49:02 +00:00
import { SmartArchive } from '@push.rocks/smartarchive';
2024-04-14 15:20:20 +00:00
async function extractArchiveFromURL() {
const url = 'https://example.com/archive.zip';
const targetDir = '/path/to/extract';
const archive = await SmartArchive.fromArchiveUrl(url);
await archive.exportToFs(targetDir);
console.log('Archive extracted successfully.');
}
2024-06-08 10:49:02 +00:00
extractArchiveFromURL();
2024-04-14 15:20:20 +00:00
```
2024-06-08 10:49:02 +00:00
### Extracting an Archive from a File
Similarly, you can extract an archive from a local file:
```typescript
import { SmartArchive } from '@push.rocks/smartarchive';
async function extractArchiveFromFile() {
const filePath = '/path/to/archive.zip';
const targetDir = '/path/to/extract';
const archive = await SmartArchive.fromArchiveFile(filePath);
await archive.exportToFs(targetDir);
console.log('Archive extracted successfully.');
}
extractArchiveFromFile();
```
### Stream-Based Extraction
For larger files, you might prefer a streaming approach to prevent high memory consumption. Heres an example:
```typescript
import { SmartArchive } from '@push.rocks/smartarchive';
import { createReadStream } from 'fs';
async function extractArchiveUsingStream() {
const archiveStream = createReadStream('/path/to/archive.zip');
const archive = await SmartArchive.fromArchiveStream(archiveStream);
const extractionStream = await archive.exportToStreamOfStreamFiles();
extractionStream.pipe(createWriteStream('/path/to/destination'));
}
2024-04-14 15:20:20 +00:00
2024-06-08 10:49:02 +00:00
extractArchiveUsingStream();
```
2024-04-14 15:20:20 +00:00
### Analyzing Archive Files
2024-06-08 10:49:02 +00:00
Sometimes, you may need to inspect the contents of an archive before extracting it. The following example shows how to analyze an archive:
2024-04-14 15:20:20 +00:00
```typescript
2024-06-08 10:49:02 +00:00
import { SmartArchive } from '@push.rocks/smartarchive';
2024-04-14 15:20:20 +00:00
async function analyzeArchive() {
2024-06-08 10:49:02 +00:00
const filePath = '/path/to/archive.zip';
2024-04-14 15:20:20 +00:00
2024-06-08 10:49:02 +00:00
const archive = await SmartArchive.fromArchiveFile(filePath);
2024-04-14 15:20:20 +00:00
const analysisResult = await archive.analyzeContent();
console.log(analysisResult); // Outputs details about the archive content
}
2024-06-08 10:49:02 +00:00
analyzeArchive();
2024-04-14 15:20:20 +00:00
```
2024-06-08 10:49:02 +00:00
### Creating Archive Files
Creating an archive file is straightforward. Here we demonstrate creating a tar.gz archive:
```typescript
import { SmartArchive } from '@push.rocks/smartarchive';
async function createTarGzArchive() {
const archive = new SmartArchive();
// Add directories and files
archive.addedDirectories.push('/path/to/directory1');
archive.addedFiles.push('/path/to/file1.txt');
// Export as tar.gz
const tarGzStream = await archive.exportToTarGzStream();
// Save to filesystem or handle as needed
tarGzStream.pipe(createWriteStream('/path/to/destination.tar.gz'));
}
createTarGzArchive();
```
2024-04-14 15:20:20 +00:00
### Stream Operations
2024-06-08 10:49:02 +00:00
Here's an example of using `smartarchive`'s streaming capabilities:
2024-04-14 15:20:20 +00:00
```typescript
import { createReadStream, createWriteStream } from 'fs';
2024-06-08 10:49:02 +00:00
import { SmartArchive } from '@push.rocks/smartarchive';
2024-04-14 15:20:20 +00:00
2024-06-08 10:49:02 +00:00
async function extractArchiveUsingStreams() {
2024-04-14 15:20:20 +00:00
const archiveStream = createReadStream('/path/to/archive.zip');
const archive = await SmartArchive.fromArchiveStream(archiveStream);
const extractionStream = await archive.exportToStreamOfStreamFiles();
2024-06-08 10:49:02 +00:00
extractionStream.pipe(createWriteStream('/path/to/extracted'));
}
extractArchiveUsingStreams();
```
### Advanced Decompression Usage
`smartarchive` supports multiple compression formats. It also provides detailed control over the decompression processes:
- For ZIP files, `ZipTools` handles decompression using the `fflate` library.
- For TAR files, `TarTools` uses `tar-stream`.
- For GZIP files, `GzipTools` provides a `CompressGunzipTransform` and `DecompressGunzipTransform`.
- For BZIP2 files, `Bzip2Tools` utilizes custom streaming decompression.
Example: Working with a GZIP-compressed archive:
```typescript
import { createReadStream, createWriteStream } from 'fs';
import { SmartArchive } from '@push.rocks/smartarchive';
async function decompressGzipArchive() {
const filePath = '/path/to/archive.gz';
const targetDir = '/path/to/extract';
const archive = await SmartArchive.fromArchiveFile(filePath);
await archive.exportToFs(targetDir);
console.log('GZIP archive decompressed successfully.');
}
decompressGzipArchive();
```
### Advancing with Custom Decompression Streams
You can inject custom decompression streams where needed:
```typescript
import { createReadStream, createWriteStream } from 'fs';
import { SmartArchive, GzipTools } from '@push.rocks/smartarchive';
async function customDecompression() {
const filePath = '/path/to/archive.gz';
const targetDir = '/path/to/extract';
const archive = await SmartArchive.fromArchiveFile(filePath);
const gzipTools = new GzipTools();
const decompressionStream = gzipTools.getDecompressionStream();
const archiveStream = await archive.getArchiveStream();
archiveStream.pipe(decompressionStream).pipe(createWriteStream(targetDir));
console.log('Custom GZIP decompression successful.');
2024-04-14 15:20:20 +00:00
}
2024-06-08 10:49:02 +00:00
customDecompression();
2024-04-14 15:20:20 +00:00
```
2024-06-08 10:49:02 +00:00
### Custom Pack and Unpack Tar
When dealing with tar archives, you may need to perform custom packing and unpacking:
```typescript
import { SmartArchive, TarTools } from '@push.rocks/smartarchive';
import { createWriteStream } from 'fs';
async function customTarOperations() {
const tarTools = new TarTools();
// Packing a directory into a tar stream
const packStream = await tarTools.packDirectory('/path/to/directory');
packStream.pipe(createWriteStream('/path/to/archive.tar'));
// Extracting files from a tar stream
const extractStream = tarTools.getDecompressionStream();
createReadStream('/path/to/archive.tar').pipe(extractStream).on('entry', (header, stream, next) => {
const writeStream = createWriteStream(`/path/to/extract/${header.name}`);
stream.pipe(writeStream);
stream.on('end', next);
});
}
customTarOperations();
```
### Extract and Analyze All-in-One
To extract and simultaneously analyze archive content:
```typescript
import { createReadStream, createWriteStream } from 'fs';
import { SmartArchive } from '@push.rocks/smartarchive';
async function extractAndAnalyze() {
const filePath = '/path/to/archive.zip';
const targetDir = '/path/to/extract';
const archive = await SmartArchive.fromArchiveFile(filePath);
const analyzedStream = archive.archiveAnalyzer.getAnalyzedStream();
const extractionStream = await archive.exportToStreamOfStreamFiles();
analyzedStream.pipe(extractionStream).pipe(createWriteStream(targetDir));
analyzedStream.on('data', (chunk) => {
console.log(JSON.stringify(chunk, null, 2));
});
}
extractAndAnalyze();
```
2024-04-14 15:20:20 +00:00
2024-06-08 10:49:02 +00:00
### Final Words
2024-04-14 15:20:20 +00:00
2024-06-08 10:49:02 +00:00
These examples demonstrate various use cases for `@push.rocks/smartarchive`. Depending on your specific project requirements, you can adapt these examples to suit your needs. Always refer to the latest documentation for the most current information and methods available in `@push.rocks/smartarchive`.
2024-04-14 15:20:20 +00:00
2024-06-08 10:49:02 +00:00
For more information and API references, check the official [`@push.rocks/smartarchive` GitHub repository](https://code.foss.global/push.rocks/smartarchive).
2024-04-14 15:20:20 +00:00
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
2020-03-13 20:22:56 +00:00
2024-04-14 15:20:20 +00:00
### Trademarks
2020-03-13 20:22:56 +00:00
2024-04-14 15:20:20 +00:00
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
2020-03-13 20:22:56 +00:00
2024-04-14 15:20:20 +00:00
### Company Information
2020-03-13 20:22:56 +00:00
2024-04-14 15:20:20 +00:00
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2020-03-13 20:22:56 +00:00
2024-04-14 15:20:20 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2020-03-13 20:22:56 +00:00
2024-04-14 15:20:20 +00:00
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.