How to use the loadAsync function from jszip

Find comprehensive JavaScript jszip.loadAsync code examples handpicked from public code repositorys.

jszip.loadAsync is a function that loads a ZIP archive asynchronously and returns a JSZip object that can be used to read, modify, or create the contents of the archive.

173
174
175
176
177
178
179
180
181
182
}

const buffer = Buffer.from(JSON.parse(fs.readFileSync(`${__dirname}/admin-config/vsFont/codicon.json`)));

// this is workaround for TTF file. somehow it will always corrupt, so we pack it into ZIP
JSZip.loadAsync(buffer)
    .then(zip => {
        zip.file('codicon.ttf').async('arraybuffer')
            .then(data => {
                if (!fs.existsSync(`${__dirname}/admin/vs/base/browser/ui/codicons/codicon/`)) {
fork icon116
star icon294
watch icon30

+ 12 other calls in file

640
641
642
643
644
645
646
647
648
649
  .map(id => `${id}.png`)
  .sort();
const needPackageItemImgs = await (async () => {
  if (!Fse.existsSync(ITEM_PKG_ZIP)) return true;
  try {
    const itemImgPkg = await JSZip.loadAsync(Fse.readFileSync(ITEM_PKG_ZIP));
    const curPackagedItemImgs = _.map(
      itemImgPkg.filter(filename => isItem(Path.parse(filename).name)),
      'name',
    ).sort();
fork icon73
star icon526
watch icon8

+ 3 other calls in file

How does jszip.loadAsync work?

jszip.loadAsync is a function provided by the JSZip library that loads a ZIP archive asynchronously and returns a JSZip object that can be used to read, modify, or create the contents of the archive. To load a ZIP archive, you can call the loadAsync function and pass in the binary data of the archive as a Blob, Buffer, ArrayBuffer, or Uint8Array object. The loadAsync function will then read the binary data of the archive and create a JSZip object that represents the contents of the archive. Once you have a JSZip object, you can use its various methods to read, modify, or create the contents of the archive. For example, you can use the file method to get a file in the archive by its name, or use the generateAsync method to generate a new ZIP archive from the modified contents. The jszip.loadAsync function is useful for working with ZIP archives in JavaScript, such as extracting files from an archive or creating a new archive with modified files.

50
51
52
53
54
55
56
57
58
59
}

let zipPath = server.writePath("mods", `${mod}_${modVersion}.zip`);
let zip = zipCache.get(zipPath);
if (!zip) {
	zip = await JSZip.loadAsync(await fs.readFile(zipPath));
	zipCache.set(zipPath, zip);
}

return zip.folder(libZipOps.findRoot(zip));
fork icon57
star icon227
watch icon13

166
167
168
169
170
171
172
173
174
175
    fs.readFile(zipfile, (err, buffer) => {
      if (err) {
        console.error(err)
        reject(err)
      } else {
        JSZip.loadAsync(buffer).then(resolve, reject)
      }
    })
  })
}
fork icon11
star icon27
watch icon2

+ 4 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const JSZip = require("jszip");
const axios = require("axios");

async function loadZipArchive(url) {
  const response = await axios.get(url, { responseType: "arraybuffer" });
  const zipData = response.data;
  const zip = await JSZip.loadAsync(zipData);

  console.log(zip.file("myFile.txt").async("string"));
}

loadZipArchive("https://example.com/myZipArchive.zip");

In this example, we use the jszip and axios libraries to load a ZIP archive from a remote URL and extract the contents of a file in the archive. We first use the axios library to send a GET request to the URL and receive the binary data of the ZIP archive in arraybuffer format. We then pass the binary data to the JSZip.loadAsync function, which loads the archive asynchronously and creates a JSZip object that represents the contents of the archive. We use the zip.file method to get a file in the archive by its name (myFile.txt) and then use the async method to extract the contents of the file as a string. Finally, we log the contents of the file to the console. This example demonstrates how you can use jszip.loadAsync to load a ZIP archive asynchronously and extract the contents of files in the archive.

90
91
92
93
94
95
96
97
98
expect(Object.keys(files)).toEqual(expect.arrayContaining(pkgs))

// 2. standalone folder has appIcon
const iconPath = 'Common/logo.png'
const standaloneSrpkBuf = zip.file('com.app.subpackage.standalone.srpk').async('nodebuffer')
const standaloneSrpkZip = await JSZip.loadAsync(standaloneSrpkBuf)
const standaloneSrpkFiles = standaloneSrpkZip.files

expect(Object.keys(standaloneSrpkFiles)).toContain(iconPath)
fork icon11
star icon27
watch icon2

+ 19 other calls in file

98
99
100
101
102
103
104
105
106
107
const previewRpkOutput = `${tempPreviewPath}/${manifest.package}.__preview__.debug.${manifest.versionName}.rpk`
const err = fs.accessSync(previewRpkOutput, fs.constants.F_OK)
expect(err).toBe(undefined)
// 2、输出文件内的包名和name是否被正确替换
const outputRpkBuf = fs.readFileSync(previewRpkOutput)
const outputRpkZip = await JSZip.loadAsync(outputRpkBuf)
const manifestJSON = await outputRpkZip.file('manifest.json').async('string')
const pkgNameExistResult =
  JSON.parse(manifestJSON).package.indexOf(previewPackageSuffix) !== -1
const nameExistResult = JSON.parse(manifestJSON).name === userHash
fork icon11
star icon27
watch icon2

+ 2 other calls in file

108
109
110
111
112
113
114
115
116
117

  for (let i = 0; i < pkgs.length; i++) {
    // 获取 rpks 里面的各个 rpk/srpk 的 buffer
    const pkgBuf = rpksZipBuf.file(pkgs[i].name).async('nodebuffer')
    // 获取 rpk/srpk 里的各项文件 zip buffer
    const pkgZipBuf = await JSZip.loadAsync(pkgBuf)
    cb(pkgZipBuf, pkgs[i].hasi18n)
  }
}
afterAll(async () => {
fork icon11
star icon27
watch icon2

+ 3 other calls in file

172
173
174
175
176
177
178
179
180
181

static unzipAskueOld(dir, zipName, onUnzip, onError) {
    const zipPath = path.join(dir, zipName);
    fs.readFile(zipPath, function (err, data) {
        if (!err) {
            JSZip.loadAsync(data).then(function (zip) {
                if (Object.keys(zip.files).length > 0) {
                    const key = zip.files[Object.keys(zip.files)[0]];
                    const unzipPath = path.join(dir, key.name);
                    zip.file(key.name)
fork icon0
star icon0
watch icon1

+ 8 other calls in file