Search code examples
javascriptrar

Saving file inside .rar (with or without password)


I have this Save as CSV button which saves my table into a CSV file which is completely working. But how can I automatically put this CSV file inside a .rar file with a password? So basically, I want my CSV file to be inside a .rar upon saving/exporting the data. And if possible, how do I add a password in the .rar file?

Html:

<a class="export"> // Export button
    <i class="fa fa-file-text-o">

    </i>
    Save as CSV
</a> <br>
CSV Format: <br><br>
<div id="dvData"> // Data to CSV/Txt file
    <table class="table table-bordered table-striped table-condensed sortable" id="table2" style="width: 50%;">
        <tr>
            <th>Employee ID</th>
            <th>Time</th>
            <th>Action</th>
        </tr>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
            <td>Data 3</td>
        </tr>
    </table>
</div>

JavaScript:

        $(document).ready(function() {
            function exportTableToCSV($table, filename) {
                var $rows = $table.find('tr:has(td):visible'),
                    tmpColDelim = String.fromCharCode(11),
                    tmpRowDelim = String.fromCharCode(0),
                    colDelim = ' ',
                    rowDelim = '\r\n',
                    csv = '' + $rows.map(function(i, row) {
                        var $row = $(row),
                            $cols = $row.find('td');
                        return $cols.map(function(j, col) {
                            var $col = $(col),
                                text = $col.text();
                            return text.replace(/"/g, '');
                        }).get().join(tmpColDelim);
                    }).get().join(tmpRowDelim)
                        .split(tmpRowDelim).join(rowDelim)
                        .split(tmpColDelim).join(colDelim) + '',
                    csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

                $(this)
                    .attr({
                    'download': filename,
                        'href': csvData,
                        'target': '_blank'
                });
            }

            $(".export").on('click', function(event) {
                exportTableToCSV.apply(this, [$('#dvData > table'), 'dtr.csv']);
            });
        });

Solution

  • The only implementation of rar in JavaScript I know is at https://github.com/43081j/rar.js/ and does not seem to support encryption. If you need a portable compression format you may use ZIP instead with two implementations at https://gildas-lormeau.github.io/zip.js/ and https://stuk.github.io/jszip/ (the latter seems to be the simpler one to use) but, as far as I can see, both do not support encryption.

    I would just ZIP the files and encrypt them with e.g.: AES http://point-at-infinity.org/jsaes/ (the code is GPL3 but that's not much of a problem with JavaScript, especially if you just use it) and in that order. AES en/decryption programs (with source, of course!) at https://www.aescrypt.com/download/ but it is quite simple to do it in e.g.: Java (many examples to be found on-line)

    Sorry, but that's the state of the art today, it might change tomorrow, or it might even have changed while I'm writing it-I'm a bit slow in this things.