prashanth-g
prashanth-g

Reputation: 1233

How to compress files in google drive?

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?

Upvotes: 8

Views: 37834

Answers (4)

max2005b
max2005b

Reputation: 373

Use Google Drive compression services: compress.my , multcloud etc.

It's the best way to go (fastest, safest) - the result isn't a new zip but the same files compressed.

Upvotes: 0

Bharat Giddwani
Bharat Giddwani

Reputation: 311

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

Upvotes: 27

Subileau Matthias
Subileau Matthias

Reputation: 9

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)

Upvotes: 1

KRR
KRR

Reputation: 4917

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!

Upvotes: 3

Related Questions