jszip

前端zip文件的压缩与解压
更新于: 2023-05-11 17:19:58

使用

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>ZipJS</title>
    <script src="https://stuk.github.io/jszip/dist/jszip.js"></script>
  </head>
  <body>
    <button id="down">Download</button>
    <script>
      const btnDown = document.getElementById('down');
      btnDown.addEventListener('click', () => {
        fetch('http://localhost:3000/result2.zip')
          .then((res) => res.blob())
          .then(JSZip.loadAsync)
          .then(function (zip) {
            // get file list:
            console.log(zip, Object.keys(zip.files));

            // read file content:
            zip
              .file('6ilw01_1020_17347_rank1/Input/6ilw.pdb')
              .async('string')
              .then(function (data) {
                // console.log(data);
              });
          });
      });
    </script>
  </body>
</html>

将内容转化为 url

因为目前项目中的组件,接收的是 URL,为了统一,用这种试完成。

zip
  .file('6ilw01_1020_17347_rank1示例/6moa_ligand.sdf')
  .async('blob')
  .then(function (data) {
    const url = URL.createObjectURL(data);
    console.log(url);
  });

参考