You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何处理Google Drive批量导出Workspace文档HTML(application/zip)响应体?

解决Google Drive批量导出HTML格式(ZIP)的响应体处理问题

一、批量响应体正确拆分方式

Google Drive批量请求的响应是多部分混合内容(multipart/mixed),拆分时不能像纯文本那样简单按标记切割——ZIP是二进制数据,直接用字符串拆分会破坏字节结构,导致解压失败。正确步骤:

  1. 从响应头的Content-Type字段中提取分隔符(boundary),格式类似----batch_xxxxxxxxxxxx
  2. 按二进制边界拆分响应体,跳过开头的边界线和结尾的结束边界
  3. 每个子响应需分离HTTP头部和二进制内容:
    • 找到子响应中\r\n\r\n的位置,前面是HTTP头部,后面是ZIP的原始二进制数据
    • 提取二进制部分时必须保留原始字节,不能转成字符串处理

二、能否仅导出HTML内容而非完整ZIP?

可以直接指定导出纯HTML格式,无需走ZIP压缩。对应的MIME类型是text/html,而非application/zip。这样批量请求的响应体处理逻辑和你之前处理纯文本的方式完全一致,无需处理二进制ZIP,更简单高效。

三、代码示例

Google Apps Script 示例(批量导出纯HTML)

function batchExportHTML() {
  const fileIds = ["文件ID1", "文件ID2", "..."]; // 替换为你的100个文档ID
  const boundary = "batch_" + Utilities.getUuid();
  const requests = fileIds.map(id => {
    return `--${boundary}
Content-Type: application/http
Content-Transfer-Encoding: binary

GET /drive/v3/files/${id}/export?mimeType=text/html HTTP/1.1

`;
  }).join("") + `--${boundary}--`;

  const options = {
    method: "POST",
    contentType: `multipart/mixed; boundary=${boundary}`,
    headers: {Authorization: `Bearer ${ScriptApp.getOAuthToken()}`},
    payload: requests,
    muteHttpExceptions: true
  };

  const response = UrlFetchApp.fetch("https://www.googleapis.com/batch/drive/v3", options);
  const responseBody = response.getContentText();
  const parts = responseBody.split(`--${boundary}`).filter(part => part.trim() !== "" && !part.includes("--"));

  parts.forEach((part, index) => {
    const htmlStart = part.indexOf("\r\n\r\n") + 4;
    const htmlContent = part.slice(htmlStart);
    // 处理HTML内容,比如保存到Google Drive
    DriveApp.createFile(`文档${index+1}.html`, htmlContent, MimeType.HTML);
  });
}

Elixir 示例(处理批量ZIP响应)

defmodule DriveBatch do
  def process_batch_zip_response(response) do
    # 从响应头提取boundary
    boundary = response.headers
    |> Enum.find(fn {k, _} -> String.downcase(k) == "content-type" end)
    |> elem(1)
    |> String.split("boundary=")
    |> List.last()
    |> then(&"--#{&1}")

    # 按二进制拆分响应体并处理每个ZIP包
    response.body
    |> String.split(boundary)
    |> Enum.filter(fn part -> String.trim(part) != "" and not String.ends_with?(part, "--") end)
    |> Enum.each(fn part ->
      # 分离HTTP头部和ZIP二进制数据
      [_header, zip_data] = String.split(part, "\r\n\r\n", parts: 2)
      # 去除结尾多余换行
      zip_data = String.trim_trailing(zip_data, "\r\n")
      # 保存为ZIP文件,后续可解压提取HTML
      File.write!("output_#{System.unique_integer()}.zip", zip_data, [:binary])
    end)
  end
end

# 使用示例(需提前构造带授权的批量请求)
# response = HTTPoison.post!("https://www.googleapis.com/batch/drive/v3", batch_request, auth_headers)
# DriveBatch.process_batch_zip_response(response)

四、注意事项

  • 如果坚持导出ZIP格式,必须用二进制方式处理响应体,绝对不能转成字符串后再拆分,否则会破坏ZIP的字节结构导致解压失败
  • 使用text/html导出纯HTML时,响应为文本格式,处理逻辑和纯文本完全一致,是更推荐的方案

内容的提问来源于stack exchange,提问作者David Tew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 08:13:27