使用 curl 下载文件时确定扩展名
Determine extension when downloading a file with curl?
>我创建了一个程序,使用 curl
从互联网下载字幕如何知道我下载的文件的扩展名(.zip或.rar)?
这是我的代码(它是函数的一部分)
FILE* download=fopen("download.zip","wb");//i assume it's a zip
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_POST,1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,post_data.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
curl_easy_setopt(curl, CURLOPT_URL,url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write2file);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, download);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
std::cout<<"Download Failed "<<std::endl;
curl_easy_cleanup(curl);
fclose(download);
您可以使用
curl_easy_getinfo()
函数,它具有内容类型信息:
CURLINFO_CONTENT_TYPE
将指针传递给 char 指针以接收 下载的对象。这是从内容类型读取的值: 田。如果得到 NULL,则表示服务器未发送有效的 内容类型标头或使用的协议不支持此功能。
使用该信息,您可以通过在磁盘上重命名下载的文件来为其提供正确的扩展名。
相关文章:
- 如何按文件扩展名引用文件夹中的文件
- 为什么我无法打开/读取从 Python 调用的 C 扩展名中的 txt 文件?
- 使用Boost文件系统C++将具有特定扩展名的文件的名称保存在特定文件夹中
- 共享库 (.so) 没有扩展名的 Linux 可执行文件之间的区别?
- CMake 不编译具有非标准扩展名的文件
- Visual Studio 2019 - 设置文件类型的默认扩展名
- 如何在 Windows 上获取带有 c++ 且没有 <dirent.h> 的特定扩展名的所有文件的列表
- 谁能告诉我,程序中的错误是什么?该程序仅用于获取文件扩展名
- 如何在 vscode 扩展名中检索 C/C++ 文件的编译命令
- 通过cMake使用具有自定义文件扩展名的共享库
- 如何在 CMake 中"override"文件的按扩展名类型
- 在另一个字符串中插入文件扩展名之前的字符串
- 如何在不使用文件扩展名的情况下使用命令行参数打开C++中的文本文件?
- 使用FindFirstFile和FindNextFile递归查找具有特定扩展名的文件
- 如何在C++中打开以特定扩展名结尾的文件
- 头文件中.h扩展名的意义是什么?
- 按扩展名过滤搜索文件会返回太多结果
- 通过扩展名搜索特定文件夹Winapi32
- Eclipse CDT:传递具有相同文件扩展名的多个程序参数
- 使用 .ipp 扩展名和.cpp扩展名文件的区别