首页 > 解决方案 > 是否可以使用 Google Drive API 从共享的 .zip 文件中获取文件

问题描述

假设以下 .zip 文件:

unzip -l myarchive.zip 
Archive:  myarchive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     3663  1980-00-00 00:00   sub_dir1/file1.txt
     4573  1980-00-00 00:00   sub_dir1/file2.txt
     6021  1980-00-00 00:00   sub_dir2/file1.txt
     6627  1980-00-00 00:00   file1.txt

以下命令在文件系统中时从 .zip 文件中提取文件 sub_dir1/file1.txt。

unzip -p myarchive.zip sub_dir1/file1.txt > file1.txt

但如果 .zip 文件在 Google Drive 中并带有共享链接(例如 fileId 为:1234567...v4rzj),

是否可以进行 Google Drive API 查询以从 .zip 文件中获取特定文件(例如 sub_dir1/file1.txt)?

标签: google-drive-apigoogle-drive-shared-drive

解决方案


谷歌驱动器只是一个文件存储系统,它本身并不能以这种方式解压缩文件或检查文件的内容。google drive api 让您能够创建、更新、删除上传和下载文件。

其他选项。

因为您的 unzip 命令适用于本地存储在您机器上的文件。您需要先从 Google 驱动器下载文件,然后运行解压缩。

由于您没有提到您打算使用哪种编程语言,我建议您查看文档以获取示例

这是一个使用 Java 的示例,您还需要授权码。

String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
    .executeMediaAndDownloadTo(outputStream);

推荐阅读