Search code examples
arraybufferjszipuint8arraytauri

TauriJS writeBinaryFile cannot freeze array buffer views with elements


I work with TauriJS and try to modify a zip file with jszip and later save it with writeBinaryFile.

function saveFile(org_path, new_path, pack_format) {
    var zip = new JSZip();

    // get file
    var org_file = await window.__TAURI__.fs.readBinaryFile(org_path);
    await zip.loadAsync(org_file);

    // edit file
    var pack_json = await zip.file("pack.json").async("string");
    pack_json = JSON.parse(pack_json);
    pack_json.pack.pack_format = pack_format;
    zip.file("pack.json", JSON.stringify(pack_json));

    // save file
    var array_zip = await zip.generateAsync({type:"uint8array"});
    await window.__TAURI__.fs.writeBinaryFile(new_path, array_zip);
}

This is the code I currently have. The problem is that it gives the error Uncaught TypeError: Cannot freeze array buffer views with elements

I wasn't able to find a solution to this error, is it somehow possible to bring the zip file in the right format to save it?


Solution

  • I found a way to fix the problem on this page: https://qdmana.com/2022/144/202205241127535226.html

    This is my adjusted code:

    function saveFile(org_path, new_path, pack_format) {
        var zip = new JSZip();
    
        // get file
        var org_file = await window.__TAURI__.fs.readBinaryFile(org_path);
        await zip.loadAsync(org_file);
    
        // edit file
        var pack_json = await zip.file("pack.json").async("string");
        pack_json = JSON.parse(pack_json);
        pack_json.pack.pack_format = pack_format;
        zip.file("pack.json", JSON.stringify(pack_json));
    
        // save file
        zip.generateAsync({ type: 'blob' }).then((content) => {
            var file = new FileReader();
            file.readAsArrayBuffer(content);
            file.onload = function (e) {
                var fileU8A = new Uint8Array(e.target.result);
                window.__TAURI__.fs.writeBinaryFile({ contents: fileU8A, path: new_path + ".zip" });
            };
        });
    }