305 }
306 } else if (f.isDirectory()) {
307 if (entries.add(f)) {
308 if (isUpdate) {
309 String dirPath = f.getPath();
310 dirPath = (dirPath.endsWith(File.separator)) ? dirPath :
311 (dirPath + File.separator);
312 entryMap.put(entryName(dirPath), f);
313 }
314 expand(f, f.list(), isUpdate);
315 }
316 } else {
317 error(formatMsg("error.nosuch.fileordir", String.valueOf(f)));
318 ok = false;
319 }
320 }
321 }
322
323 void create(OutputStream out) throws IOException
324 {
325 ZipOutputStream zos = new ZipOutputStream(out, cs);
326 if (flag0) {
327 zos.setMethod(ZipOutputStream.STORED);
328 }
329 for (File file: entries) {
330 addFile(zos, file);
331 }
332 zos.close();
333 }
334
335 boolean update(InputStream in, OutputStream out) throws IOException
336 {
337 ZipInputStream zis = new ZipInputStream(in, cs);
338 ZipOutputStream zos = new ZipOutputStream(out, cs);
339 ZipEntry e = null;
340 byte[] buf = new byte[1024];
341 int n = 0;
342 boolean updateOk = true;
343
344 // put the old entries first, replace if necessary
345 while ((e = zis.getNextEntry()) != null) {
346 String name = e.getName();
347 if (!entryMap.containsKey(name)) { // copy the old stuff
348 // do our own compression
349 ZipEntry e2 = new ZipEntry(name);
350 e2.setMethod(e.getMethod());
351 e2.setTime(e.getTime());
352 e2.setComment(e.getComment());
353 e2.setExtra(e.getExtra());
354 if (e.getMethod() == ZipEntry.STORED) {
355 e2.setSize(e.getSize());
356 e2.setCrc(e.getCrc());
357 }
358 zos.putNextEntry(e2);
359 while ((n = zis.read(buf, 0, buf.length)) != -1) {
360 zos.write(buf, 0, n);
361 }
362 } else { // replace with the new files
363 File f = entryMap.get(name);
364 addFile(zos, f);
365 entryMap.remove(name);
366 entries.remove(f);
367 }
368 }
369
370 // add the remaining new files
371 for (File f: entries) {
372 addFile(zos, f);
373 }
374 zis.close();
375 zos.close();
376 return updateOk;
377 }
378
379 private String entryName(String name) {
380 name = name.replace(File.separatorChar, '/');
381 String matchPath = "";
382 for (String path : paths) {
383 if (name.startsWith(path) && (path.length() > matchPath.length())) {
384 matchPath = path;
385 }
386 }
387 name = name.substring(matchPath.length());
388
389 if (name.startsWith("/")) {
390 name = name.substring(1);
391 } else if (name.startsWith("./")) {
392 name = name.substring(2);
393 }
394 return name;
395 }
500 ZipInputStream zis = new ZipInputStream(in, cs);
501 ZipEntry e;
502 Set<ZipEntry> dirs = newDirSet();
503 while ((e = zis.getNextEntry()) != null) {
504 if (files == null) {
505 dirs.add(extractFile(zis, e));
506 } else {
507 String name = e.getName();
508 for (String file : files) {
509 if (name.startsWith(file)) {
510 dirs.add(extractFile(zis, e));
511 break;
512 }
513 }
514 }
515 }
516 updateLastModifiedTime(dirs);
517 }
518
519 void extract(String fname, String files[]) throws IOException {
520 ZipFile zf = new ZipFile(fname, cs);
521 Set<ZipEntry> dirs = newDirSet();
522 Enumeration<? extends ZipEntry> zes = zf.entries();
523 while (zes.hasMoreElements()) {
524 ZipEntry e = zes.nextElement();
525 InputStream is;
526 if (files == null) {
527 dirs.add(extractFile(zf.getInputStream(e), e));
528 } else {
529 String name = e.getName();
530 for (String file : files) {
531 if (name.startsWith(file)) {
532 dirs.add(extractFile(zf.getInputStream(e), e));
533 break;
534 }
535 }
536 }
537 }
538 zf.close();
539 updateLastModifiedTime(dirs);
540 }
541
542 ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException {
543 ZipEntry rc = null;
544 String name = e.getName();
545 File f = new File(e.getName().replace('/', File.separatorChar));
546 if (e.isDirectory()) {
547 if (f.exists()) {
548 if (!f.isDirectory()) {
549 throw new IOException(formatMsg("error.create.dir",
550 f.getPath()));
551 }
552 } else {
553 if (!f.mkdirs()) {
554 throw new IOException(formatMsg("error.create.dir",
555 f.getPath()));
556 } else {
557 rc = e;
558 }
590 }
591 }
592 }
593 long lastModified = e.getTime();
594 if (lastModified != -1) {
595 f.setLastModified(lastModified);
596 }
597 return rc;
598 }
599
600 void list(InputStream in, String files[]) throws IOException {
601 ZipInputStream zis = new ZipInputStream(in, cs);
602 ZipEntry e;
603 while ((e = zis.getNextEntry()) != null) {
604 zis.closeEntry();
605 printEntry(e, files);
606 }
607 }
608
609 void list(String fname, String files[]) throws IOException {
610 ZipFile zf = new ZipFile(fname, cs);
611 Enumeration<? extends ZipEntry> zes = zf.entries();
612 while (zes.hasMoreElements()) {
613 printEntry(zes.nextElement(), files);
614 }
615 zf.close();
616 }
617
618 void printEntry(ZipEntry e, String[] files) throws IOException {
619 if (files == null) {
620 printEntry(e);
621 } else {
622 String name = e.getName();
623 for (String file : files) {
624 if (name.startsWith(file)) {
625 printEntry(e);
626 return;
627 }
628 }
629 }
630 }
631
632 void printEntry(ZipEntry e) throws IOException {
633 if (vflag) {
634 StringBuilder sb = new StringBuilder();
635 String s = Long.toString(e.getSize());
|
305 }
306 } else if (f.isDirectory()) {
307 if (entries.add(f)) {
308 if (isUpdate) {
309 String dirPath = f.getPath();
310 dirPath = (dirPath.endsWith(File.separator)) ? dirPath :
311 (dirPath + File.separator);
312 entryMap.put(entryName(dirPath), f);
313 }
314 expand(f, f.list(), isUpdate);
315 }
316 } else {
317 error(formatMsg("error.nosuch.fileordir", String.valueOf(f)));
318 ok = false;
319 }
320 }
321 }
322
323 void create(OutputStream out) throws IOException
324 {
325 try (ZipOutputStream zos = new ZipOutputStream(out, cs)) {
326 if (flag0) {
327 zos.setMethod(ZipOutputStream.STORED);
328 }
329 for (File file: entries) {
330 addFile(zos, file);
331 }
332 }
333 }
334
335 boolean update(InputStream in, OutputStream out) throws IOException
336 {
337 try (ZipInputStream zis = new ZipInputStream(in, cs);
338 ZipOutputStream zos = new ZipOutputStream(out, cs))
339 {
340 ZipEntry e = null;
341 byte[] buf = new byte[1024];
342 int n = 0;
343 boolean updateOk = true;
344
345 // put the old entries first, replace if necessary
346 while ((e = zis.getNextEntry()) != null) {
347 String name = e.getName();
348 if (!entryMap.containsKey(name)) { // copy the old stuff
349 // do our own compression
350 ZipEntry e2 = new ZipEntry(name);
351 e2.setMethod(e.getMethod());
352 e2.setTime(e.getTime());
353 e2.setComment(e.getComment());
354 e2.setExtra(e.getExtra());
355 if (e.getMethod() == ZipEntry.STORED) {
356 e2.setSize(e.getSize());
357 e2.setCrc(e.getCrc());
358 }
359 zos.putNextEntry(e2);
360 while ((n = zis.read(buf, 0, buf.length)) != -1) {
361 zos.write(buf, 0, n);
362 }
363 } else { // replace with the new files
364 File f = entryMap.get(name);
365 addFile(zos, f);
366 entryMap.remove(name);
367 entries.remove(f);
368 }
369 }
370
371 // add the remaining new files
372 for (File f: entries) {
373 addFile(zos, f);
374 }
375 }
376 return updateOk;
377 }
378
379 private String entryName(String name) {
380 name = name.replace(File.separatorChar, '/');
381 String matchPath = "";
382 for (String path : paths) {
383 if (name.startsWith(path) && (path.length() > matchPath.length())) {
384 matchPath = path;
385 }
386 }
387 name = name.substring(matchPath.length());
388
389 if (name.startsWith("/")) {
390 name = name.substring(1);
391 } else if (name.startsWith("./")) {
392 name = name.substring(2);
393 }
394 return name;
395 }
500 ZipInputStream zis = new ZipInputStream(in, cs);
501 ZipEntry e;
502 Set<ZipEntry> dirs = newDirSet();
503 while ((e = zis.getNextEntry()) != null) {
504 if (files == null) {
505 dirs.add(extractFile(zis, e));
506 } else {
507 String name = e.getName();
508 for (String file : files) {
509 if (name.startsWith(file)) {
510 dirs.add(extractFile(zis, e));
511 break;
512 }
513 }
514 }
515 }
516 updateLastModifiedTime(dirs);
517 }
518
519 void extract(String fname, String files[]) throws IOException {
520 try (ZipFile zf = new ZipFile(fname, cs)) {
521 Set<ZipEntry> dirs = newDirSet();
522 Enumeration<? extends ZipEntry> zes = zf.entries();
523 while (zes.hasMoreElements()) {
524 ZipEntry e = zes.nextElement();
525 InputStream is;
526 if (files == null) {
527 dirs.add(extractFile(zf.getInputStream(e), e));
528 } else {
529 String name = e.getName();
530 for (String file : files) {
531 if (name.startsWith(file)) {
532 dirs.add(extractFile(zf.getInputStream(e), e));
533 break;
534 }
535 }
536 }
537 }
538 }
539 updateLastModifiedTime(dirs);
540 }
541
542 ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException {
543 ZipEntry rc = null;
544 String name = e.getName();
545 File f = new File(e.getName().replace('/', File.separatorChar));
546 if (e.isDirectory()) {
547 if (f.exists()) {
548 if (!f.isDirectory()) {
549 throw new IOException(formatMsg("error.create.dir",
550 f.getPath()));
551 }
552 } else {
553 if (!f.mkdirs()) {
554 throw new IOException(formatMsg("error.create.dir",
555 f.getPath()));
556 } else {
557 rc = e;
558 }
590 }
591 }
592 }
593 long lastModified = e.getTime();
594 if (lastModified != -1) {
595 f.setLastModified(lastModified);
596 }
597 return rc;
598 }
599
600 void list(InputStream in, String files[]) throws IOException {
601 ZipInputStream zis = new ZipInputStream(in, cs);
602 ZipEntry e;
603 while ((e = zis.getNextEntry()) != null) {
604 zis.closeEntry();
605 printEntry(e, files);
606 }
607 }
608
609 void list(String fname, String files[]) throws IOException {
610 try (ZipFile zf = new ZipFile(fname, cs)) {
611 Enumeration<? extends ZipEntry> zes = zf.entries();
612 while (zes.hasMoreElements()) {
613 printEntry(zes.nextElement(), files);
614 }
615 }
616 }
617
618 void printEntry(ZipEntry e, String[] files) throws IOException {
619 if (files == null) {
620 printEntry(e);
621 } else {
622 String name = e.getName();
623 for (String file : files) {
624 if (name.startsWith(file)) {
625 printEntry(e);
626 return;
627 }
628 }
629 }
630 }
631
632 void printEntry(ZipEntry e) throws IOException {
633 if (vflag) {
634 StringBuilder sb = new StringBuilder();
635 String s = Long.toString(e.getSize());
|