-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·61 lines (46 loc) · 1.76 KB
/
index.js
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
59
60
61
#!/usr/bin/env node
//This script is used to parse a tarball and output a gzipped JSON file with the index
const process = require("process");
const fs = require("fs");
const tar = require("tar");
const { Readable } = require('stream');
const { createGzip } = require('zlib');
const tarballPath = process.argv[2];
const outputPath = process.argv[3];
let totalOffset = 0;
let result = {};
let id = 0;
async function compressAndWriteToFile(data, filename) {
// Create a readable stream from the string
const readable = Readable.from(data);
// Create a writable stream to hold the compressed data
const writable = fs.createWriteStream(filename);
// Create a compression transform stream with the gzip algorithm
const compressionStream = createGzip();
// Pipe the data through the compression stream and into the writable stream
await readable.pipe(compressionStream).pipe(writable);
console.log(`Compressed data written to ${filename}`);
}
const parser = new tar.Parse();
parser.on("entry", (entry) => {
const headerSize = 512; // Tar header size is usually 512 bytes
const BLOCK_SIZE = 512; // for MacOS
const fileSize = entry.type === "File" ? entry.size : 0;
const s = fileSize > 0 ? totalOffset + headerSize : null;
const e = fileSize > 0 ? s + fileSize - 1 : null;
const padding =
fileSize % BLOCK_SIZE ? BLOCK_SIZE - (fileSize % BLOCK_SIZE) : 0;
const fileInfo = {
s,
e,
};
result[entry.path] = fileInfo;
totalOffset += fileSize + headerSize + padding;
id += 1;
entry.resume(); // Discard the file content and move to the next entry
});
parser.on("end", () => {
let content = JSON.stringify(result, null, 2);
let compressedContent = compressAndWriteToFile(content, outputPath)
});
fs.createReadStream(tarballPath).pipe(parser);