139 lines
3.5 KiB
TypeScript
139 lines
3.5 KiB
TypeScript
|
import { saveAs } from 'file-saver'
|
||
|
import type { ZipProvider } from '../zip.js'
|
||
|
import { DefaultZipProvider, FileInfo } from '../zip.js'
|
||
|
import { StringBuider } from './StringBuidler.js'
|
||
|
|
||
|
// import { textFile } from './File';
|
||
|
import { StringBase64 } from './StringBase64.js'
|
||
|
|
||
|
export class FileZip
|
||
|
{
|
||
|
zipName: string
|
||
|
// zip对象
|
||
|
private zip: ZipProvider = DefaultZipProvider()
|
||
|
// 文件数据
|
||
|
private stringBuider: StringBuider
|
||
|
|
||
|
constructor()
|
||
|
{
|
||
|
this.stringBuider = new StringBuider()
|
||
|
this.SetZipFileName('')
|
||
|
}
|
||
|
|
||
|
/** 设置zip文件名 */
|
||
|
SetZipFileName(_name: string)
|
||
|
{
|
||
|
this.zipName = _name
|
||
|
}
|
||
|
|
||
|
getZipFileName()
|
||
|
{
|
||
|
return this.zipName
|
||
|
}
|
||
|
|
||
|
PushBlobFile(fileName: string, content)
|
||
|
{
|
||
|
let newFile = new FileInfo(fileName, content, false)
|
||
|
newFile.binary = true
|
||
|
this.zip.addFile(newFile)
|
||
|
}
|
||
|
|
||
|
// /**添加文件 */
|
||
|
// PushFile(fileName: string, fileText: string, isBase64 = false, isgb2312 = false, isUtf8Bom=false)
|
||
|
// {
|
||
|
// if (isgb2312)
|
||
|
// {
|
||
|
// isBase64 = true;
|
||
|
// fileText = StringBase64.ToBase64_gb2312(fileText);
|
||
|
// }
|
||
|
// if(isUtf8Bom)
|
||
|
// {
|
||
|
// fileText = String.fromCharCode(parseInt("0xFEFF")) + fileText;
|
||
|
// }
|
||
|
// let newFile = new FileInfo(fileName, fileText, isBase64);
|
||
|
// this.zip.addFile(newFile);
|
||
|
// }
|
||
|
|
||
|
/** 添加文件 fileName文件名 fileText文件内容 isBase64是否base64 encoding编码格式(gb2312, utf8bom) */
|
||
|
PushFile(fileName: string, fileText: string, isBase64 = false, encoding: string = 'gb2312')
|
||
|
{
|
||
|
if (encoding == 'gb2312')
|
||
|
{
|
||
|
isBase64 = true
|
||
|
fileText = StringBase64.ToBase64_gb2312(fileText)
|
||
|
}
|
||
|
if (encoding == 'utf8bom')
|
||
|
{
|
||
|
fileText = String.fromCharCode(Number.parseInt('0xFEFF')) + fileText
|
||
|
}
|
||
|
let newFile = new FileInfo(fileName, fileText, isBase64)
|
||
|
this.zip.addFile(newFile)
|
||
|
}
|
||
|
|
||
|
pushFile_withBom(fileName: string, fileText: string)
|
||
|
{
|
||
|
this.PushFile(fileName, fileText, false, 'utf8bom')
|
||
|
}
|
||
|
|
||
|
PushFile_GB2312(fileName: string, fileText: string)
|
||
|
{
|
||
|
this.PushFile(fileName, fileText, false, 'gb2312')
|
||
|
}
|
||
|
|
||
|
/** 新建文件 */
|
||
|
NewFile()
|
||
|
{
|
||
|
this.stringBuider.Clear()
|
||
|
}
|
||
|
|
||
|
/** 推送文本到缓存区 */
|
||
|
AppentText(comment: string)
|
||
|
{
|
||
|
this.stringBuider.Append(comment)
|
||
|
}
|
||
|
|
||
|
// //将缓存区的文本 保存起来
|
||
|
// SaveFile(fileName: string, isgb2312: boolean = false, isutf8bom = false)
|
||
|
// {
|
||
|
// let fileText = this.stringBuider.ToString();
|
||
|
// this.stringBuider.Clear();
|
||
|
// this.PushFile(fileName, fileText, false, isgb2312, isutf8bom);
|
||
|
// }
|
||
|
|
||
|
// 将缓存区的文本 保存起来
|
||
|
SaveFile(fileName: string, encoding: string = 'gb2312')
|
||
|
{
|
||
|
let fileText = this.stringBuider.ToString()
|
||
|
this.stringBuider.Clear()
|
||
|
this.PushFile(fileName, fileText, false, encoding)
|
||
|
}
|
||
|
|
||
|
// 下载zip文件
|
||
|
async Download(zipName = '')
|
||
|
{
|
||
|
let content = await this.zip.saveAsync()
|
||
|
// textFile.saveAs(FileZip.zipName,content);
|
||
|
let name = zipName || this.zipName
|
||
|
saveAs(content, name)
|
||
|
}
|
||
|
|
||
|
static WriteFile(fname: string, data: BlobPart)
|
||
|
{
|
||
|
let blob = new Blob([data], { type: 'octet/stream' })
|
||
|
|
||
|
let download = document.createElement('a')
|
||
|
download.download = fname
|
||
|
download.href = window.URL.createObjectURL(blob)
|
||
|
download.style.display = 'none'
|
||
|
download.onclick = function ()
|
||
|
{
|
||
|
document.body.removeChild(download)
|
||
|
}
|
||
|
document.body.appendChild(download)
|
||
|
|
||
|
download.click()
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export { FileInfo }
|