Skip to content

Instantly share code, notes, and snippets.

@tsertkov
Created February 17, 2015 15:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsertkov/b29b9adfd6b9a50a2e44 to your computer and use it in GitHub Desktop.
Save tsertkov/b29b9adfd6b9a50a2e44 to your computer and use it in GitHub Desktop.
Google Apps Script save fileBlob splitting it by chunks of given size
/**
* Save file splitting it by chunks of 9M
* @param {Blob} FileBlob blob data to save
* @param {Folder} folder destination folder
* @param {Number} chunkSize
* @return {String}
*/
function saveFileByChunks(fileBlob, folder, chunkSize) {
var
fileName = new Date().getTime(),
fileBytes = fileBlob.getBytes(),
fileSize = fileBytes.length,
chunksCount = Math.ceil(fileSize / chunkSize),
chunkBlob, chunkBytes, file,
i = 0;
if (!fileSize) {
// should not get here in first place :-)
return '';
}
if (chunksCount) {
do {
i++;
chunkBlob = fileBlob
.setBytes(fileBytes.splice(0, chunkSize))
.setName(fileName + '-chunk-' + i + 'of' + chunksCount);
file = folder.createFile(chunkBlob);
} while (fileBytes.length);
} else {
fileBlob.setName(fileName);
file = folder.createFile(fileBlob);
}
return fileName;
}
@machinefriendly
Copy link

The issue is that when the file is big than 100M, blob methods don't work but throw an error:Zip file content item exceeds limit of 100MB.
But I cannot see official doc mentioning this limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment