File: zziptest.c

package info (click to toggle)
zziplib 0.13.56-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,760 kB
  • ctags: 2,135
  • sloc: sh: 12,147; ansic: 7,392; perl: 2,826; python: 2,190; makefile: 968; sed: 44
file content (153 lines) | stat: -rw-r--r-- 4,131 bytes parent folder | download | duplicates (5)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Author: 
 *	Guido Draheim <guidod@gmx.de>
 *	Tomi Ollila <Tomi.Ollila@iki.fi>
 *
 *	Copyright (c) 1999,2000,2001,2002,2003 Guido Draheim
 * 	    All rights reserved,
 *	    use under the restrictions of the
 *	    Lesser GNU General Public License
 *          or alternatively the restrictions 
 *          of the Mozilla Public License 1.1
 */

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

#if defined _MSC_VER /* Win32*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define sleep Sleep
#endif

#include <zzip/lib.h>

#ifdef ZZIP_HAVE_UNISTD_H
#include <unistd.h> /* sleep */
#endif

#ifndef O_BINARY
#define O_BINARY 0
#endif

#if __GNUC__+0 >= 3 && __GNUC_MINOR__+0 >= 3
# ifdef DEBUG
# warning suppress a warning where the compiler should have optimized instead.
# endif
#define I_(_T,_L,_R) do { _T _l = (_T) _L; \
                          _l _R; _L = (typeof(_L)) _l; } while(0)
#else
#define I_(_T,_L,_R) _L _R
#endif

int main(int argc, char ** argv)
{
    ZZIP_DIR * dir;
    const char * name = "test.zip";
    zzip_error_t rv;
    int i;
  
    if (argc > 1 && argv[1] != NULL)
    {
	if (! strcmp (argv[1], "--help")) {
	    printf ("zziptest [testfile]\n - selftest defaults to 'test.zip'");
	    return 0;
	}else if (! strcmp (argv[1], "--version")) {
	    printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
	    return 0;
	}else{
	    name = argv[1];
	    argv++; argc--;
	}
    }

    printf("Opening zip file `%s'... ", name);
    { 	int fd = open (name, O_RDONLY|O_BINARY);
        if (fd == -1) { perror ("could not open input file"); return 0; }
        if (! (dir = zzip_dir_fdopen(fd, &rv)))
        {
            printf("error %d.\n", rv);
            return 0;
        }
    } printf("OK.\n");
  
#if 1
    printf("{check...\n");
    { struct zzip_dir_hdr * hdr = dir->hdr0;
    
        if (hdr == NULL) 
          { printf ("could not find first header in dir_hdr"); }
        else
        {   
            while (1)
            {
                printf("\ncompression method: %d", hdr->d_compr);
                if (hdr->d_compr == 0) printf(" (stored)");
                else if (hdr->d_compr == 8) printf(" (deflated)");
                else printf(" (unknown)");
                printf("\ncrc32: %x\n", hdr->d_crc32);
                printf("compressed size: %d\n", hdr->d_csize);
                printf("uncompressed size: %d\n", hdr->d_usize);
                printf("offset of file in archive: %d\n", hdr->d_off);
                printf("filename: %s\n\n", hdr->d_name);
    
                if (hdr->d_reclen == 0) break;
                I_(char *, hdr, += hdr->d_reclen);
                sleep(1);
            }
        }
    } printf ("\n}\n");
#endif
#if 1  
    { 	printf("{contents...\n");
        for (i = 0; i < 2; i++)
        {
            ZZIP_DIRENT* d;
    
            while((d=zzip_readdir(dir)))
            {
                printf(" name \"%s\", compr %d, size %d, ratio %2d\n", 
                    d->d_name, d->d_compr, d->st_size, 
                    100 - (d->d_csize|1)*100/(d->st_size|1));
            }
            printf(" %d. time ---------------\n", i + 1);
            zzip_rewinddir(dir);
        }
        printf("}...OK\n");
    }
#endif
  
    {   ZZIP_FILE * fp;
        char buf[17];
        const char * name = argv[1]? argv[1]: "README";


        printf("Opening file `%s' in zip archive... ", name);    
        fp = zzip_file_open(dir, (char *)name, ZZIP_CASEINSENSITIVE);

        if (! fp)
          { printf("error %d: %s\n", zzip_error(dir), zzip_strerror_of(dir)); }
        else
        {
            printf("OK.\n");
            printf("Contents of the file:\n");

            while (0 < (i = zzip_file_read(fp, buf, 16)))
            {
                buf[i] = '\0';
                /*printf("\n*** read %d ***\n", i); fflush(stdout); */
                printf("%s", buf);
                /*write(1, buf, i);*/ /* Windows does not have write !!! */
            }
            if (i < 0) printf("error %d\n", zzip_error(dir));
        }
    }  
    
    return 0;
} 

/* 
 * Local variables:
 * c-file-style: "stroustrup"
 * End:
 */