Reading Compressed Files With Pure JavaScript – uncompress.js

Category: Javascript | August 7, 2018
Author: workhorsy
Views Total: 1,411 views
Official Page: Go to website
Last Update: August 7, 2018
License: MIT

Preview:

Reading Compressed Files With Pure JavaScript – uncompress.js

Description:

uncompress.js is a JavaScript tool that uses several JavaScript libraries to read compressed files and output file information on the screen. Supports Zip, Rar and Tar.

How to use it:

Load the uncompress.js JavaScript file and other required resources in the html page.

<script src="js/polyfill.js"></script>
<script src="js/libunrar.js"></script>
<script src="js/jszip.js"></script>
<script src="js/libuntar.js"></script>
<script src="js/uncompress.js"></script>

Create a file input to import local compressed files.

<input id="fileInput" type="file" required>

Create a container where the JavaScript library will output the file information.

<div id="entryList"></div>

The JavaScript.

var entryList = null;

function getFileMimeType(file_name) {
  file_name = file_name.toLowerCase();
  if (file_name.endsWith('.jpeg') || file_name.endsWith('.jpg')) {
    return 'image/jpeg';
  } else if (file_name.endsWith('.png')) {
    return 'image/png';
  } else if (file_name.endsWith('.bmp')) {
    return 'image/bmp';
  } else if (file_name.endsWith('.gif')) {
    return 'image/gif';
  } else {
    // Uses jpeg as default mime type
    return 'image/jpeg';
  }
}

function toFriendlySize(size) {
  if (size >= 1024000000) {
    return (size / 1024000000).toFixed(2) + ' GB';
  } else if (size >= 1024000) {
    return (size / 1024000).toFixed(2) + ' MB';
  } else if (size >= 1024) {
    return (size / 1024).toFixed(2) + ' KB';
  } else if (size >= 1) {
    return (size / 1).toFixed(2) + ' B';
  } else if (size === 0) {
    return '0 B';
  }

  return '?';
}

function onEach(archive, i) {
  // If this is the last entry, close the archive
  if (i >= archive.entries.length) {
    archiveClose(archive);
    return;
  }

  // Read the data for this entry
  var entry = archive.entries[i];
  entry.readData(function(data) {
    if (entry.is_file) {
      // Convert the data into an Object URL
      var blob = new Blob([data], {type: getFileMimeType(entry.name)});
      var url = URL.createObjectURL(blob);

      // Add a BR to the document
      entryList.appendChild(document.createElement('br'));

      // Add a link to the Object URL
      var a = document.createElement('a');
      a.href = url;
      a.innerHTML = entry.name + ' (' + toFriendlySize(data.byteLength) + ')';
      entryList.appendChild(a);
    }

    // Schedule the next iteration. Use a timeout so we don't block too long.
    setTimeout(function() {
      onEach(archive, i + 1);
    }, 0);
  });
}

window.onload = function() {
  entryList = document.getElementById('entryList');

  document.getElementById('fileInput').onchange = function() {
    // Just return if there is no file selected
    var file_input = document.getElementById('fileInput');
    if (file_input.files.length === 0) {
      entryList.innerHTML = 'No file selected';
      return;
    }

    // Get the file's info
    var file = file_input.files[0];
    var blob = file.slice();
    var file_name = file.name;

    // Convert the blob into an array buffer
    var reader = new FileReader();
    reader.onload = function(evt) {
      var array_buffer = reader.result;

      // Open the file as an archive
      var archive = archiveOpen(file_name, array_buffer);
      if (archive) {
        console.info('Uncompressing ' + archive.archive_type + ' ...');

        // Start iterating over each entry in the archive
        entryList.innerHTML = '';
        onEach(archive, 0);
      } else {
        entryList.innerHTML = 'Failed to uncompress file';
      }
    };
    reader.readAsArrayBuffer(blob);
  };
};

Changelog:

08/07/2018

  • Bugfixes

You Might Be Interested In:


Leave a Reply