-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzippy.js
161 lines (151 loc) · 4.54 KB
/
unzippy.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Unzippy - A Node.js unzip library Copyright 2019 Hitesh Lala
* This file is covered by the License file in the root of this project https://github.com/hiteshlala/unzippy
*/
const fs = require( 'fs' );
const path = require( 'path' );
const t = require( './tools' );
class Unzippy {
constructor( options ) {
this.src = path.resolve(options.src || '' );
this.dest = path.resolve( options.dest || './' );
this.dir; // []
this.errorState = false;
this.threads = options.threads || 1;
try {
const toRead = 1024;
this.stat = fs.statSync( this.src );
this.fd = fs.openSync( this.src, 'r' );
let buf = Buffer.alloc( toRead, 0 );
fs.readSync( this.fd, buf, 0, toRead, this.stat.size - toRead );
this.ecdr = t.readECDR( buf );
this.zecdl = this.ecdr && t.readZECDL( buf );
this.zecdr = this.zecdl.found && t.readZECDR( this.fd, this.zecdl.startZip64CD );
if ( !this.ecdr.found ) {
this.error = 'Unrecognized file archive.';
this.errorState = true;
}
}
catch( e ) {
this.error = e.message || e;
this.errorState = true;
}
}
async unzip() {
if ( this.errorState ) {
return Promise.reject( new Error( this.error ) );
}
if ( !this.dir ) {
this.getDir();
}
let processing = 0;
const threads = [];
const errors = [];
const success = [];
const processOne = async () => {
try {
if ( processing < this.dir.length ) {
const index = processing;
processing++;
const result = await this.extractSingle( index );
success.push( result );
return processOne();
}
else {
return Promise.resolve();
}
}
catch( e ) {
errors.push( e );
return processOne();
}
};
for( let i = 0; i < this.threads; i++ ) {
threads.push( processOne() );
}
return Promise.all( threads )
.then( () => {
return {
errors,
success
};
})
}
getDir() {
if ( this.errorState ) {
throw new Error( this.error );
}
if ( this.dir ) {
return this.dir;
}
else {
const { ecdr, zecdl, fd, zecdr } = this;
let buf;
let records;
if ( ecdr.found && !zecdl.found ) {
const toRead = ecdr.sizeCDRecord;
const start = ecdr.startCD;
records = ecdr.numCDRecOnDisk;
buf = Buffer.alloc( toRead, 0 );
fs.readSync( fd, buf, 0, toRead, start );
}
else if ( zecdl.found ) {
const toRead = zecdr.sizeCDRecord;
const start = zecdr.startCD;
records = zecdr.numCDRecOnDisk;
buf = Buffer.alloc( toRead, 0 );
fs.readSync( fd, buf, 0, toRead, start );
}
this.dir = [];
let index = 0;
let cdh;
for ( let i = 0; i < records; i++ ) {
index = cdh ? cdh.length + index : 0;
cdh = t.readCDH( buf, index );
this.dir.push( cdh );
}
return this.dir;
}
}
async extractSingle( id ) {
if ( this.errorState ) { return new Error( this.error ); }
if ( typeof id !== 'number' || id >= this.dir.length ) { return new Error( 'Invalid id'); }
const cdh = this.dir[ id ];
const lfh = t.readLFH( this.fd, cdh.offsetStart )
// console.log( '\nLFH:\n', lfh );
if ( lfh.found ) {
try {
if ( lfh.comprMeth === 8 ) {
await t.inflate( fs.openSync( this.src, 'r' ), cdh, lfh, this.dest );
return { extracted: cdh.fName };
}
else if ( lfh.comprMeth === 0 ) {
await t.copy( fs.openSync( this.src, 'r' ), cdh, lfh, this.dest );
return { extracted: cdh.fName };
}
else {
const msg = `Error extracting file [ ${cdh.fName} ] -Unsupported compression method ( ${lfh.comprMeth} ), skipping`;
console.log( msg );
return new Error( msg );
}
}
catch( e ) {
const msg = `Error extracting file [ ${cdh.fName} ] - ${ e.message || e }`;
console.log( msg );
return new Error( msg );
}
}
else {
const msg = `Error extracting file [ ${cdh.fName} ] - Unable to find the local file header (lfh)`;
console.log( msg );
return new Error( msg );
}
}
}
module.exports.Unzippy = Unzippy;
module.exports.unzip = async ( src, dest, options ) => {
const threads = options ? options.threads || 1 : 1;
if ( !src || !dest ) return new Error( 'Source path or Destination path missing' );
const u = new Unzippy({ src, dest, threads });
return u.unzip();
}