Skip to content

Commit 7dca35b

Browse files
Add Archive-Conversions functions
1 parent 5f71f13 commit 7dca35b

File tree

1 file changed

+314
-0
lines changed

1 file changed

+314
-0
lines changed
Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
import { spawn } from 'node:child_process';
2+
import { lstat, mkdtemp } from 'node:fs/promises';
3+
import path from 'node:path';
4+
5+
/**
6+
* Create a tar archive from a directory.
7+
*/
8+
export async function createTar(sourceFile: string, destinationFile: string): Promise<void> {
9+
return new Promise(async (resolve, reject) => {
10+
11+
try {
12+
const stat = await lstat(sourceFile);
13+
if (!stat.isDirectory) {
14+
reject(new Error(`Directory '${sourceFile}' does not exist`));
15+
}
16+
} catch (e) {
17+
reject(e);
18+
}
19+
20+
const cwd = path.dirname(sourceFile);
21+
destinationFile = path.resolve(path.normalize(destinationFile));
22+
23+
const proc = spawn(
24+
'tar',
25+
['-cvf', destinationFile, sourceFile]
26+
);
27+
28+
proc.on('exit', (code) => {
29+
if (code !== 0) {
30+
reject(new Error(`Failed to create tar archive. Exit code: ${code}`));
31+
}
32+
resolve(undefined);
33+
});
34+
});
35+
}
36+
37+
/**
38+
* Extract the contents of a tar archive.
39+
*/
40+
export async function extractTar(sourceFile: string): Promise<void> {
41+
return new Promise(async (resolve, reject) => {
42+
43+
try {
44+
const stat = await lstat(sourceFile);
45+
if (!stat.isDirectory) {
46+
reject(new Error(`Directory '${sourceFile}' does not exist`));
47+
}
48+
} catch (e) {
49+
reject(e);
50+
}
51+
52+
const proc = spawn(
53+
'tar',
54+
['-xvf', sourceFile]
55+
);
56+
57+
proc.on('exit', (code) => {
58+
if (code !== 0) {
59+
reject(new Error(`Failed to extract tar archive. Exit code: ${code}`));
60+
}
61+
resolve(undefined);
62+
});
63+
});
64+
}
65+
66+
/**
67+
* Create a gzip archive from a directory.
68+
*/
69+
export async function createGzip(sourceFile: string, destinationFile: string): Promise<void> {
70+
return new Promise(async (resolve, reject) => {
71+
72+
try {
73+
const stat = await lstat(sourceFile);
74+
if (!stat.isDirectory) {
75+
reject(new Error(`Directory '${sourceFile}' does not exist`));
76+
}
77+
} catch (e) {
78+
reject(e);
79+
}
80+
81+
const cwd = path.dirname(sourceFile);
82+
destinationFile = path.resolve(path.normalize(destinationFile));
83+
84+
const proc = spawn(
85+
'tar',
86+
['-czvf', destinationFile, sourceFile]
87+
);
88+
89+
proc.on('exit', (code) => {
90+
if (code !== 0) {
91+
reject(new Error(`Failed to create gzip archive. Exit code: ${code}`));
92+
}
93+
resolve(undefined);
94+
});
95+
});
96+
}
97+
98+
/**
99+
* Extract the contents of a gzip archive.
100+
*/
101+
export async function extractGzip(sourceFile: string): Promise<void> {
102+
return new Promise(async (resolve, reject) => {
103+
104+
try {
105+
const stat = await lstat(sourceFile);
106+
if (!stat.isDirectory) {
107+
reject(new Error(`Directory '${sourceFile}' does not exist`));
108+
}
109+
} catch (e) {
110+
reject(e);
111+
}
112+
113+
const proc = spawn(
114+
'tar',
115+
['-xzvf', sourceFile]
116+
);
117+
118+
proc.on('exit', (code) => {
119+
if (code !== 0) {
120+
reject(new Error(`Failed to extract gzip archive. Exit code: ${code}`));
121+
}
122+
resolve(undefined);
123+
});
124+
});
125+
}
126+
127+
/**
128+
* Create a bzip2 archive from a directory.
129+
*/
130+
export async function createBzip2(sourceFile: string, destinationFile: string): Promise<void> {
131+
return new Promise(async (resolve, reject) => {
132+
133+
try {
134+
const stat = await lstat(sourceFile);
135+
if (!stat.isDirectory) {
136+
reject(new Error(`Directory '${sourceFile}' does not exist`));
137+
}
138+
} catch (e) {
139+
reject(e);
140+
}
141+
142+
const cwd = path.dirname(sourceFile);
143+
destinationFile = path.resolve(path.normalize(destinationFile));
144+
145+
const proc = spawn(
146+
'tar',
147+
['-cjvf', destinationFile, sourceFile]
148+
);
149+
150+
proc.on('exit', (code) => {
151+
if (code !== 0) {
152+
reject(new Error(`Failed to create bzip2 archive. Exit code: ${code}`));
153+
}
154+
resolve(undefined);
155+
});
156+
});
157+
}
158+
159+
/**
160+
* Extract the contents of a bzip2 archive.
161+
*/
162+
export async function extractBzip2(sourceFile: string): Promise<void> {
163+
return new Promise(async (resolve, reject) => {
164+
165+
try {
166+
const stat = await lstat(sourceFile);
167+
if (!stat.isDirectory) {
168+
reject(new Error(`Directory '${sourceFile}' does not exist`));
169+
}
170+
} catch (e) {
171+
reject(e);
172+
}
173+
174+
const proc = spawn(
175+
'tar',
176+
['-xjvf', sourceFile]
177+
);
178+
179+
proc.on('exit', (code) => {
180+
if (code !== 0) {
181+
reject(
182+
new Error(`Failed to extract bzip2 archive. Exit code: ${code}`),
183+
);
184+
}
185+
resolve(undefined);
186+
});
187+
});
188+
}
189+
190+
/**
191+
* Create a rar archive from a directory recursively.
192+
*/
193+
export async function createRar(sourceFile: string, destinationFile: string): Promise<void> {
194+
return new Promise(async (resolve, reject) => {
195+
196+
try {
197+
const stat = await lstat(sourceFile);
198+
if (!stat.isDirectory) {
199+
reject(new Error(`Directory '${sourceFile}' does not exist`));
200+
}
201+
} catch (e) {
202+
reject(e);
203+
}
204+
205+
const cwd = path.dirname(sourceFile);
206+
destinationFile = path.resolve(path.normalize(destinationFile));
207+
208+
const proc = spawn(
209+
'rar',
210+
['a', '-r', destinationFile, sourceFile]
211+
);
212+
213+
proc.on('exit', (code) => {
214+
if (code !== 0) {
215+
reject(
216+
new Error(
217+
`Failed to create rar archive from directory. Exit code: ${code}`,
218+
),
219+
);
220+
}
221+
resolve(undefined);
222+
});
223+
});
224+
}
225+
226+
/**
227+
* Extract the contents of a rar archive.
228+
*/
229+
export async function extractRar(sourceFile: string, destinationFile: string): Promise<void> {
230+
return new Promise(async (resolve, reject) => {
231+
try {
232+
const stat = await lstat(sourceFile);
233+
if (!stat.isFile) {
234+
reject(new Error(`File '${sourceFile}' does not exist`));
235+
}
236+
} catch (e) {
237+
reject(e);
238+
}
239+
240+
const proc = spawn(
241+
'unrar',
242+
['x', sourceFile, destinationFile]
243+
);
244+
245+
proc.on('exit', (code) => {
246+
if (code !== 0) {
247+
reject(new Error(`Failed to extract rar archive. Exit code: ${code}`));
248+
}
249+
resolve(undefined);
250+
});
251+
});
252+
}
253+
254+
/**
255+
* Create a zip archive from a directory recursively.
256+
*/
257+
export async function createZip(sourceFile: string, destinationFile: string): Promise<void> {
258+
return new Promise(async (resolve, reject) => {
259+
try {
260+
const stat = await lstat(sourceFile);
261+
if (!stat.isFile) {
262+
reject(new Error(`File '${sourceFile}' does not exist`));
263+
}
264+
} catch (e) {
265+
reject(e);
266+
}
267+
268+
const cwd = path.dirname(sourceFile);
269+
destinationFile = path.resolve(path.normalize(destinationFile));
270+
271+
const proc = spawn(
272+
'zip',
273+
['-r', destinationFile, sourceFile]
274+
);
275+
276+
proc.on('exit', code => {
277+
if (code !== 0) {
278+
reject(new Error(`Failed to create zip archive from directory. Exit code: ${code}`));
279+
}
280+
resolve(undefined);
281+
});
282+
});
283+
}
284+
285+
/**
286+
* Extract the contents of a zip archive.
287+
*/
288+
export async function extractZip(sourceFile: string, destinationFile: string): Promise<void> {
289+
return new Promise(async (resolve, reject) => {
290+
try {
291+
const stat = await lstat(sourceFile);
292+
if (!stat.isFile) {
293+
reject(new Error(`File '${sourceFile}' does not exist`));
294+
}
295+
} catch (e) {
296+
reject(e);
297+
}
298+
299+
const cwd = path.dirname(sourceFile);
300+
destinationFile = path.resolve(path.normalize(destinationFile));
301+
302+
const proc = spawn(
303+
'unzip',
304+
[sourceFile, '-d', destinationFile]
305+
);
306+
307+
proc.on('exit', code => {
308+
if (code !== 0) {
309+
reject(new Error(`Failed to extract zip archive. Exit code: ${code}`));
310+
}
311+
resolve(undefined);
312+
});
313+
});
314+
}

0 commit comments

Comments
 (0)