沈斌
2018-07-10 fec630978ad9b1ce5caff7dbc74e7d10d43a0970
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Component, OnInit } from '@angular/core';
import { ZipService } from '@delon/abc';
import * as JSZip from 'jszip';
import { NzMessageService } from 'ng-zorro-antd';
 
@Component({
  selector: 'app-zip',
  templateUrl: './zip.component.html',
})
export class ZipComponent {
  constructor(private zip: ZipService, private msg: NzMessageService) {
    this.zip.create().then(ret => (this.instance = ret));
  }
 
  // region: read
 
  list: any;
  private format(data: any) {
    const files = data.files;
    this.list = Object.keys(files).map(key => {
      return {
        name: key,
        dir: files[key].dir,
        date: files[key].date,
      };
    });
  }
 
  url() {
    this.zip.read(`./assets/tmp/demo.zip`).then(res => this.format(res));
  }
 
  change(e: Event) {
    const file = (e.target as HTMLInputElement).files[0];
    this.zip.read(file).then(res => this.format(res));
  }
 
  // endregion
 
  // region: write
 
  instance: JSZip = null;
  data: { path: string; url: string }[] = [
    { path: 'demo.docx', url: 'http://ng-alain.com/assets/demo.docx' },
    {
      path: '小程序标志.zip',
      url: 'https://wximg.gtimg.com/shake_tv/mina/standard_logo.zip',
    },
  ];
 
  download() {
    const promises: Promise<any>[] = [];
    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: any) => {
        console.warn(error);
        this.msg.error(JSON.stringify(error));
      },
    );
  }
 
  // endregion
}