posts - 36, comments - 30, trackbacks - 0, articles - 0

zlib

Posted on 2007-10-11 11:36 vcommon 阅读(1270) 评论(0)  编辑 收藏 引用

A:http://zlib.net/                                                               zlib1.2.3
B:http://www.info-zip.org/                                               zipUtil

C:引自http://www.cppblog.com/artmouse/archive/2005/12/13/1705.html
1说能用在wince里面的了,但是好像用起来烦了点
http://www.codeproject.com/file/zip_utils.asp
 2还有一个是国产的,但是wince里面用不了,
http://www.vckbase.com/document/viewdoc/?id=1265


zip_util:

Example 1 - create a Zip file from existing files


// We place the file "simple.bmp" inside, but inside
  // the zipfile it will actually be called "znsimple.bmp".
  // Similarly the textfile.

  HZIP hz = CreateZip("simple1.zip",0);
  ZipAdd(hz,"znsimple.bmp",  "simple.bmp");
  ZipAdd(hz,"znsimple.txt",  "simple.txt");
  CloseZip(hz);
 

Example 2 - unzip a Zip file using the names it has inside it

  HZIP hz = OpenZip("\\simple1.zip",0);
ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
// -1 gives overall information about the zipfile
for (int zi=0; zi<numitems; zi++)
{ ZIPENTRY ze; GetZipItem(hz,zi,&ze); // fetch individual details
UnzipItem(hz, zi, ze.name);         // e.g. the item's name.
}
CloseZip(hz);

Example 3- unzip from resource directly into memory

HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
  HANDLE hglob = LoadResource(hInstance,hrsrc);
  void *zipbuf = LockResource(hglob);
  unsigned int ziplen = SizeofResource(hInstance,hrsrc);
  hz = OpenZip(zipbuf, ziplen, 0);
  ZIPENTRY ze; int i; FindZipItem(hz,"sample.jpg",true,&i,&ze);
  // that lets us search for an item by filename.
  // Now we unzip it to a membuffer.
  char *ibuf = new char[ze.unc_size];
  UnzipItem(hz,i, ibuf, ze.unc_size);
  ...
  delete[] ibuf;
  CloseZip(hz);
  // note: no need to free resources obtained through Find/Load/LockResource

Example 4 - unzip chunk by chunk to a membuffer

char buf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0;
  while (zr==ZR_MORE)
  { zr = UnzipItem(hz,i, buf,1024);
    unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
    ... maybe write the buffer to a disk file here
    totsize+=bufsize;
  }

zlib1.2.3

1:compress和uncompress:压缩和解压
compress(&out,&outlen,in,inlen);
2:gz file:
gzopen,gzseek,gzclose,gztell
写入gz: gzputc单个字符,gzputs字符串,gzprintf以format的方式写入.
读取gz:gzread,gzgetc,gzungetc(把前边的字符变成c),gzgets
3:deflate算法是先利用LZ77压缩后用Huffman在压缩的
deflateInit_,deflate,deflateEnd,deflateParams
inflateInit,inflate,inflateEnd
inflateSync,deflateSetDictionary,inflateSetDictionary



void test_compress(compr, comprLen, uncompr, uncomprLen)
    Byte *compr, *uncompr;
    uLong comprLen, uncomprLen;
{
    int err;
    uLong len = (uLong)strlen(hello)+1;

    err = compress(compr, &comprLen, (const Bytef*)hello, len);
    CHECK_ERR(err, "compress");

    strcpy((char*)uncompr, "garbage");

    err = uncompress(uncompr, &uncomprLen, compr, comprLen);
    CHECK_ERR(err, "uncompress");

    if (strcmp((char*)uncompr, hello)) {
        fprintf(stderr, "bad uncompress\n");
        exit(1);
    } else {
        printf("uncompress(): %s\n", (char *)uncompr);
    }
}

/* ===========================================================================
 * Test read/write of .gz files
 */
void test_gzio(fname, uncompr, uncomprLen)
    const char *fname; /* compressed file name */
    Byte *uncompr;
    uLong uncomprLen;
{
#ifdef NO_GZCOMPRESS
    fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
#else
    int err;
    int len = (int)strlen(hello)+1;
    gzFile file;
    z_off_t pos;

    file = gzopen(fname, "wb");
    if (file == NULL) {
        fprintf(stderr, "gzopen error\n");
        exit(1);
    }
    gzputc(file, 'h');
    if (gzputs(file, "ello") != 4) {
        fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
        exit(1);
    }
    if (gzprintf(file, ", %s!", "hello") != 8) {
        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
        exit(1);
    }
    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    gzclose(file);

    file = gzopen(fname, "rb");
    if (file == NULL) {
        fprintf(stderr, "gzopen error\n");
        exit(1);
    }
    strcpy((char*)uncompr, "garbage");

    if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
        fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
        exit(1);
    }
    if (strcmp((char*)uncompr, hello)) {
        fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
        exit(1);
    } else {
        printf("gzread(): %s\n", (char*)uncompr);
    }

    pos = gzseek(file, -8L, SEEK_CUR);
    if (pos != 6 || gztell(file) != pos) {
        fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
                (long)pos, (long)gztell(file));
        exit(1);
    }

    if (gzgetc(file) != ' ') {
        fprintf(stderr, "gzgetc error\n");
        exit(1);
    }

    if (gzungetc(' ', file) != ' ') {
        fprintf(stderr, "gzungetc error\n");
        exit(1);
    }

    gzgets(file, (char*)uncompr, (int)uncomprLen);
    if (strlen((char*)uncompr) != 7) { /* " hello!" */
        fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
        exit(1);
    }
    if (strcmp((char*)uncompr, hello + 6)) {
        fprintf(stderr, "bad gzgets after gzseek\n");
        exit(1);
    } else {
        printf("gzgets() after gzseek: %s\n", (char*)uncompr);
    }

    gzclose(file);
#endif
}

只有注册用户登录后才能发表评论。