ohayolibs
Version:
Ohayo is a set of essential modules for ohayojp.
73 lines (66 loc) • 2.13 kB
Markdown
---
order: 2
title: 压缩
---
通过 `pushUrl` 可以快速将URL资源写入 Zip 实例。
```ts
import { Component } from '@angular/core';
import { ZipService } from '@ohayo/components/zip';
import * as JSZip from 'jszip';
import { NzMessageService } from 'ng-zorro-antd/message';
export class DemoComponent {
instance: JSZip | null = null;
data: { path: string; url: string }[] = [
{ path: 'demo.docx', url: 'https://ohayojp.com/assets/demo.docx' },
{ path: 'img/zorro.svg', url: 'https://ng.ant.design/assets/img/zorro.svg' },
{ path: '小程序标志.zip', url: 'https://wximg.gtimg.com/shake_tv/mina/standard_logo.zip' },
];
constructor(private zip: ZipService, private msg: NzMessageService) {
this.zip.create().then(ret => (this.instance = ret));
}
add(): void {
this.data.push({ path: '', url: '' });
}
download(): void {
const promises: Promise<void>[] = [];
this.data.forEach(item => {
promises.push(this.zip.pushUrl(this.instance, item.path, item.url));
});
Promise.all(promises).then(
() => {
this.zip.save(this.instance).then(() => {
this.msg.success('download success');
this.data = [];
});
},
(error: {}) => {
console.warn(error);
this.msg.error(JSON.stringify(error));
},
);
}
}
```