Created
January 31, 2020 06:47
-
-
Save zhangzhhz/f54a5c2e6b5d665c3543ad3590f90139 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| function packFile(filename, outfilename, noOfBytesToPrefix = 100) { | |
| const filenameEncoded = new Buffer.from(filename, 'utf8'); | |
| const filenameLength = filenameEncoded.length; | |
| if (filenameLength > noOfBytesToPrefix) { | |
| throw new Error("file name after utf-8 encoding is too long"); | |
| } | |
| let prefixBuffer = new Buffer.allocUnsafe(noOfBytesToPrefix).fill(0); | |
| filenameEncoded.copy(prefixBuffer) | |
| // console.log(filenameEncoded); | |
| // console.log(prefixBuffer); | |
| if (!fs.existsSync(filename)) { | |
| throw new Error(`File does not exist: [${filename}]`); | |
| } | |
| if (!fs.statSync(filename).isFile()) { | |
| throw new Error(`Is not a file: [${filename}]`); | |
| } | |
| let rs = fs.createReadStream(filename, { | |
| encoding: null, | |
| autoClose: true, | |
| }); | |
| let ws = fs.createWriteStream(outfilename, { | |
| encoding: null, | |
| autoClose: true, | |
| }); | |
| ws.write(prefixBuffer); | |
| rs.on('error', (err) => { | |
| rs.close(); | |
| ws.close(); | |
| throw(new Error(`error piping ${filename} to ${outfilename}`)); | |
| }); | |
| rs.pipe(ws); | |
| } | |
| function unpackFile(filename, noOfBytesPrefixed = 100) { | |
| if (!fs.existsSync(filename)) { | |
| throw new Error(`File does not exist: [${filename}]`); | |
| } | |
| if (!fs.statSync(filename).isFile()) { | |
| throw new Error(`Is not a file: [${filename}]`); | |
| } | |
| let rs = fs.createReadStream(filename, { | |
| encoding: null, | |
| autoClose: true, | |
| }); | |
| // get the first noOfBytesPrefixed octets to get the original file name | |
| rs.once('readable', () => { | |
| let prefixBuffer = rs.read(noOfBytesPrefixed); | |
| if (prefixBuffer) { | |
| let idx = prefixBuffer.indexOf(0); | |
| if (idx === -1) { | |
| idx = prefixBuffer.length; | |
| } | |
| const filenameEncoded = prefixBuffer.subarray(0, idx); | |
| const outfilename = filenameEncoded.toString('utf8'); | |
| let ws = fs.createWriteStream(outfilename, { | |
| encoding: null, | |
| autoClose: true, | |
| }); | |
| rs.resume(); | |
| rs.on('error', (err) => { | |
| rs.close(); | |
| ws.close(); | |
| throw(new Error(`error piping ${filename} to ${outfilename}`)); | |
| }); | |
| rs.pipe(ws); | |
| } | |
| }); | |
| } | |
| function testStream() { | |
| let crypto = require('crypto'); | |
| let fs = require('fs'); | |
| let readStream = fs.createReadStream('test.js'); | |
| let hash = crypto.createHash('sha256'); | |
| readStream.on('readable', function () { | |
| let chunk; | |
| while (null !== (chunk = readStream.read())) { | |
| hash.update(chunk); | |
| } | |
| }).on('end', function () { | |
| console.log(hash.digest('hex')); | |
| }); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment