| 1 4 package gnu.bytecode; 5 import java.io.*; 6 import java.util.zip.*; 7 8 13 14 public class ZipArchive 15 { 16 private static void usage () 17 { 18 System.err.println ("zipfile [ptxq] archive [file ...]"); 19 System.exit (-1); 20 } 21 22 public static long copy(InputStream in, OutputStream out, byte[] buffer) 23 throws IOException 24 { 25 long total = 0; 26 for (;;) 27 { 28 int count = in.read(buffer); 29 if (count <= 0) 30 return total; 31 out.write(buffer, 0, count); 32 total += count; 33 } 34 } 35 36 public static void copy(InputStream in, String name, byte[] buffer) 37 throws IOException 38 { 39 File f = new File(name); 40 String dir_name = f.getParent(); 41 if (dir_name != null) 42 { 43 File dir = new File(dir_name); 44 if (! dir.exists()) 45 System.err.println("mkdirs:"+dir.mkdirs()); 46 } 47 if (name.charAt (name.length () - 1) != '/') 48 { 49 OutputStream out = new BufferedOutputStream(new FileOutputStream(f)); 50 copy(in, out, buffer); 51 out.close (); 52 } 53 } 54 55 74 75 public static void main (String args[]) throws IOException 76 { 77 if (args.length < 2) 78 usage (); 79 String command = args[0]; 80 String archive_name = args[1]; 81 82 try 83 { 84 if (command.equals ("t") 85 || command.equals ("p") 86 || command.equals ("x")) 87 { 88 PrintStream out = System.out; 89 byte[] buf = new byte[1024]; 90 if (args.length == 2) 91 { 92 BufferedInputStream in 93 = new BufferedInputStream(new FileInputStream(archive_name)); 94 ZipInputStream zin = new ZipInputStream (in); 95 ZipEntry zent; 96 while ((zent = zin.getNextEntry()) != null) 97 { 98 String name = zent.getName(); 99 if (command.equals("t")) 100 { 101 out.print(name); 102 out.print(" size: "); 103 out.println(zent.getSize()); 104 } 105 else if (command.equals("p")) 106 { 107 copy(zin, out, buf); 108 } 109 else { 111 copy(zin, name, buf); 112 } 113 } 114 } 115 else 116 { 117 ZipFile zar = new ZipFile(archive_name); 118 for (int i = 2; i < args.length; i++) 119 { 120 String name = args[i]; 121 ZipEntry zent = zar.getEntry(name); 122 if (zent == null) 123 { 124 System.err.println ("zipfile " + archive_name + ":" + 125 args[i] + " - not found"); 126 System.exit (-1); 127 } 128 else if (command.equals("t")) 129 { 130 out.print(name); 131 out.print(" size: "); 132 out.println(zent.getSize()); 133 } 134 else if (command.equals("p")) 135 { 136 copy(zar.getInputStream(zent), out, buf); 137 } 138 else { 140 copy(zar.getInputStream(zent), name, buf); 141 } 142 } 143 } 144 } 145 else if (command.equals ("q")) 146 { 147 ZipOutputStream zar 148 = new ZipOutputStream(new FileOutputStream(archive_name)); 149 for (int i = 2; i < args.length; i++) 150 { 151 File in = new File (args[i]); 152 if (!in.exists ()) 153 throw new IOException (args[i] + " - not found"); 154 if (!in.canRead ()) 155 throw new IOException (args[i] + " - not readable"); 156 int size = (int) in.length (); 157 FileInputStream fin = new FileInputStream (in); 158 byte[] contents = new byte[size]; 159 if (fin.read (contents) != size) 160 throw new IOException (args[i] + " - read error"); 161 fin.close (); 162 163 ZipEntry ze = new ZipEntry(args[i]); 164 ze.setSize(size); 165 ze.setTime(in.lastModified()); 166 zar.putNextEntry(ze); 167 zar.write(contents, 0, size); 168 } 169 zar.close (); 170 } 171 else 172 usage (); 173 } 174 catch (IOException ex) 175 { 176 System.err.println ("I/O Exception: " + ex); 177 } 178 } 179 } 180 | Popular Tags |