javascript - 如何使用 Google Drive Javascript API 更新 Google Drive 中的 zip 文件

标签 javascript google-drive-api zip

我对文件压缩和 google drive api 完全没有经验。在我的项目中,我使用 zip.js和谷歌驱动器 API。 zip.js 运行良好,因为我直接下载生成的 zip 文件并且我的设备可以解压缩它。尽管我没有向 zip 的内容添加任何额外的数据,但 zip 文件的大小发生了变化(加倍),并且在更新时损坏了。

我生成 zip 文件的函数:

function createZip(data,fileName,callback){
    var blob = new Blob([ data ], {
      type : "text/xml"
    });

    zipBlob(fileName, blob, function(zippedBlob){
        console.log(zippedBlob)
        // saveAs(zippedBlob,"test.zip") //this test.zip is a valid file.
        var reader = new FileReader();
        reader.addEventListener("loadend", function() {
           // reader.result contains the contents of blob as a typed array
           console.log(reader.result);
           callback(reader.result);
        });
        reader.readAsText(zippedBlob)
    })


    function zipBlob(filename, blob, callback) {
      zip.createWriter(new zip.BlobWriter("application/zip"), function(zipWriter) {
        zipWriter.add(filename, new zip.BlobReader(blob), function() {
          zipWriter.close(callback);
        });
      }, function(error){
        console.error(error)
      });
    }
}

我更新文件的函数:

this.updateFile = function(id, text, accessToken, callback) {
    var boundary = '-------314159265358979323846',
        delimiter = "\r\n--" + boundary + "\r\n",
        close_delim = "\r\n--" + boundary + "--",
        mimeType = 'application/zip';

    var multipartRequestBody =
        delimiter + 'Content-Type: application/json\r\n\r\n' +
        delimiter + 'Content-Type:' + mimeType + '\r\n\r\n' +
        text +
        close_delim;

    gapi.client.request({
        'path': '/upload/drive/v3/files/'+id,
        'method': 'PATCH',
        'params': { 'uploadType': 'multipart' },
        'headers': { 'Content-Type': 'multipart/mixed; boundary="' + boundary + '"', 'Authorization': 'Bearer ' + accessToken, },
        'body': multipartRequestBody
    }).execute(function(file) {
        if (typeof callback === "function") callback(file);
    }, function(error) {
        console.error(error)
        callback(error);
    });
}

我想知道 FileReader 是否损坏了内容,因为 reader.result 与 zip 的原始内容不同。我使用这个函数来读取文件内容:

this.readFile = function(fileId, callback) {
    var request = gapi.client.drive.files.get({
        fileId: fileId,
        alt: 'media',
        Range : 'bytes=100-200'
    })
    request.then(function(response) {
        console.log(response);
        if (typeof callback === "function") callback(response.body);
    }, function(error) {
        console.error(error)
    })
    //return request;
}

zip 的原始内容为文本:

PKÊL    PV-A2.xmlUXñçûZÂÌZùì½m,I÷]þC£?I@ÌNøK¼=F °KÃ(­«Ýgz=Crçß+²ne§y¸¥U6... (some thousands characters more)

文件阅读器内容为文本:

PK�U�L  PV-A2.xml�]�<��w�O1�+(���� �@�6`@+  �Ґ`��r��EÑ�������̊s"���0�..(some thousands characters more)

解决方案

从zip文件中获取base64数据(记得去掉data:application/zip;base64,):

    // saveAs(zippedBlob,"test.zip") //this test.zip is a valid file.
    var reader = new FileReader();
    reader.addEventListener("loadend", function() {
       // reader.result contains the contents of blob as a typed array
       callback(reader.result.replace("data:application/zip;base64,",""));
    });
    reader.readAsDataURL(zippedBlob)

Content-Transfer-Encoding: base64 添加到 multipartRequestBody 并改用 base64 内容。

var multipartRequestBody =
    delimiter + 'Content-Type: application/json\r\n\r\n' +
    delimiter + 'Content-Type:' + mimeType + '\r\n' +
    'Content-Transfer-Encoding: base64\r\n\r\n' +
    base64Data +
    close_delim;

最佳答案

如果text是转成base64的zip文件,这个修改怎么样?我认为您的脚本几乎是正确的。作为修改,它使用Content-Transfer-Encoding 给出了文件的编码方法。

发件人:

delimiter + 'Content-Type:' + mimeType + '\r\n\r\n' +
text +

收件人:

delimiter + 'Content-Type: ' + mimeType + '\r\n' + // Modified
'Content-Transfer-Encoding: base64\r\n\r\n' + // Added
text +

注意事项:

  • 在我的环境中,我确认使用您的脚本会出现与您的情况相同的错误。我还确认,当此修改反射(reflect)到您的脚本中时,zip 文件已更新,并且可以解压缩文件。

但如果这不能解决您的情况,我很抱歉。

关于javascript - 如何使用 Google Drive Javascript API 更新 Google Drive 中的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50385786/

相关文章:

javascript - jQuery 模糊地换行。如何获得正确的html结果结构?

javascript - 如何在 OrientDB 中使用创建顶点创建边

javascript - 如何使 while 循环真正异步

google-docs - Google Apps 域 : drive migrator

android - 如何在电子表格中获取应用程序脚本的 ID

java - 在 ZIP 中搜索具有散列名称的 XML 文件的有效方法

c# - 使用 C# 压缩单个文件

javascript - 无法修改 CustomEvent 数据

qt - 如何使用 v3 Rest api 从 Google Drive 下载文件?

用于大量文件的 Java zip 库