summaryrefslogtreecommitdiff
path: root/DevIL/test/Unzip/nv_unzip.cpp
blob: 61bef60845a0b1a8bd08477dd031605c5a4f0246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*********************************************************************NVMH1****
File:
nv_unzip.cpp

Copyright (C) 1999, 2000 NVIDIA Corporation
This file is provided without support, instruction, or implied warranty of any
kind.  NVIDIA makes no guarantee of its fitness for a particular purpose and is
not liable under any circumstances for any damages or loss whatsoever arising
from the use or inability to use this file or items derived from it.

Comments:


******************************************************************************/

#ifndef __nv_util_h__
#include <nv_util.h>
#endif // __nv_util_h__

#ifndef _unz_H
#include <unzip.h>
#endif // _unz_H

namespace unzip
{
    unsigned char * open(const char * filename, const char * inzipfile, unsigned int * size)
    {
	    char filename_inzip[256];
        int err=UNZ_OK;
        FILE *fout=NULL;
        unsigned char * buf;
        unzFile uf=NULL;
    
        uf = unzOpen(filename);

        if (unzLocateFile(uf,inzipfile,0)!=UNZ_OK) //CASESENSITIVITY
        {
            printf("file %s not found in the zipfile\n",filename);
            return NULL;
        }

        unz_file_info file_info;
	    uLong ratio=0;
	    err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);

	    if (err!=UNZ_OK)
	    {
		    printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
		    return NULL;
	    }

  
  	    err = unzOpenCurrentFile(uf);
	    if (err!=UNZ_OK)
	    {
		    printf("error %d with zipfile in unzOpenCurrentFile\n",err);
            return NULL;
	    }

        *size = file_info.uncompressed_size;
        buf = new unsigned char[file_info.uncompressed_size];

        unsigned int count = 0;
        err = 1;
        while (err > 0)
        {
            err = unzReadCurrentFile(uf,&buf[count],65535);
	        if (err<0)	
	        {
		        printf("error %d with zipfile in unzReadCurrentFile\n",err);
                break;
	        }
            else
                count += err;
        }
        assert(count == file_info.uncompressed_size);
        if (err==UNZ_OK)
        {
		    err = unzCloseCurrentFile (uf);
		    if (err!=UNZ_OK)
		    {
			    printf("error %d with zipfile in unzCloseCurrentFile\n",err);
		    }
        }
        else
        {
            *size = 0;
            delete [] buf;
            buf = NULL;
            unzCloseCurrentFile(uf); /* don't lose the error */
        }

        return buf;
    }

} // namespace unzip