nodejs extract/decompress all content of .gz compressed files to directory - javascript

i have a file in the file system named temp.gz
the temp gzip file contains files and folder
i would like to extract them and have them in a folder like we would normally unzip a file in OS.
i have tried below code but thats not meant for extracting files to folder.
const unzip = zlib.createUnzip();
var input = fs.createReadStream(inFilename); /path/to/temp.gz
var output = fs.createWriteStream(outFilename); /path/to/output/folder
Above didnt work and i believe the reason is that it writes to a file and i have provided a directoy.
my requirement is extract files to directory.
zlib.gzip(responseData, (err, buffer) => {
// Calling gunzip method
zlib.gunzip(buffer, (err, buffer) => {
console.log(buffer,pathFileName);
fs.appendFileSync(pathFileName, buffer);
});
});
above i was trying to unzip as per the docs and write buffer to the output file and didnt create a folder as expected and didnt add the files there
sure i am missing something but not sure what.

Related

Use gunzip on a folder

Right so I have a folder full of other folders, which are compressed into .gz files. Inside these folders is text files.
I want to have a program that loops through these text files to see if they contain a specific string, but to do so I need to uncompress them first. I don't want to start messing about with files (unless I can just make them temporarily and delete after), i just want to perform operations on the contents of the .gz folder. I've tried zlib.Gunzip()._outBuffer.toString() which gives a load of gibberish when used on a compressed folder.
How should I proceed?
Had to to something quite similar recently, here's what worked for me:
Basically you just read in the file into a buffer which you then can pass to the gunzip function. This will return another buffer on which you can invoke toString('utf8') in order the get the contents as a string, which is exactly what you need:
const util = require('util');
let {gunzip} = require('zlib');
const fs = require('fs');
gunzip = util.promisify(gunzip);
async function getStringFromGzipFile(inputFilePath) {
const sourceBuffer = await fs.promises.readFile(inputFilePath);
return await gunzip(sourceBuffer);
}
(async () => {
const stringContent = await getStringFromGzipFile('/path/to/file');
console.log(stringContent);
})()
EDIT:
If you want to gunzip and extract a directory, you can use tar-fs which will extract the contents to a specified directory. Once your done processing the files in it you could just remove the directory. Here's how you would gunzip and extract a .tar.gz:
function gunzipFolder(sourceDir, destination) {
fs.createReadStream(sourceDir)
.pipe(zlib.createGunzip())
.pipe(tar.extract(destination));
}

Javascript create zip file

I am developing a website using react.js and admin-on-rest. One feature is allowing users to upload a pdf file.
I get the file as type FILE and want to get the file from FILE, compress it to zip, and then make it to type FILE.
So it should be FILE -> origin file -> zip file -> FILE from zip file.
I tried JSZip but still can not figure it out.
Any help is appreciated. Thanks
You can use JSZIP.
**use npm to install JSZIP
let zip = require('jszip')();
//hoping you have already taken input
let input = document.getElementById('fileInput'); // fileInput is id of my input element
let file = input.files[0];
let allZip = zip.file(file.name, file);
console.log(allZip)
Hi Garrick following are the steps you need to take.
1) handle fileupload in a rest wrapper
https://marmelab.com/admin-on-rest/RestClients.html#decorating-your-rest-client-example-of-file-upload
the above example is for image upload. But you will essentially be doing the same thing.
2)
const addUploadCapabilities = requestHandler => (type, resource, params) => {
if (type === 'UPDATE' && resource === 'posts') {
//use jszip to zip file here and package it however you need
// call the API with zipped file
} return requestHandler(type, resource, params);
};
There is a small app called jszip for this. Try, it would help. https://stuk.github.io/jszip/

node.js unzip file. file name is broken

