12 static void zip(Biobuf *bout, char *file, int stdout);
13 static void zipDir(Biobuf *bout, int fd, ZipHead *zh, int stdout);
14 static int crcread(void *fd, void *buf, int n);
15 static int zwrite(void *bout, void *buf, int n);
16 static void put4(Biobuf *b, ulong v);
17 static void put2(Biobuf *b, int v);
18 static void put1(Biobuf *b, int v);
19 static void header(Biobuf *bout, ZipHead *zh);
20 static void trailer(Biobuf *bout, ZipHead *zh, vlong off);
21 static void putCDir(Biobuf *bout);
23 static void error(char*, ...);
24 #pragma varargck argpos error 1
37 static ZipHead *zheads;
43 fprint(2, "usage: zip [-vD] [-1-9] [-f zipfile] file ...\n");
48 main(int argc, char *argv[])
67 case '1': case '2': case '3': case '4':
68 case '5': case '6': case '7': case '8': case '9':
79 crctab = mkcrctab(ZCrcPoly);
82 sysfatal("deflateinit failed: %s", flateerr(err));
87 fd = create(zfile, OWRITE, 0664);
89 sysfatal("can't create %s: %r", zfile);
91 Binit(&bout, fd, OWRITE);
95 fprint(2, "zip: removing output file %s\n", zfile);
101 for(i = 0; i < argc; i++)
102 zip(&bout, argv[i], zfile == nil);
110 zip(Biobuf *bout, char *file, int stdout)
118 fd = open(file, OREAD);
120 error("can't open %s: %r", file);
123 error("can't stat %s: %r", file);
128 if(nzheads >= zhalloc){
129 zhalloc += HeadAlloc;
130 zheads = realloc(zheads, zhalloc * sizeof(ZipHead));
132 error("out of memory");
134 zh = &zheads[nzheads++];
136 zh->madevers = (2 * 10) + 0;
138 zh->extvers = (2 * 10) + 0;
140 t = localtime(dir->mtime);
141 zh->modtime = (t->hour<<11) | (t->min<<5) | (t->sec>>1);
142 zh->moddate = ((t->year-80)<<9) | ((t->mon+1)<<5) | t->mday;
148 zh->file = strdup(file);
150 error("out of memory");
153 if((dir->mode & 0700) == 0)
154 zh->eattr |= ZDROnly;
155 zh->off = Boffset(bout);
157 if(dir->mode & DMDIR){
160 zipDir(bout, fd, zh, stdout);
164 zh->flags |= ZTrailInfo;
172 err = deflate(bout, zwrite, (void*)fd, crcread, level, debug);
174 error("deflate failed: %s: %r", flateerr(err));
179 trailer(bout, zh, off);
186 zipDir(Biobuf *bout, int fd, ZipHead *zh, int stdout)
192 nf = strlen(zh->file) + 1;
193 if(strcmp(zh->file, ".") == 0){
202 error("out of memory");
203 snprint(pfile, nf, "%s/", zh->file);
209 nf += 256; /* plenty of room */
212 error("out of memory");
213 while((nd = dirread(fd, &dirs)) > 0){
214 for(i = 0; i < nd; i++){
215 snprint(file, nf, "%s%s", pfile, dirs[i].name);
216 zip(bout, file, stdout);
223 header(Biobuf *bout, ZipHead *zh)
228 fprint(2, "adding %s\n", zh->file);
230 put1(bout, zh->extvers);
231 put1(bout, zh->extos);
232 put2(bout, zh->flags);
233 put2(bout, zh->meth);
234 put2(bout, zh->modtime);
235 put2(bout, zh->moddate);
237 put4(bout, zh->csize);
238 put4(bout, zh->uncsize);
239 flen = strlen(zh->file);
242 if(Bwrite(bout, zh->file, flen) != flen)
243 error("write error");
247 trailer(Biobuf *bout, ZipHead *zh, vlong off)
252 if(!(zh->flags & ZTrailInfo)){
253 coff = Boffset(bout);
254 if(Bseek(bout, off + ZHeadCrc, 0) < 0)
255 error("can't seek in archive");
258 put4(bout, zh->csize);
259 put4(bout, zh->uncsize);
260 if(!(zh->flags & ZTrailInfo)){
261 if(Bseek(bout, coff, 0) < 0)
262 error("can't seek in archive");
267 cheader(Biobuf *bout, ZipHead *zh)
271 put4(bout, ZCHeader);
272 put1(bout, zh->madevers);
273 put1(bout, zh->madeos);
274 put1(bout, zh->extvers);
275 put1(bout, zh->extos);
276 put2(bout, zh->flags & ~ZTrailInfo);
277 put2(bout, zh->meth);
278 put2(bout, zh->modtime);
279 put2(bout, zh->moddate);
281 put4(bout, zh->csize);
282 put4(bout, zh->uncsize);
283 flen = strlen(zh->file);
288 put2(bout, zh->iattr);
289 put4(bout, zh->eattr);
291 if(Bwrite(bout, zh->file, flen) != flen)
292 error("write error");
296 putCDir(Biobuf *bout)
301 hoff = Boffset(bout);
303 for(i = 0; i < nzheads; i++)
304 cheader(bout, &zheads[i]);
306 ecoff = Boffset(bout);
308 if(nzheads >= (1 << 16))
309 error("too many entries in zip file: max %d", (1 << 16) - 1);
310 put4(bout, ZECHeader);
315 put4(bout, ecoff - hoff);
321 crcread(void *fd, void *buf, int n)
326 for(; !eof && n > 0; n -= m){
327 m = read((int)(uintptr)fd, (char*)buf+nr, n);
332 fprint(2, "input error %r\n");
339 crc = blockcrc(crctab, crc, buf, nr);
345 zwrite(void *bout, void *buf, int n)
347 if(n != Bwrite(bout, buf, n)){
356 put4(Biobuf *b, ulong v)
360 for(i = 0; i < 4; i++){
362 error("write error");
368 put2(Biobuf *b, int v)
372 for(i = 0; i < 2; i++){
374 error("write error");
380 put1(Biobuf *b, int v)
383 error("unexpected eof reading file information");
387 error(char *fmt, ...)
393 vfprint(2, fmt, arg);