将文件保存在 .rar 中(有或没有密码)

Saving file inside .rar (with or without password)

我有这个 Save as CSV 按钮,它可以将我的 table 保存到一个完全有效的 CSV 文件中。但是我怎样才能自动将这个 CSV 文件放入一个带密码的 .rar 文件中呢?所以基本上,我希望我的 CSV 文件位于 saving/exporting 数据的 .rar 中。如果可能,如何在 .rar 文件中添加密码?

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']);
            });
        });

我所知道的Java脚本中rar的唯一实现是在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/(后者似乎使用起来更简单)但是,据我所知, 两者都不支持加密。

我只是将文件压缩并用例如 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/ 加密它们,但这样做非常简单,例如:Java(许多示例可以在线找到)

抱歉,这是今天的最新技术,明天可能会改变,甚至可能在我写的时候就已经改变了——我在这方面有点慢。