Zip Utils--简单优雅的C++接口

在codeproject上看到的大神写的,真牛逼。
地址:
http://www.codeproject.com/Articles/7530/Zip-Utils-clean-elegant-simple-C-Win

使用,下载四个文件:
Zip Utils--简单优雅的C++接口_第1张图片

将四个文件加入到你的工程目录中,然后包含头文件即可。

For example

1 创建zip

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

别忘了关闭close!!!!

2 遍历zip内部文件名进行解压

HZIP hz = OpenZip("\\simple1.zip",0);
ZIPENTRY ze; 
GetZipItem(hz,-1,&ze); 
int numitems = ze.index;
for (int zi = 0; zi < numitems; zi++)
{
    ZIPENTRY ze; 
    GetZipItem(hz, zi, &ze); 
    UnzipItem(hz, zi, ze.name);         
}
CloseZip(hz);

注意,numitems有时候不是真正的文件数目。一般情况是,比真实的文件数多1。
可以重点看一下结构体ZIPENTRY 。

3 解压文件到内存

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

4 向已经存在的zip中添加文件

ZRESULT AddFileToZip(const TCHAR *zipfn, const TCHAR *zename, const TCHAR *zefn)
{
    if (GetFileAttributes(zipfn) == 0xFFFFFFFF || (zefn != 0 && GetFileAttributes(zefn) == 0xFFFFFFFF))
        return ZR_NOFILE;
    // Expected size of the new zip will be the size of the old zip plus the size of the new file

    HANDLE hf = CreateFile(zipfn, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
    if (hf == INVALID_HANDLE_VALUE)
        return ZR_NOFILE; DWORD size = GetFileSize(hf, 0);
    CloseHandle(hf);

    if (zefn != 0)
    {
        hf = CreateFile(zefn, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
        if (hf == INVALID_HANDLE_VALUE)
            return ZR_NOFILE; size += GetFileSize(hf, 0);
        CloseHandle(hf);
    }

    size *= 2; // just to be on the safe side.


    HZIP hzsrc = OpenZip(zipfn, 0); if (hzsrc == 0) return ZR_READ;

    HZIP hzdst = CreateZip(0, size, 0);
    if (hzdst == 0) { CloseZip(hzsrc); return ZR_WRITE; }
    // hzdst is created in the system pagefile
    // Now go through the old zip, unzipping each item into a memory buffer, and adding it to the new one
    char *buf = 0;
    unsigned int bufsize = 0; // we'll unzip each item into this memory buffer
    ZIPENTRY ze;
    ZRESULT zr = GetZipItem(hzsrc, -1, &ze);
    int numitems = ze.index;
    if (zr != ZR_OK) { CloseZip(hzsrc); CloseZip(hzdst); return zr; }

    for (int i = 0; iif (zr != ZR_OK) { CloseZip(hzsrc); CloseZip(hzdst); return zr; }

        if (stricmp(ze.name, zename) == 0) continue; // don't copy over the old version of the file we're changing

        if (ze.attr&FILE_ATTRIBUTE_DIRECTORY) { zr = ZipAddFolder(hzdst, ze.name); if (zr != ZR_OK) { CloseZip(hzsrc); CloseZip(hzdst); return zr; } continue; }

        if (ze.unc_size>(long)bufsize)
        {
            if (buf != 0) delete[] buf; bufsize = ze.unc_size * 2; buf = new char[bufsize];
        }

        zr = UnzipItem(hzsrc, i, buf, bufsize);
        if (zr != ZR_OK) { CloseZip(hzsrc); CloseZip(hzdst); return zr; }
        zr = ZipAdd(hzdst, ze.name, buf, bufsize); if (zr != ZR_OK) { CloseZip(hzsrc); CloseZip(hzdst); return zr; }
    }
    delete[] buf;
    // Now add the new file
    if (zefn != 0) { zr = ZipAdd(hzdst, zename, zefn); if (zr != ZR_OK) { CloseZip(hzsrc); CloseZip(hzdst); return zr; } }
    zr = CloseZip(hzsrc); if (zr != ZR_OK) { CloseZip(hzdst); return zr; }
    //
    // The new file has been put into pagefile memory. Let's store it to disk, overwriting the original zip
    zr = ZipGetMemory(hzdst, (void**)&buf, &size); 
    if (zr != ZR_OK) { CloseZip(hzdst); return zr; }
    hf = CreateFile(zipfn, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); 
    if (hf == INVALID_HANDLE_VALUE) { CloseZip(hzdst); return ZR_WRITE; }
    DWORD writ; WriteFile(hf, buf, size, &writ, 0); CloseHandle(hf);
    zr = CloseZip(hzdst); if (zr != ZR_OK) return zr;
    return ZR_OK;
}

实际上就是新建了一个zip,然后把已经存在的zip中的文件拷贝过去!!!!!

5 从已经存在的zip中删除文件

ZRESULT RemoveFileFromZip(const TCHAR *zipfn, const TCHAR *zename)
{
    return AddFileToZip(zipfn, zename, 0);
}

你可能感兴趣的:(C++)