2012-08-24 43 views
3

所有,使用zlib的創建gzip文件使用zpipe.c例如

我只是試圖讓zpipe演示使用工作DEV-C++,我有所有的zlib的代碼導入並使用zpipe.c作爲一個例子。一切都編譯並運行。如果我嘗試使用註釋掉的deflateInit2創建一個gzip文件,它現在會創建錯誤,但在使用7zip解壓縮時會損壞。如果我使用標準的zlib頭文件來創建文件,當我使用相應的調用來膨脹時,給我一個-3/Z_DATA_ERROR的返回值,表示我的defalte數據已損壞。一切都指向了錯誤的東西,但它幾乎就是這個例子。

任何想法??非常感謝!

int main(int argc, char **argv) 
{ 
    int ret; 
    FILE *source; 
    FILE *zip; 
    FILE *zipped; 
    FILE *back; 

    source = fopen ("C:\\Users\\schmoudm\\Pictures\\caela.jpg", "r"); 
    zip = fopen ("C:\\Core\\RD\\test.gz", "w"); 

    printf ("calling def \n"); 
    ret = def(source, zip, Z_DEFAULT_COMPRESSION); 
    printf("def return: %i \n", ret); 
    fclose(source); 
    fclose(zip); 

    if (ret == 0) { 
     printf ("setting up inf \n"); 
     zipped = fopen ("C:\\Core\\RD\\test.gz", "r"); 
     back = fopen ("C:\\Core\\RD\\zlibout.txt", "w"); 
     printf ("calling inf \n"); 
     ret = inf(zipped, back); 
     printf("inf return: %i \n", ret); 
     zerr(ret); 
    } 

    fclose(source); 
    fclose(zip); 

    printf("DONE!"); 
    system("PAUSE"); 
    return 0; 
} 

int def(FILE *source, FILE *dest, int level) 
{ 
    int ret, flush; 
    unsigned have; 
    z_stream strm; 
    unsigned char in[CHUNK]; 
    unsigned char out[CHUNK]; 

    /* allocate deflate state */ 
    strm.zalloc = Z_NULL; 
    strm.zfree = Z_NULL; 
    strm.opaque = Z_NULL; 

    ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION); 
    //ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8,  Z_DEFAULT_STRATEGY); 
    if (ret != Z_OK) 
     return ret; 

    /* compress until end of file */ 
    do { 
     strm.avail_in = fread(in, 1, CHUNK, source); 
     printf("available in: %u \n", strm.avail_in); 
     if (ferror(source)) { 
      (void)deflateEnd(&strm); 
      return Z_ERRNO; 
     } 
     flush = feof(source) ? Z_FINISH : Z_NO_FLUSH; 
     strm.next_in = in; 

     /* run deflate() on input until output buffer not full, finish 
      compression if all of source has been read in */ 
     do { 
      strm.avail_out = CHUNK; 
      strm.next_out = out; 
      ret = deflate(&strm, flush); /* no bad return value */ 
      assert(ret != Z_STREAM_ERROR); /* state not clobbered */ 
      have = CHUNK - strm.avail_out; 
      if (fwrite(out, 1, have, dest) != have || ferror(dest)) { 
       (void)deflateEnd(&strm); 
       return Z_ERRNO; 
      } 
     } while (strm.avail_out == 0); 
     assert(strm.avail_in == 0);  /* all input will be used */ 

     /* done when last data in file processed */ 
    } while (flush != Z_FINISH); 
    assert(ret == Z_STREAM_END);  /* stream will be complete */ 

    /* clean up and return */ 
    (void)deflateEnd(&strm); 
    return Z_OK; 
} 
+1

在Windows上,你不應該打開帶有「rb」和「wb」的二進制文件嗎? –

回答

3

但其幾乎完全的例子。

唉,您遺漏了zpipe.c的關鍵部分。

#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) 
# include <fcntl.h> 
# include <io.h> 
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) 
#else 
# define SET_BINARY_MODE(file) 
#endif 

/* avoid end-of-line conversions */ 
SET_BINARY_MODE(stdin); 
SET_BINARY_MODE(stdout); 

因此,在你的代碼,你需要做的SET_BINARY_MODE(source)SET_BINARY_MODE(zip)

或者您需要在fopen()中使用"b"選項。

+0

我有包含和定義,但「失去」對SET_BINARY_MODE的調用。非常感謝! – schmouder