equal
deleted
inserted
replaced
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 import java.io.*; |
24 import java.io.*; |
25 import java.nio.charset.Charset; |
25 import java.nio.charset.Charset; |
|
26 import java.text.MessageFormat; |
26 import java.util.*; |
27 import java.util.*; |
27 import java.util.zip.*; |
28 import java.util.zip.CRC32; |
28 import java.text.MessageFormat; |
29 import java.util.zip.ZipEntry; |
|
30 import java.util.zip.ZipException; |
|
31 import java.util.zip.ZipFile; |
|
32 import java.util.zip.ZipInputStream; |
|
33 import java.util.zip.ZipOutputStream; |
29 |
34 |
30 /** |
35 /** |
31 * A stripped-down version of Jar tool with a "-encoding" option to |
36 * A stripped-down version of Jar tool with a "-encoding" option to |
32 * support non-UTF8 encoidng for entry name and comment. |
37 * support non-UTF8 encoidng for entry name and comment. |
33 */ |
38 */ |
37 String fname; |
42 String fname; |
38 String zname = ""; |
43 String zname = ""; |
39 String[] files; |
44 String[] files; |
40 Charset cs = Charset.forName("UTF-8"); |
45 Charset cs = Charset.forName("UTF-8"); |
41 |
46 |
42 Map<String, File> entryMap = new HashMap<String, File>(); |
47 Map<String, File> entryMap = new HashMap<>(); |
43 Set<File> entries = new LinkedHashSet<File>(); |
48 Set<File> entries = new LinkedHashSet<>(); |
44 List<String> paths = new ArrayList<String>(); |
49 List<String> paths = new ArrayList<>(); |
45 |
50 |
46 CRC32 crc32 = new CRC32(); |
51 CRC32 crc32 = new CRC32(); |
47 /* |
52 /* |
48 * cflag: create |
53 * cflag: create |
49 * uflag: update |
54 * uflag: update |
328 addFile(zos, file); |
333 addFile(zos, file); |
329 } |
334 } |
330 } |
335 } |
331 } |
336 } |
332 |
337 |
333 boolean update(InputStream in, OutputStream out) throws IOException |
338 boolean update(InputStream in, OutputStream out) throws IOException { |
334 { |
|
335 try (ZipInputStream zis = new ZipInputStream(in, cs); |
339 try (ZipInputStream zis = new ZipInputStream(in, cs); |
336 ZipOutputStream zos = new ZipOutputStream(out, cs)) |
340 ZipOutputStream zos = new ZipOutputStream(out, cs)) |
337 { |
341 { |
338 ZipEntry e = null; |
342 ZipEntry e = null; |
339 byte[] buf = new byte[1024]; |
343 byte[] buf = new byte[1024]; |
340 int n = 0; |
344 int n = 0; |
341 boolean updateOk = true; |
|
342 |
345 |
343 // put the old entries first, replace if necessary |
346 // put the old entries first, replace if necessary |
344 while ((e = zis.getNextEntry()) != null) { |
347 while ((e = zis.getNextEntry()) != null) { |
345 String name = e.getName(); |
348 String name = e.getName(); |
346 if (!entryMap.containsKey(name)) { // copy the old stuff |
349 if (!entryMap.containsKey(name)) { // copy the old stuff |
365 entries.remove(f); |
368 entries.remove(f); |
366 } |
369 } |
367 } |
370 } |
368 |
371 |
369 // add the remaining new files |
372 // add the remaining new files |
370 for (File f: entries) { |
373 for (File f : entries) { |
371 addFile(zos, f); |
374 addFile(zos, f); |
372 } |
375 } |
373 } |
376 } |
374 return updateOk; |
377 return true; |
375 } |
378 } |
376 |
379 |
377 private String entryName(String name) { |
380 private String entryName(String name) { |
378 name = name.replace(File.separatorChar, '/'); |
381 name = name.replace(File.separatorChar, '/'); |
379 String matchPath = ""; |
382 String matchPath = ""; |
477 } |
480 } |
478 } |
481 } |
479 |
482 |
480 Set<ZipEntry> newDirSet() { |
483 Set<ZipEntry> newDirSet() { |
481 return new HashSet<ZipEntry>() { |
484 return new HashSet<ZipEntry>() { |
|
485 private static final long serialVersionUID = 4547977575248028254L; |
|
486 |
482 public boolean add(ZipEntry e) { |
487 public boolean add(ZipEntry e) { |
483 return (e == null || super.add(e)); |
488 return (e == null || super.add(e)); |
484 }}; |
489 }}; |
485 } |
490 } |
486 |
491 |
518 try (ZipFile zf = new ZipFile(fname, cs)) { |
523 try (ZipFile zf = new ZipFile(fname, cs)) { |
519 Set<ZipEntry> dirs = newDirSet(); |
524 Set<ZipEntry> dirs = newDirSet(); |
520 Enumeration<? extends ZipEntry> zes = zf.entries(); |
525 Enumeration<? extends ZipEntry> zes = zf.entries(); |
521 while (zes.hasMoreElements()) { |
526 while (zes.hasMoreElements()) { |
522 ZipEntry e = zes.nextElement(); |
527 ZipEntry e = zes.nextElement(); |
523 InputStream is; |
|
524 if (files == null) { |
528 if (files == null) { |
525 dirs.add(extractFile(zf.getInputStream(e), e)); |
529 dirs.add(extractFile(zf.getInputStream(e), e)); |
526 } else { |
530 } else { |
527 String name = e.getName(); |
531 String name = e.getName(); |
528 for (String file : files) { |
532 for (String file : files) { |
531 break; |
535 break; |
532 } |
536 } |
533 } |
537 } |
534 } |
538 } |
535 } |
539 } |
536 } |
540 updateLastModifiedTime(dirs); |
537 updateLastModifiedTime(dirs); |
541 } |
538 } |
542 } |
539 |
543 |
540 ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException { |
544 ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException { |
541 ZipEntry rc = null; |
545 ZipEntry rc = null; |
542 String name = e.getName(); |
546 String name = e.getName(); |
725 st.wordChars(' ', 255); |
729 st.wordChars(' ', 255); |
726 st.whitespaceChars(0, ' '); |
730 st.whitespaceChars(0, ' '); |
727 st.commentChar('#'); |
731 st.commentChar('#'); |
728 st.quoteChar('"'); |
732 st.quoteChar('"'); |
729 st.quoteChar('\''); |
733 st.quoteChar('\''); |
730 while (st.nextToken() != st.TT_EOF) { |
734 while (st.nextToken() != StreamTokenizer.TT_EOF) { |
731 args.add(st.sval); |
735 args.add(st.sval); |
732 } |
736 } |
733 r.close(); |
737 r.close(); |
734 } |
738 } |
735 |
739 |
736 public static void main(String args[]) { |
740 public static void main(String args[]) { |
737 zip z = new zip(System.out, System.err, "zip"); |
741 zip z = new zip(System.out, System.err, "zip"); |
738 System.exit(z.run(args) ? 0 : 1); |
742 System.exit(z.run(args) ? 0 : 1); |
739 } |
743 } |
740 } |
744 } |
741 |
|