下载的Google云端硬盘文件损坏了C#

龟仙人掌

我正在编写一个程序来下载Google云端硬盘中的所有数据并保存。问题是,当文件被保存时,其原始大小会增加(例如,一个.xls文件的大小为287KB,另存为87411KB的大小),我无法打开它,因为它说它已损坏。大多数文件是.xls和.xlsx文件。

我遵循了教程以及Google API文档。

现在,我正在使用以下代码:

public static void GetFilesFromDrive()
        {
            UserCredential credential;

            using (var stream =
                new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Drive API service.
            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // Define parameters of request.
            FilesResource.ListRequest listRequest = service.Files.List();
            listRequest.PageSize = 400;
            listRequest.Fields = "nextPageToken, files(id, name)";

            // List files.
            IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
                .Files;
            Console.WriteLine("Files:");


            String path = copypath;
            var jetStream = new System.IO.MemoryStream();

            if (files != null && files.Count > 0)
            {
                foreach (var file in files)
                {
                    Console.WriteLine("{0} ({1})", file.Name, file.Id);
                    FilesResource.GetRequest request = new FilesResource.GetRequest(service, file.Id);
                    //ExportRequest(service, file.Id, "application/vnd.google-apps.file");
                    //(service, file.Id);
                    string pathforfiles = path + file.Name.ToString();

                    request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
                    {
                        switch (progress.Status)
                        {
                            case Google.Apis.Download.DownloadStatus.Downloading:
                                {
                                    Console.WriteLine(progress.BytesDownloaded);
                                    break;
                                }
                            case Google.Apis.Download.DownloadStatus.Completed:
                                {
                                    Console.WriteLine("Download complete.");
                                    using (System.IO.FileStream file = new System.IO.FileStream(pathforfiles, System.IO.FileMode.Create, System.IO.FileAccess.Write))
                                    {
                                        jetStream.WriteTo(file);
                                    }
                                    break;
                                }
                            case Google.Apis.Download.DownloadStatus.Failed:
                                {
                                    Console.WriteLine("Download failed.");
                                    break;
                                }
                        }
                    };

                    request.DownloadWithStatus(jetStream);

                }
            }
            else
            {
                Console.WriteLine("No files found.");
            }
            //Console.Read();
        }
戴姆敦

看来您没有搁置内存流。尝试var jetStream = new System.IO.MemoryStream();进入foreach。

if (files != null && files.Count > 0)
{
    foreach (var file in files)
    {
        var jetStream = new System.IO.MemoryStream();

        Console.WriteLine("{0} ({1})", file.Name, file.Id);
        FilesResource.GetRequest request = new FilesResource.GetRequest(service, file.Id);
        //ExportRequest(service, file.Id, "application/vnd.google-apps.file");
        //(service, file.Id);
        string pathforfiles = path + file.Name.ToString();

        request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
        {
            switch (progress.Status)
            {
                case Google.Apis.Download.DownloadStatus.Downloading:
                    {
                        Console.WriteLine(progress.BytesDownloaded);
                        break;
                    }
                case Google.Apis.Download.DownloadStatus.Completed:
                    {
                        Console.WriteLine("Download complete.");
                        using (System.IO.FileStream file = new System.IO.FileStream(pathforfiles, System.IO.FileMode.Create, System.IO.FileAccess.Write))
                        {
                            jetStream.WriteTo(file);
                        }
                        break;
                    }
                case Google.Apis.Download.DownloadStatus.Failed:
                    {
                        Console.WriteLine("Download failed.");
                        break;
                    }
            }
        };

        request.DownloadWithStatus(jetStream);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Google云端硬盘下载所选文件

从Google云端硬盘下载公共文件-Golang

从Google云端硬盘中运行C#文件

从Google云端硬盘下载文件时的下载进度状态?

使用Google Picker从Google云端硬盘下载文件

如何下载Google云端硬盘?

Python:如何从Google云端硬盘下载整个文件夹

从R中的Google云端硬盘下载xlsx文件

无法通过其ID从Google云端硬盘下载文件

如何从Chrome扩展程序下载公共Google云端硬盘文件?

使用Python从Google云端硬盘下载文件

从Android中的Google云端硬盘下载文件

将Google云端硬盘文件下载到Ubuntu终端

使用VBA从Excel Sheet下载Google云端硬盘文件

Google云端硬盘不允许我下载文件

Google云端硬盘API-使用JSZip获取文件和zip返回损坏的文件

Google 云端硬盘 - 同时下载

Google云端硬盘API下载jpg

C#文件下载已损坏

用 C# 下载的 RAR 文件损坏

C#Google云端硬盘APIv3上传文件

从共享的Google云端硬盘文件夹下载所有文件夹

使用google-api-ruby-client从Google云端硬盘下载文件

如何使用Google colab将文件下载到Google云端硬盘?

尝试从Google云端硬盘下载文件时禁止出现错误403

我正在尝试使用Google云端硬盘API获取文件的直接下载链接

如何将从Google云端硬盘下载的数据保存为文件-Python Drive API

使用Google云端硬盘API从文件夹下载电子表格

使用Java SDK API从Google云端硬盘下载文件时出现内存不足错误