zip Zip 操作

一个基于 jszip 的Zip文件操作。

依赖

  1. yarn add file-saver

由于 jszip 脚本大小以及对 Zip 的操作并不是刚需的原因,因此采用一种延迟加载脚本的形式,可以通过全局配置配置来改变默认 CDN 路径(或使用本地路径),默认情况下使用 //cdn.bootcss.com/jszip/3.3.0/jszip.min.js

  1. import { ZipModule } from '@delon/abc/zip';

代码演示

zip Zip 操作 - 图1

解压

读取Zip文件信息(含内容),支持 File、URL 形式

  1. import { Component } from '@angular/core';
  2. import { ZipService } from '@delon/abc/zip';
  3. @Component({
  4. selector: 'components-zip-read',
  5. template: `
  6. <button nz-button (click)="url()">Via Url</button>
  7. <input type="file" (change)="change($event)" multiple="false" class="ml-sm" />
  8. <ol>
  9. <li *ngFor="let i of data">{{i | json}}</li>
  10. </ol>
  11. `
  12. })
  13. export class ComponentsZipReadComponent {
  14. constructor(private zip: ZipService) {}
  15. data: any;
  16. private format(data: any) {
  17. const files = data.files;
  18. this.data = Object.keys(files).map(key => {
  19. return {
  20. name: key,
  21. dir: files[key].dir,
  22. date: files[key].date
  23. };
  24. });
  25. }
  26. url() {
  27. this.zip.read(`./assets/demo.zip`).then(res => this.format(res));
  28. }
  29. change(e: Event) {
  30. const file = (e.target as HTMLInputElement).files![0];
  31. this.zip.read(file).then(res => this.format(res));
  32. }
  33. }

zip Zip 操作 - 图2

压缩

通过 pushUrl 可以快速将URL资源写入 Zip 实例。

  1. import { NzMessageService } from 'ng-zorro-antd/message';
  2. import { Component } from '@angular/core';
  3. import { ZipService } from '@delon/abc/zip';
  4. import * as JSZip from 'jszip';
  5. @Component({
  6. selector: 'components-zip-save',
  7. template: `
  8. <div *ngIf="instance">
  9. <button nz-button (click)="add()" [nzType]="'primary'">new</button>
  10. <button nz-button (click)="download()" class="ml-sm">download</button>
  11. <nz-table [nzData]="data" [nzFrontPagination]="false" [nzShowPagination]="false" class="mt-sm">
  12. <thead>
  13. <tr>
  14. <th><span>path</span></th>
  15. <th><span>url</span></th>
  16. </tr>
  17. </thead>
  18. <tbody>
  19. <tr *ngFor="let i of data; let index = index">
  20. <td><input nz-input [(ngModel)]="i.path" name="path{{index}}"></td>
  21. <td><input nz-input [(ngModel)]="i.url" name="url{{index}}"></td>
  22. </tr>
  23. </tbody>
  24. </nz-table>
  25. </div>
  26. `
  27. })
  28. export class ComponentsZipSaveComponent {
  29. instance: JSZip | null = null;
  30. data: { path: string, url: string }[] = [
  31. { path: 'demo.docx', url: 'https://ng-alain.com/assets/demo.docx' },
  32. { path: 'img/zorro.svg', url: 'https://ng.ant.design/assets/img/zorro.svg' },
  33. { path: '小程序标志.zip', url: 'https://wximg.gtimg.com/shake_tv/mina/standard_logo.zip' }
  34. ];
  35. constructor(private zip: ZipService, private msg: NzMessageService) {
  36. this.zip.create().then(ret => this.instance = ret);
  37. }
  38. add() {
  39. this.data.push({ path: '', url: '' });
  40. }
  41. download() {
  42. const promises: Promise<any>[] = [];
  43. this.data.forEach(item => {
  44. promises.push(this.zip.pushUrl(this.instance, item.path, item.url));
  45. });
  46. Promise.all(promises).then(() => {
  47. this.zip.save(this.instance).then(() => {
  48. this.msg.success('download success');
  49. this.data = [];
  50. });
  51. }, (error: any) => {
  52. console.warn(error);
  53. this.msg.error(JSON.stringify(error));
  54. });
  55. }
  56. }

API

ZipService

成员说明类型默认值
read(fileOrUrl: File | string, options?: JSZip.JSZipLoadOptions)解压Promise<JSZip>-
create()创建 Zip 实例,用于创建压缩文件Promise<JSZip>-
pushUrl(zip: JSZip, path: string, url: string)下载URL资源并写入 zipPromise<void>-
save(zip: JSZip, options?: ZipWriteOptions)保存ZipPromise<void>-