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

富文

我对文件压缩和Google Drive API完全没有经验。在我的项目中,我使用的是zip.js和Google Drive 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;
Tanaike

如果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 +

注意 :

  • 在我的环境中,我确认使用脚本会发生与您的情况相同的错误。我还确认,当此修改反映到您的脚本时,该zip文件将更新,并且该文件可以解压缩。

但是,如果这不能解决您的情况,对不起。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 pydrive 或 google drive api 在 google drive 中复制文件

如何使用 Google Drive API Javascript 列出特定文件夹中的更改

如何使用Google Drive API上传文件?

是否可以将 SQLite 文件放入 Google Drive 并使用 Google Drive API 更新其数据?

如何使用带有 CORS 或 API 凭据的 javascript 共享来自 Google Drive 的 mp3 文件

Google Drive API:如何从Google Drive下载文件?

如何使用 Google Drive API 上传我的 Drive (PHP) 中的文件?

将Google Drive API与Java结合使用,如何仅列出Drive Section / Folder中的文件

如何使用Google的Python Drive Client API在Team Drive中创建文件夹?

使用Google Drive API在指定的Google Drive中上传文件

将文件夹下载为Zip Google Drive API

使用Google Drive API上传文件夹中的文件

如何通过 Javascript Google Drive API 登录特定的 Google Drive 帐户

是否可以使用Google Drive API列出Google Drive中指定目录中的文件?

使用Google Drive API上传PDF文件-Google Drive Web Client中没有预览

Google Drive API附加文件?

Google Drive API文件搜索

如何使用Java中的Google Drive API v3获取上载文件到Google Drive的可共享链接

如何使用Google Drive API将文件上传到Google Drive?

如何使用Google Drive Java API v3在Google Drive上复制文件?

如何使用google drive api获取google drive中所有文件的名称?

如何使用PHP Google Drive API在Google Drive上上传文件

如何使用python google drive api将文件夹上传到共享的google drive?

如何使用 Google Drive API for python 与多个用户共享来自 Google Drive 的文件?

如何在 google drive api 中复制文件夹?

如何在Google Drive REST API中查看文件的权限?

Google Drive API不会更新/创建/删除任何文件

Google Drive API PHP-更新txt文件的内容

Python 3 Google Drive API | 更新文件错误