LoginSignup
0
0

More than 5 years have passed since last update.

C gzip圧縮 httpサーバー送信

Last updated at Posted at 2018-06-20

#include <stdio.h>
#include <string.h>
#include <zlib.h>

int compress_gzfile_puts(const char* filename, char* source)
{
    gzFile zp;

    zp = gzopen(filename, "w9");
    if(zp == NULL){
        fprintf(stderr, "gzopen error\n");
        return -1;
    }

    gzwrite(zp, source, strlen(source));

    gzclose(zp);

    return 0;
}

static bool httpUploadGzipFile(const char* filename, char* source)
{
    CURLcode res;
    long statusCode= -1;
    struct stat file_info;
    FILE *fd;

    //gzip圧縮
    compress_gzfile_puts(filename, source);

    //open file to upload
  fd = fopen(filename, "rb");
    //can't continue
    if(!fd) {
    return false;
    }
  //to get the file size
  if(fstat(fileno(fd), &file_info) != 0) {
    return false;
    }

    //Post
    curl_easy_setopt(m_curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(m_curl, CURLOPT_POST, 1);

    /*圧縮じゃない方法でデータをサーバー送る
    curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, postthis);
    curl_easy_setopt(m_curl, CURLOPT_POSTFIELDSIZE, strlen(postthis));
    */

    //tell it to "upload" to the URL
  curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);

  //set where to read from (on Windows you need to use READFUNCTION too)
  curl_easy_setopt(m_curl, CURLOPT_READDATA, fd);

  //and give the size of the upload (optional)
  curl_easy_setopt(m_curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);

    //Set the default value: strict certificate check please
    curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYPEER, 0L);

    //実行
    curl_easy_perform(m_curl);

  res = curl_easy_getinfo( m_curl, CURLINFO_RESPONSE_CODE, &statusCode);
    if(res != CURLE_OK) {
    return false;
  }

    return true;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0