UWP 将 zip 文件保存到磁盘

UWP Saving zip file to disk

我有一个用 HTML/Javascript 编写的 UWP 应用程序,我无法保存由 JSZip 制作的 zip 文件。专门将它写入磁盘是我挂断的地方。

我在 Microsoft 文档中看到有 WriteBufferAsync、WriteBytesAsync、WriteLinesAsync 和 WriteTextAsync。我不确定我需要哪个。 JSZip 还可以生成不同的类型,如 base64、binarystring、uint8array、arraybuffer 和 blob。我只是不确定我需要什么组合才能将此 zip 文件写入用户磁盘。

下面是我的代码:

savePNGButton.addEventListener('click', function (e) {
  var zip = new JSZip();
  if (WatermarkText === ""){
    ZipFolder = zip.folder("ImageFolder");
  } else {
    ZipFolder = zip.folder(WatermarkText);
  }
  $(".WatermarkPhoto").each(function(index) {
   imgsrc = this.src;
   var DataURL = imgsrc.replace('data:image/png;base64,', '');
   ZipFolder.file(WatermarkText + index + ".png", DataURL, { base64: true });

  });
  zip.generateAsync({ type:"blob"})
      .then(function (content) {
          console.log(content);
          var savePicker = new Windows.Storage.Pickers.FileSavePicker();
          savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
          savePicker.fileTypeChoices.insert("ZIP archive", [".zip"]);
          savePicker.suggestedFileName = WatermarkText+".zip";
          savePicker.pickSaveFileAsync().then(function (file) {
              if (file) {
                  Windows.Storage.CachedFileManager.deferUpdates(file);
                  Windows.Storage.FileIO.writeTextAsync(file, content).done(function () {
                      Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) {
                          if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
                              console.log("File " + file.name + " was saved.");
                          } else {
                              console.log("File " + file.name + " couldn't be saved.");
                          }
                      });
                  });
              } else {
                  console.log("Operation cancelled.");
              }
          });
      });
});

如果有人遇到这个问题,我发现这个 link 向我发送了正确的方向 https://blog.appliedis.com/2013/09/18/zipping-and-unzipping-files-in-a-winjs-application/ 我使用了来自 JSZip 的 uint8array 以及来自 windows FileIO 的文件流和 WriteBytesAsync class。下面是我用来压缩和显示文件保存对话框的最后一段代码。

savePNGButton.addEventListener('click', function (e) {
  var zip = new JSZip();
  if (WatermarkText === ""){
    ZipFolder = zip.folder("Images");
  } else {
    ZipFolder = zip.folder(WatermarkText);
  }
  $(".WatermarkPhoto").each(function(index) {
   imgsrc = this.src;
   var DataURL = imgsrc.replace('data:image/png;base64,', '');
   ZipFolder.file(WatermarkText + index + ".png", DataURL, { base64: true });

  });

  zip.generateAsync({ type: "uint8array", streamFiles:"true"})
      .then(function (content) {
          console.log(content);
              var savePicker = new Windows.Storage.Pickers.FileSavePicker();
              savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
              savePicker.fileTypeChoices.insert("ZIP archive", [".zip"]);
              savePicker.suggestedFileName = WatermarkText + ".zip";
              savePicker.pickSaveFileAsync().then(function (file) {
                  if (file) {
                      Windows.Storage.CachedFileManager.deferUpdates(file);
                      Windows.Storage.FileIO.writeBytesAsync(file, content).done(function () {
                          Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) {
                              if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
                                  console.log("File " + file.name + " was saved.");
                              } else {
                                  console.log("File " + file.name + " couldn't be saved.");
                              }
                          });
                      });
                  } else {
                      console.log("Operation cancelled.");
                  }
              });
      });
});