1

I have a XML file like:

<People>
    <Person>
        <Name>John</Name>
        <Lastname>Doe</Lastname>
        <Age>30</Age>
    </Person>
    <Person>
        <Name>Jane</Name>
        <Lastname>Doe</Lastname>
        <Age>29</Age>
    </Person>
</People>

It is compressed to .zip file. I can read standalone (non zipped) file easily with:

HTML

<input type="file" id="unzipTest">

JavaScript

document.getElementById( 'unzipTest' ).addEventListener( 'change', unzipFile );

function unzipFile( event ) {
    var eTarget = event.target;
    var file = eTarget.files[ 0 ];

    var reader = new FileReader();
    reader.onload = function(){
      var result = reader.result;
      console.log( 'result: ', result );
    };
    reader.readAsText( file );
}

I correctly get XML file content in result variable.

The problem is when I am trying to extract a zip file containing that XML. I am doing like so:

function unzipFile( event ) {
    var eTarget = event.target;
    var file = eTarget.files[ 0 ];

    var zip = new JSZip();
    zip.loadAsync( file, { optimizedBinaryString: true } ).then( function( fileContent ) {
        var key = Object.keys( fileContent.files )[ 0 ];
        var data = fileContent.files[ key ];
        var compressedContent = data[ '_data' ].compressedContent;

        console.log( 'compressed content: ', compressedContent );

        var blob = new Blob( [ compressedContent ] );

        var reader = new FileReader();
        reader.onload = function(){
            var result = reader.result;
            console.log( 'result: ', result );
        };
        reader.readAsArrayBuffer( blob );
    });
}

The compressedContent variable is some kind of Array (uint8array) with bits(?). I tried to read this Array by .readAsArrayBuffer(); method of reader with passing it a blob object. Unfortunately what I get is some weird ArrayBuffer with byteLength property which is a number.

What I do wrong and how to read extracted XML file properly?

I am using JSZip library: https://stuk.github.io/jszip/

Additionally - I need it to work with Internet Explorer 11

[SOLVED] That solution helped me: https://stackoverflow.com/a/39964957/4983840

Community
  • 1
  • 1
ScriptyChris
  • 639
  • 4
  • 16
  • 48
  • try this - http://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers – Pradyut Bhattacharya Nov 27 '16 at 11:50
  • Method with using `new TextDecoder();` gave me some weird symbols (and it doesn't work for IE11, which I need to work with). Solution with `String.fromCharCode` doesn't work either. – ScriptyChris Nov 27 '16 at 12:18
  • I am a little bit closer with using `var base64 = btoa(String.fromCharCode.apply(null, compressedContent));` The output is string of some characters, but without weird symbols. – ScriptyChris Nov 27 '16 at 13:03

0 Answers0