1 回答

TA貢獻1906條經驗 獲得超3個贊
如果你想逐行翻譯該函數:
// This is ES6 code; if you want better browser compatibility
// use the ES5 variant.
import { gunzipSync, strToU8, strFromU8 } from 'fflate';
const decompress = str => {
? // atob converts Base64 to Latin-1
? // strToU8(str, true) converts Latin-1 to binary
? const bytes = strToU8(atob(str), true);
? // subarray creates a new view on the same memory buffer
? // gunzipSync synchronously decompresses
? // strFromU8 converts decompressed binary to UTF-8
? return strFromU8(gunzipSync(bytes.subarray(4)));
}
如果您不知道 ES6 是什么:
在您的 HTML 文件中:
<script src="https://cdn.jsdelivr.net/npm/fflate/umd/index.js"></script>
在你的JS中:
var decompress = function(str) {
? var bytes = fflate.strToU8(atob(str), true);
? return fflate.strFromU8(fflate.gunzipSync(bytes.subarray(4)));
}
我想提一下,如果您要在最后累積成一個字符串,那么流幾乎完全沒有用處,因此 C# 代碼不是最佳的。同時,由于您使用的是標準庫,因此它是唯一的選擇。
此外,如果可能的話,我強烈建議使用回調變體(即gunzip代替gunzipSync),因為它在單獨的線程上運行,以避免導致瀏覽器凍結。
添加回答
舉報