下载文件中文乱码问题
continuous-integration/drone/push Build is passing Details

main
wangxiaoshuang 2026-01-19 17:51:11 +08:00
parent 44519c5f8e
commit d059d016a6
1 changed files with 11 additions and 8 deletions

View File

@ -61,7 +61,7 @@ function concatUint8(chunks, total) {
function createWriter(underlyingSource) {
const files = Object.create(null)
const filenames = []
const encoder = new TextEncoder()
const encoder = new TextEncoder() // 保持UTF-8编码
let offset = 0 // bytes written to output so far
let activeZipIndex = 0
@ -141,7 +141,7 @@ function createWriter(underlyingSource) {
if (fileLike.directory && !name.endsWith('/')) name += '/'
if (files[name]) throw new Error('File already exists.')
const nameBuf = encoder.encode(name)
const nameBuf = encoder.encode(name) // 保持UTF-8编码
const commentBuf = encoder.encode(fileLike.comment || '')
const { time, date } = dosDateTime(typeof fileLike.lastModified === 'undefined' ? Date.now() : fileLike.lastModified)
@ -174,15 +174,17 @@ function createWriter(underlyingSource) {
writeLocalHeader() {
this.offset = offset
const generalPurposeBitFlag = 0x0008 // data descriptor present
// 关键修改设置通用位标记第11位0x0800表示文件名使用UTF-8编码
// 保留原0x0008数据描述符存在新增0x0800UTF-8编码
const generalPurposeBitFlag = 0x0808
const compressionMethod = 0 // store
const versionNeeded = 20 // 2.0 (ZIP64 switches this to 45)
const versionNeeded = this.needsZip64() ? 45 : 20 // 适配ZIP64
// We always use data descriptor; write zeros for crc/sizes in local header
const local = getDataHelper(30 + this.nameBuf.length)
u32(local.view, 0, ZIP_SIGNATURE_LOCAL)
u16(local.view, 4, versionNeeded)
u16(local.view, 6, generalPurposeBitFlag)
u16(local.view, 6, generalPurposeBitFlag) // 应用UTF-8标记
u16(local.view, 8, compressionMethod)
u16(local.view, 10, this.time)
u16(local.view, 12, this.date)
@ -250,7 +252,8 @@ function createWriter(underlyingSource) {
const versionMadeBy = useZip64 ? 45 : 20
const versionNeeded = useZip64 ? 45 : 20
const generalPurposeBitFlag = 0x0008
// 关键修改中央目录也需要设置UTF-8标记
const generalPurposeBitFlag = 0x0808
const compressionMethod = 0
const crc = file.crc ? file.crc.get() >>> 0 : 0
@ -277,7 +280,7 @@ function createWriter(underlyingSource) {
u32(cd.view, 0, ZIP_SIGNATURE_CENTRAL)
u16(cd.view, 4, versionMadeBy)
u16(cd.view, 6, versionNeeded)
u16(cd.view, 8, generalPurposeBitFlag)
u16(cd.view, 8, generalPurposeBitFlag) // 中央目录应用UTF-8标记
u16(cd.view, 10, compressionMethod)
u16(cd.view, 12, file.time)
u16(cd.view, 14, file.date)