Editing a file within a zipped file using JSZip - javascript

Using JSZip, is there a way to edit a file within a zipped file?
I've tried looking for solutions and going through the API but I can't seem to find a solution.
Any help with this would be great! Thanks in advance!

You can edit a file inside your zip with .file method.
zip.file("existing_filename", "new file content");
This method is used for adding and updating file content.
Just make sure the file already exist.
You can read more about it in the documentation.

You can refer to the official documentation.
And here's a more complete Node.js example:
var fs = require("fs");
var JSZip = require("jszip");
async function zipDemo() {
// read the existing zip file
var zipData = fs.readFileSync("input.zip");
var zip = await JSZip.loadAsync(zipData);
// add a new JSON file to the zip
zip.file("sample.json", JSON.stringify({demo:123}));
// write out the updated zip
zip.generateNodeStream({type:'nodebuffer', streamFiles:true})
.pipe(fs.createWriteStream('output.zip'))
.on('finish', function () {
console.log("output`enter code here`.zip written.");
});
}
zipDemo();

Related

javascript - unzip tar.gz archive in google script

I am trying to unzip the file: [https://wwwinfo.mfcr.cz/ares/ares_vreo_all.tar.gz][1] into google drive folder.
So, I have downloaded the file using google script, but can not properly unzip it. Could you, please, help me with it?
Thank you in advance!
Update:
Here is my code
function updateDB() {
var url = 'https://wwwinfo.mfcr.cz/ares/ares_vreo_all.tar.gz';
var blob = UrlFetchApp.fetch(url).getBlob();
var folder = DriveApp.getFolderById('fileid');
var archive = folder.createFile(blob);
unzip(archive);
}
function unzip(archive){
var zipblob = archive.getBlob();
var uncompressed1 = Utilities.ungzip(zipblob);
}
So I receive the following error:
Exception: Could not decompress gzip.
I guess it does not decompress normally, that is why I am asking if you would know different way
[1]: https://wwwinfo.mfcr.cz/ares/ares_vreo_all.tar.gz
You can use the Utilities Class to unzip your File.
To unzip the gzip you have to call the ungzip() method:
var textBlob = Utilities.newBlob("Some text to compress using gzip compression");
// Create the compressed blob.
var gzipBlob = Utilities.gzip(textBlob, "text.gz");
// Uncompress the data.
var uncompressedBlob = Utilities.ungzip(gzipBlob);
That's the example provided in the official documentation.
You can also take a look at the answer given to this question that also explains how to use the Utilities Class in combination with Google Drive.

How to iterate through folder of files on GitHub using Javascript?

I want to be able to iterate through a local folder of files (CSVs) on GitHub and use the file contents in Javascript. I used this code but it only retrieves the contents of one file:
var array = [];
var file = new XMLHttpRequest();
file.onreadystatechange = function () {
array.push(this.responseText);
}
file.open("GET", "[csv link]", true);
I read through other questions, and the suggested method was to use PHP, but because I am going to be using this for GitHub pages, PHP isn't supported. Are there any workarounds to this?

How to make a pdf file with node js using a custom font from a local file?

I tried all library and tricks possibles, I tested everything found on the web, but nothing worked so... I need to make a pdf file with node js using a custom font from a local file. The font format is otf. Nothing more to tell you :x
To generate PDF file from Node.js, I can advise you to do it from a HTML template.
To do this, I'm using this library: phantom-html-to-pdf.
Here is an example of usage :
const fs = require('fs');
const htmlTopdf = require('phantom-html-to-pdf')();
let html = fs.readFileSync('./template.html');
htmlTopdf({html: html}, function(err, pdf) {
if (err) {
console.log(err);
}
else {
pdf.stream.pipe(fs.createWriteStream('./destination.pdf'));
}
});
Hope it helps.

How to create a folder in Firebase Storage?

So the new Firebase has support for storage using Google Cloud Platform.
You can upload a file to the images folder using:
var uploadTask = storageRef.child('images').put(file, metadata);
What if you want to create a subfolder images/user1234 dynamically using code?
The offical sample does not show how to do that, nor the official guide or reference docs.
Is the Firebase Console the only place where folders can be created manually?
The Firebase Storage API dynamically creates "folders" as intermediate products: if you create a file at images/user1234/file.txt, all intermediate "folders" like "images" and "user1234" will be created along the way. So your code becomes:
var uploadTask = storageRef.child('images/user1234/file.txt').put(file, metadata);
Note that you need to include the file name (foo.txt for example) in the child() call, since the reference should include the full path as well as the file name, otherwise your file will be called images.
The Firebase Console does allow you to create a folder, since it's the easiest way to add files to a specific folder there.
But there is no public API to create a folder. Instead folders are auto-created as you add files to them.
You most certainly can create directories... with a little bit of playing with the references I did the following.
test = (e,v) => {
let fileName = "filename"
let newDirectory = "someDir"
let storage = firebase.storage().ref(`images/${newDirectory}/${fileName}`)
let file = e.target.files[0]
if(file !== undefined && file.type === "image/png" ) {
storage.put(file)
.then( d => console.log('you did it'))
.catch( d => console.log("do something"))
}
}
String myFolder = "MyImages";
StorageReference riversRef = storageReference.child(myFolder).child("images/pic.jpg");
Firebase is lacking very important functionality, there's always the need to be doing some tricks to emulate behaviours that should be standard.
If you create a folder manually from the Firebase console it will
persist even when there are no more files in it.
If you create a folder dynamically and all files get deleted at some
point, the folder will disappear and be deleted as well.
I implemented a file manager using Firebase Storage so when a user wants to upload a file he can do it through this interface not from something external to the app as is the Firebase Console. You want to give the user the option to reorganize the files as he wants, but something as common as creating a new folder cannot be done without tricks, why? just because this is Firebase.
So in order to emulate this behaviour what I came up with was the following:
Create a reference with the new folder name.
Create a reference for a "ghost" file as child of the folder's reference and give it always the same fixed name, eg. '.ghostfile'
Upload the file to this newly created folder. Any method is valid, I just use uploadString.
Every time I list the files of a reference, exclude any file named as before. So this "ghost" file is not shown in the file manager.
So an example to create a foler:
async function createFolder (currentRef: StorageReference, folderName: string) {
const newDir = ref(currentRef, name)
const ghostFile = ref(newDir, '.ghostfile')
await uploadString(ghostFile, '')
}
And an example to list the files:
async function loadLists (ref: StorageReference) {
const { prefixes, items } = await listAll(ref)
return {
directories: prefixes,
files: items.filter(file => file.name !=== '.ghostfile')
}
}
Firebase console allows you to create a folder. I don't think there is another way to create a folder.

WinJs getFileAsync without stating a folder first

I have a string to the path of a file, and i would like to get the file. So far i have only found this, which relies on the file being within a specified folder, or a child of this folder.
var rootFolder = Windows.Storage.ApplicationData.current.localFolder;
rootFolder.getFileAsync('MY_FILE_PATH_FROM_ROOT').then(function (file) {
});
By rootFolder points to a folder where the app is installed, something like C:\Program Files....
What if i have a filePath string of something like C:\MyFiles\Picture\Pic_123.png. How would i get this file?
Sorry i am new to WinJs.
Kind of simple once you see the API. Use
Windows.Storage.StorageFile.getFileFromPathAsync('YOUR_FILE_PATH')
.then(function (file) {
});

Categories

Resources