Search code examples
c#.netgoogle-drive-api

How to Zip before Dowload a file/folder using Google.Drive.apis.V3?


I have a function that download folder and subfolder/file from Google Drive to my computer. But the function only download a file/folder a time. I want to Zip it on Google Drive and then download it(Just like how Google Drive web work when you download a folder or multiple file). Is there a way to do this using Google.drive.apis.v3?

Here's my Download Folder function using C#.

public void DownloadFolder(DriveService service, string folderId, string folderPath)
{
    Directory.CreateDirectory(folderPath);
    var fileListRequest = service.Files.List();
    fileListRequest.Q = $"'{folderId}' in parents";
    fileListRequest.Fields = "files(id, name, mimeType)";
    var fileList = fileListRequest.Execute();

    // Dowload file/Folder
    foreach (var file in fileList.Files)
    {
        Container contain = new Container(file.Name, file.Id, file.MimeType, this.userName, this.location);
        listOfFiles.Add(contain);
        if (file.MimeType == "application/vnd.google-apps.folder")
        {
            // Recursive if obj(file) is a folder.
            string subfolderPath = Path.Combine(folderPath, file.Name);
            DownloadFolder(service, file.Id, subfolderPath);
        }
        else
        {
            // if obj(file) is a normal file, download.
            string filePath = Path.Combine(folderPath, file.Name);
            DownloadFile(service, file.Id, filePath);
        }
    }
}

Solution

  • The Google drive API doesn't offer that kind of functionality you need to download the files one at a time and then zip them locally