-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolder.ts
More file actions
58 lines (55 loc) · 1.82 KB
/
Copy pathfolder.ts
File metadata and controls
58 lines (55 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @module api/folder
*
* High-level API for folder compression and decompression.
*
* ## In-memory path (`compressFolder`)
*
* Walks the tree, builds a binary archive in RAM, compresses, and encodes.
* Suitable for small-to-medium folders.
*
* ## Decompression (`decompressToPath`)
*
* Decodes, decompresses, checks the folder tag, and unpacks to disk.
*/
import { collectEntries } from "../archive/collect.js"
import { serializeArchive } from "../archive/format.js"
import { unpackDirectory } from "../archive/unpack.js"
import { compressTaggedPayload, decompressPayload, TAG_FOLDER } from "../payload/tags.js"
import type { Encoding } from "../types.js"
/**
* Pack a directory tree into a single encoded string (in-memory).
*
* @returns Encoded blob plus statistics about the source folder.
*/
export function compressFolder(dirPath: string, encoding: Encoding = 64, password?: string) {
const entries = collectEntries(dirPath)
const archive = serializeArchive(entries)
const encoded = compressTaggedPayload(TAG_FOLDER, archive, encoding, password)
const files = entries.filter((e) => e.type === "f")
const originalBytes = files.reduce((sum, e) => sum + (e.content?.length ?? 0), 0)
return {
encoded,
fileCount: files.length,
dirCount: entries.length - files.length,
originalBytes,
archiveBytes: archive.length,
}
}
/**
* Decode and unpack a folder archive to a destination directory.
*
* @throws If the payload is text, not a folder archive.
*/
export function decompressToPath(
encoded: string,
destDir: string,
encoding: Encoding = 64,
password?: string,
) {
const { tag, data } = decompressPayload(encoded, encoding, password)
if (tag !== TAG_FOLDER) {
throw new Error("This payload is compressed text, not a folder. Use decompress.")
}
return unpackDirectory(data, destDir)
}