# web-async-zip

> 前端异步打包下载

Latest version **1.0.1** (published 2023-04-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install web-async-zip
pnpm add web-async-zip
yarn add web-async-zip
bun add web-async-zip
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2023-04-04 |
| First published | 2023-04-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | 上海恒时计算机信息技术有限公司北京分公司：宋印赠 |
| Maintainers | songyz |
| Keywords | web, async, zip |

## Links

- npm: https://www.npmjs.com/package/web-async-zip
- Repository: https://gitee.com/songyinzeng/web-async-zip
- npm.io page: https://npm.io/package/web-async-zip

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2023-04-04
- 1.0.0 — 2023-04-04

## README

# web-async-zip

#### 介绍
前端异步打包下载

#### 软件架构
封装 StreamSaver.js ZipStream.js


#### 安装

npm i web-async-zip

#### 使用

```
import {StreamSaver,ZIP} from "web-async-zip";

/**
 * 
 * @param { [{name:String,dir:String,url:String}] } files 
 * @param {String} zipName 
 * @returns 
 */
export function downloadBlobZip(files, zipName = '未命名.zip') {
  let arr = files.map(i => {
    return urlToBlob(i).then(f => {
      return {
        name: (f.dir ? `${f.dir}/` : "") + f.name,
        stream: () => f.blob.stream()
      };
    });
  });

  const fileStream = StreamSaver.createWriteStream(zipName)

  const readableZipStream = new ZIP({
    start(ctrl) {
      arr.forEach(item => item.then(opt => ctrl.enqueue(opt)));

      Promise.all(arr).then(() => ctrl.close()).catch(() => ctrl.close());
    }
  })

  // more optimized
  if (window.WritableStream && readableZipStream.pipeTo) {
    return readableZipStream.pipeTo(fileStream).then(() => console.log('done writing'))
  } else {
    // less optimized
    const writer = fileStream.getWriter()
    const reader = readableZipStream.getReader()
    const pump = () => reader.read()
      .then(res => res.done ? writer.close() : writer.write(res.value).then(pump))

    pump()
  }
}
```

注：urlToBlob 方法需要自己实现，即请求url获取blob逻辑。

---
_Source: https://npm.io/package/web-async-zip · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
