Adm zip write zip buffer to ExpressJS response - node.js

Hi I'm trying to send a zip buffer made by Adm Zip npm module to my response for client download.
I manage to download the zip file but unable to expand it. OSX says "error 2 No such file or directory"...
The downlaoded zip file has got the right size I believe and is sent over this way:
var zip = new AdmZip();
// added files with zip.addFile(...)
var zipFile = zip.toBuffer();
res.contentType('zip');
res.write(zipFile);
res.end();
Any idea what could be wrong?
Thanks

Apparently it comes from the Adm-zip code base and hasn't been merged yet:
https://github.com/cthackers/adm-zip/compare/master...mygoare:unzipErr

Related

Python ZipFile module problem when file is encrypted

I have the following short program
from zipfile import ZipFile
procFile1 ="C:\\Temp\\XLFile-Demo.zip"
procFile2 ="C:\\Temp2\\XLFile-Demo-PW123.zip"
# Unencrypted file
print ("Unencrypted file")
myzip1 = ZipFile(procFile1)
print (myzip1.infolist())
myzip1.extractall("C:\\Temp")
# Encrypted File
print ("Encrypted file")
myzip2 = ZipFile(procFile2)
print (myzip2.infolist())
myzip2.setpassword(bytes('123', 'utf-8'))
myzip2.extractall("C:\\Temp2")enter code here
At this Amazon Drive link are the two files. They are identical except that one zip is protected with the password 123.
Executing the above code successfully extracts the unencrypted one but raises the error NotImplementedError: That compression method is not supported for the other.
Unencrypted file
[<ZipInfo filename='XLFile-Demo.xlsx' compress_type=deflate external_attr=0x20 file_size=31964 compress_size=29252>]
Encrypted file
[<ZipInfo filename='XLFile-Demo.xlsx' compress_type=99 external_attr=0x20 file_size=31964 compress_size=29280>]
Am I doing anything wrong from my end?
The error came up when the file was zipped using WinRar's ZIP option. I installed 7Zip and it is working.
The .infolist for the 7Zip file is the following:
[<ZipInfo filename='XLFile-Demo.xlsx' compress_type=deflate external_attr=0x20 file_size=31964 compress_size=29340>]
Incidentally WinRar can handle this file and 7Zip can correctly process the encrypted Zip archive created by WinRar.

how I can prevent res.sendFile changing file path?

I'm using node.js and want to send file to the frontend. So I specified the direct path to my file like:
path = "c:/app/A"
and when I run res.sendFile(path, fileName);
I'm getting the Error: ENOENT: no such file or directory, stat '/home/projects/c:/app/A'
How I can disable this auto path adding "/home/projects" part?
I want to download file that is not in my project folder with my code. File is in my computer in different folder.
Try to use \\ as path delimiter for Windows (c:\\app\\A) and read about Node.js module "path".
so I need use just new URL(file:${"c:/app/A"});
so it will be like that:
let filename = "someName.com"
let absPath = "c:/app/someName.com";
fs.writeFileSync(`${filename}`, fs.readFileSync(new URL(`file:${absPath}`)));
res.download(`${filename}`, `${filename}`)

AWS Lambda access denied to a module in subfolder

I have this Nodejs lambda function where some files are in a subfolder, like this:
- index.js
- connectors/
- affil.js
I have a Cannot find module error when trying to require the affil.js file. Trying to read it with fs.readFile returns an access denied error.
When I move the file to the root folder, it is accessible. Is there a requirement that Lambda functions files must all be at the root directory? How can I fix that?
Mostly it is because of the way zipping the files making the problem. Instead of zipping the root folder you have to select all files and zip it like below,
Please upload all files and subfolders like below. Please include node_modules folder as well in the zip.
As pointed by #Vijayanath Viswanathan, the issue is with how the zip file is created rather than Lambda.
I used to feed gulp-zip with this:
var src = gulp.src('src/**/*')
The correct way is to prevent folders from being included:
var src = gulp.src('src/**/*.js')
or (if you need to include file with other file extensions)
var src = gulp.src('src/**/*', {nodir: true})

Create tar file from files in a particular directory

I need to use nodejs to create a tar file that isn't encompassed in a parent directory.
For example, here is the file system:
/tmp/mydir
/tmp/mydir/Dockerfile
/tmp/mydir/anotherfile
What I'm looking to do is the equivalent to this:
cd /tmp/mydir
tar -cvf archive.tar *
So, when I extract archive.tar, Dockerfile will end up in the same directory I execute the command.
I've tried tar.gz and a few others, but all the examples are compressing an entire directory, and not just files.
I'm doing this so I can utilize the Docker REST API to send builds.
With a modern module node-tar you can create a .tar file like this:
tar.create(
{ file: 'archive.tar' },
['/tmp/mydir']
).then(_ => { .. tarball has been created .. })
The tar.gz module referenced in other answers is deprecated.
Use tar.gz module. Here is a sample code
var targz = require('tar.gz');
var compress = new targz().compress('/path/to/compress', '/path/to/store.tar.gz',
function(err){
if(err)
console.log(err);
console.log('The compression has ended!');
});
For more options, visit the documentation page.
This package is now deprecated. Check the answer provided by #Kelin.
Second argument to the constructor is passed on as properties to the tar module.
var TarGz = require('tar.gz');
var compressor = new TarGz({}, {fromBase: true});
This will use create the archive without top level directory.
Edit: This was undocumented in node-tar.gz but pull request has now been merged: https://github.com/alanhoff/node-tar.gz#tar-options

NodeJs: pipe tar file from http response to tar.Extract?

Following task:
We have a nodejs client (daemon) downloading audio data which is contained in a tar file (NOT tar.gz - it really is uncompressed!) from Amazon S3.
At the moment, we glue the chunks together in the 'data' handler of the response, save the whole buffer to disc as a file and then call tar.Extract(inPath, outPath) on the newly created file.
I'd like to skip the process of writing the data to disc and instead pass the data from the response directly to tar.Extract().
This is my handler code:
var readResponseData = function (response) {
response.setEncoding('binary');
response.pipe(tar.Extract( { path: '/tmp/testyeah' }));
....
....
I always get "Error: Invalid tar file"
I also tried without success the suggestions from this page (https://groups.google.com/forum/#!topic/nodejs/A7jz6b9daZc) although that should apply to compressed tar files and not the uncompressed ones that we use.
Any suggestions?

Resources