5

I am trying to create a self-extracting archive, in the form of a single .html file (as opposed to the more traditional .exe ones). In other words a single .html file with another file (of any type) embedded in it.

Here is a very simple example of a .html file that does exactly that. It has a link which you click to generate generate and download a .png file from a base 64 encoded string which is hard-coded in the source. This could be extended fairly easily to work with any kind of file.

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Self-Extracting Archive</title>
</head>

<body>
    <a href="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
        download>Save File</a>
</body>

</html>

The problem is that base 64 is not very efficient. Is it possible to encode the file as plain binary? I know if it is possible that it will probably be hacky. That is fine, as long as it can be made compatible with all major browsers.

I think something like File.getAsBinary() might work. If it were available (but it isn't), you could append the binary to the end of the .html file and then somehow convince the browser not to parse into that part of the file.

Ben Hansell
  • 101
  • 6
  • Attention, [File.getAsBinary()](https://developer.mozilla.org/en-US/docs/Web/API/File/getAsBinary) is not standard and obsolete. [UInt8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) could be interesting for this task. – Sebastian Waldbauer Jan 24 '19 at 18:44
  • I'm assuming you're trying to create a file *for download*, not that will actually execute, right? Because you can't do the latter (thank goodness). If it's the former, this question is a duplicate of [Saving binary data as file using JavaScript from a browser](https://stackoverflow.com/q/23451726/215552). – Heretic Monkey Jan 24 '19 at 18:55
  • @HereticMonkey no I do not need to execute the file, in fact the file could be any file type (executable or not). Also, as far as I can tell it is not a duplicate of that question because that question only covers base-64, which has a 33% overhead, whereas I am interested in whether it is possible to store the data in raw binary, with 0% overhead. – Ben Hansell Jan 25 '19 at 19:41
  • The [accepted answer](https://stackoverflow.com/a/23451803/215552) uses an `Int8Array` and a `Blob`. It also uses [`createObjectURL`](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL), which does not create a Base 64 encoded data URI -- it creates a URL to the Blob in the browser's internal Blob store; presumably the browser uses a more efficient storage method than Base 64. More detailed information can be had [on W3's site](https://w3c.github.io/FileAPI/#dfn-createObjectURL). – Heretic Monkey Jan 25 '19 at 19:53
  • @HereticMonkey I am not sure if I am explaining myself properly. I am aware that `UInt8Array`s and `Blob`s stores data efficiently, however they can only exist in RAM. I am interested in storing data efficiently inside the HTML file itself. The HTML file will then be distributed to the user who can use the HTML file to generate another file from data stored in the HTML file. The generated file should be only slightly smaller than the HTML file (not 33% smaller as it would be if base 64 encoding was used). – Ben Hansell Jan 26 '19 at 09:42
  • Ah, well, there’s a reason we have binary file formats and not just html (a text file format), I guess... – Heretic Monkey Jan 27 '19 at 21:08
  • Why are you wanting this? – Seth B Feb 17 '21 at 23:27
  • At the time I was interested in doing what I described in the question. Creating a kind of self-extracting or self-decrypting archive compatible with any platform that has a web browser. Similar to how WinZip can make self-extracting .exe archives which contain not only the data, but the algorithm to extract the data. Instead of .exe, the archive would have the extension .html. – Ben Hansell Feb 19 '21 at 00:24

0 Answers0