Skip to content

planet0104/bzip2-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bzip2-js

compile bzip2-rs to javascript.

use in HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="bzip2.js"></script>
</head>
<body>
    <script>
        function stringToBytes(str) {
            var array = new Uint8Array(str.length);
            for (var i = 0, l = str.length; i < l; i++) {
                array[i] = str.charCodeAt(i);
            }
            return array;
        }

        var data = "hello!hello!hello!";
        var compressed = Bzip2.compress(stringToBytes(data));
        console.log("compressed=", compressed);
        document.body.append("compressed="+Bzip2.decompressUtf8String(compressed));
    </script>
</body>
</html>

use in miniprogram

    var Bzip2 = require("./bzip2.js");

    let input = "hello!hello!hello!";

    let result = Bzip2.compress(new Uint8Array(stringToBytes(input)));

    console.log('compressed:', result);

    console.log('decompressed:', Bzip2.decompressUtf8String(result));