How to compress files in google drive?

折月煮酒 提交于 2020-05-16 11:25:33

问题


Is there any way to compress large files into smaller .zip of .tar.gz files in google drive? I tried google appscript but it created .zip files but not compressing. How to do this?


回答1:


I have tried and tested the below code with a pdf file which is in my Drive of 10MB size and it did compressed it for 9MB. I even tested for a Drive file(slides), it did compress it.

function fileZip(){
 var files = DriveApp.getFilesByName('Google_Apps_Script_Second_Edition.pdf');
  while (files.hasNext()) {
    var file = files.next();
 file = file.getBlob()}

DriveApp.createFile(Utilities.zip([file], 'tutorialFiles.zip'));

}

Hope that helps!




回答2:


Actually it's even easier than that. Files are already Blobs (anything that has getBlob() can be passed in to any function that expects Blobs). So the code looks like this:

var folder = DocsList.getFolder('path/to/folder');
folder.createFile(Utilities.zip(folder.getFiles(), 'newFiles.zip'));

Additionally, it won't work if you have multiple files with the same name in the Folder... Google Drive folders support that, but Zip files do not.

To make this work with multiple files that have the same name:

var folder = DocsList.getFolder('path/to/folder');
var names = {};
folder.createFile(Utilities.zip(folder.getFiles().map(function(f){
  var n = f.getName();
  while (names[n]) { n = '_' + n }
  names[n] = true;
  return f.getBlob().setName(n);
}), 'newFiles.zip'));

(Source)




回答3:


I found there is an efficient, fast, simple but indirect method of zipping/unzipping the large files into Google Drive using Google Colaboratory.

For example, Let us assume there is a directory named Dataset of size 5 GB in your Google Drive, the task is to zip (compress) it to Dataset.zip.

Google Drive:

My Drive

Dataset

Steps:

  1. Open Google Colab (it's free), Create a New Notebook.
  2. Mount Google drive into Google Colab by running the following command lines:

    from google.colab import drive drive.mount('/content/gdrive')

    Note: Now on the left side (Files) you can observe your My Drive inside gdrive and folder Dataset inside My Drive.

Screenshot before zipping.

Colab Drive Mount

  1. Change the current directory by following command run.

    cd gdrive/My Drive/

  2. Zip files/folder: Dataset to Dataset.zip using the following command run.

    !zip -r Dataset.zip Dataset/

Screenshots after zipping:

Colab Drive Mount after zipping

Google Drive Screenshot after zipping

Note: The above commands are inspired by ubuntu. Hence, for unzipping use:

!unzip filename.zip -d ./filename



来源:https://stackoverflow.com/questions/27812997/how-to-compress-files-in-google-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!