I tried to unzip the file.
let zip = new AdmZip('./sample.zip')
zip.extractAllTo(path, true)
but,the name of the contents file is Japanese.
So file name is broken.
���̑�-�T�|�[�g�O
Any suggestions?

Generate pdf files with Weasyprint, save in zip file, send that zip file to client and present it for download

Let me break down my requirement. Here's what I'm doing right now.
1. Generate PDF files from HTML
for this I'm using Weasyprint as following:
lstFileNames = []
for i, content in enumerate(lstHtmlContent):
repName = 'report'+ str(uuid.uuid4()) + '.pdf'
lstFileNames.append("D:/Python/Workspace/" + repName)
HTML(string=content).write_pdf(target=repName,
stylesheets=[CSS(filename='/css/bootstrap.css')])
all files names, with paths, are saved in lstFileNames.
2. Create a zip file with pdf files generated by weasyprint
for this I'm using zipfile
zipPath = 'reportDir' + str(uuid.uuid4()) + '.zip'
myzip = zipfile.ZipFile(zipPath, 'w')
with myzip:
for f in lstFileNames:
myzip.write(f)
3. Send zip file to client for download
resp = HttpResponse(myzip, content_type = "application/x-zip-compressed")
resp['Content-Disposition'] = 'attachment; filename=%s' % 'myzip.zip'
4. Open file for downloading via Javascript
var file = new Blob([response], {type: 'application/x-zip-compressed'});
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
Problems
1. While the zip file is successfully received at front end, after I try to open it, it gives the following error:
The archive is in either unknown format or damaged
Am I sending the file wrong or is my Javascript code the problem?
2. Is there a way to store all pdf files in list of byte arrays and generate zip files with those byte array and send it to the client? I tried that with weasyprint but the result was same damaged file.
3. Not exactly a problem but I haven't been able to find it in weasyprint docs. Can I enforce the path to where the file should be saved?
Problem # 1 is of extreme priority, rest are secondary. I would like to know if I'm doing it right i.e. generating pdf files and sending their zip file to client.
Thanks in advance.
A slightly different approach would be to move the zip file to a public directory and then send that location to the client (e.g. json formatted), i.e.:
publicPath = os.path.join('public/', os.path.basename(zipPath))
os.rename(zipPath, os.path.join('/var/www/', publicPath))
jsonResp = '{ "zip-location": "' + publicPath + '" }'
resp = HttpResponse(jsonResp, content_type = 'application/json');
Then in your client's javascript:
var res = JSON.parse(response);
var zipFileUrl = '/' + res['zip-location'];
window.open(zipFileUrl, '_blank');
'/' + res['zip-location'] assumes that your page lives in the same folder as the public directory (so http://example.com/public/pdf-files-123.zip points to /var/www/public/pdf-files-123.zip on your file system).
You can clean up the public directory with a cron job that deletes all the .zip files in there that are older than an hour or so.
Once you have exited the with block the filehandle is closed. You should reopen the file (this time with open) and use read() to pass the contents to HttpResponse instead of passing the filehandle itself.
with zipfile.ZipFile(zipPath, 'w') as myzip
for f in lstFileNames:
myzip.write(f)
with open(zipPath, 'r') as myzip:
return HttpResponse(myzip.read(), content_type = "application/x-zip-compressed")
If that works, then you can use a StringIO instance instead of a filehandle to store the zip file. I'm not familiar with Weasyprint so I don't know whether you can use StringIO for that.

Can I get upload path at runtime?

I need to get upload path of my uploaded files via jquery-file-uploader.
$('.new_plotphoto').fileupload({
done: function (e, data) {
var filess = data.files[0];
var filename = filess.name;
var filepath = filess.filepath;
console.log(filename); // this shows filename
console.log(filepath); // this shows undefined
}
});
Path of uploading file is a secure feature. From W3C File API docs about file name:
The name of the file; on getting, this must return the name of the
file as a string. There are numerous file name variations on different
systems; this is merely the name of the file, without path
information.
File path will be enabled only then you upload directory:
<input type=file multiple directory webkitdirectory onchange="console.log(this.files)">
In this case webkitRelativePath(for WebKit browsers) field of file object will be not empty. But it will show path only inside uploaded directory.
Also note that iOS browsers then uploading images always have this file name: image.jpg and no info about path.

Categories

Resources