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