我可以使用apps脚本在Google云端硬盘中解压缩文件吗? [英] Can I ungzip files in Google Drive with apps script?

查看:1178
本文介绍了我可以使用apps脚本在Google云端硬盘中解压缩文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此主题的后续行动。



我试图使用@ July.Tech ,但我一直在收到未知的压缩方法错误或错误的标题检查错误。当使用两种不同的gzip方法创建压缩文件时出现错误,所以我认为该文件被正确地压缩。

有什么建议吗? (我的输入文件是gzip,所以我不能使用Utilities.unzip()。)



以下是整个代码:

  reports_folder_id ='xxxxx'; //保存gzipped csv报告的文件夹ID 
report_name ='xxxxxx.gz'; // gzipped CSV文件的名称

函数importData(){
var fSource = DriveApp.getFolderById(reports_folder_id);
var fi = fSource.getFilesByName(report_name); //最新的报告文件

eval(UrlFetchApp.fetch('https://cdn.rawgit.com/nodeca/pako/master/dist/pako.js').getContentText());如果(fi.hasNext()){//如果报告文件夹中存在report_name文件,则继续处理


var file = fi.next();

var charData = file.getBlob()。getDataAsString(); //使用.getBytes()时出现同样的错误

var binData = []; (var i = 0; i< charData.length; i ++){
binData.push(charData [i] <0?charData [i] + 256:charData [i])的
;
}

var data = pako.ungzip(binData); //我得到相同的错误pako.inflate(binData);

var decoded ='';
for(var i = 0; i< data.length; i ++){
decode + = String.fromCharCode(data [i]);
}

}
}

如果否建议如何解决上述问题,关于如何以编程方式解压gDrive文件的任何想法?



谢谢。

解决方案

截至2018年1月19日(查看发布说明

a>)Apps脚本现在支持使用以下实用程序方法访问的gzip压缩:


This is a follow-up to this thread.

I am trying to use the code provided by @July.Tech there, but I keep getting unknown compression method error or incorrect header check error. The error came up when either of two different gzip methods were used to create the compressed file, so I would think the file is correctly gzipped.

Any suggestions? (My input file is gzipped so I cannot use Utilities.unzip().)

Here is the entire code:

reports_folder_id = 'xxxxx'; //id of folder where gzipped csv reports are saved
report_name = 'xxxxxx.gz'; // name of gzipped CSV file

function importData() { 
  var fSource = DriveApp.getFolderById(reports_folder_id);
  var fi = fSource.getFilesByName(report_name); // latest report file

  eval(UrlFetchApp.fetch('https://cdn.rawgit.com/nodeca/pako/master/dist/pako.js').getContentText());

  if ( fi.hasNext() ) { // proceed if report_name file exists in the reports folder
    var file = fi.next();

    var charData = file.getBlob().getDataAsString(); // same error if .getBytes() is used

    var binData = [];
    for (var i = 0; i < charData.length; i++) {
      binData.push(charData[i] < 0 ? charData[i] + 256 : charData[i]);
    }

    var data = pako.ungzip(binData); // I get same error for pako.inflate(binData);

    var decoded = '';
    for (var i = 0; i < data.length; i++) {
      decoded += String.fromCharCode(data[i]);
    }

  }
}

If no suggestion for fixing the above, any ideas on how to ungzip a gDrive file programatically?

Thanks.

解决方案

As of January 19, 2018 (see release notes) Apps Script now supports gzip compression accessible using the following Utilities methods:

这篇关于我可以使用apps脚本在Google云端硬盘中解压缩文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